forked from ipdxco/unified-github-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (202 loc) · 7.51 KB
/
releaser.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: Releaser
on:
workflow_call:
inputs:
branches:
required: false
type: string
default: ${{ format('["{0}"]', github.event.repository.default_branch) }}
sources:
required: false
type: string
default: '["version.json"]'
separator:
required: false
type: string
default: '/'
outputs:
json:
description: JSON aggregation of release.json artifacts
value: ${{ jobs.aggregate.outputs.json }}
secrets:
UCI_GITHUB_TOKEN:
required: false
jobs:
releaser:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
source: ${{ fromJSON(inputs.sources) }}
steps:
- uses: actions/checkout@v4
- id: version
name: Determine version
env:
SOURCE: ${{ matrix.source }}
SEPARATOR: ${{ inputs.separator }}
run: |
root="$(dirname "$SOURCE")"
source="$(basename "$SOURCE")"
echo "root=$root" | tee -a $GITHUB_OUTPUT
echo "source=$source" | tee -a $GITHUB_OUTPUT
if [[ "$root" == "." ]]; then
prefix="v"
else
name="$(yq -r '.package.name // .name // "'"$root"'"' "$root/$source")"
prefix="${name}${SEPARATOR}v"
fi
echo "prefix=$prefix" | tee -a $GITHUB_OUTPUT
while [[ -z "$version" ]]; do
echo "Checking $root/$source"
if [[ -f "$root/$source" ]]; then
version="$(yq -r '.workspace.package.version // .package.version // .version | select(type == "!!str")' "$root/$source")"
version="${version#v}"
fi
if [[ "$root" == "." ]]; then
break
fi
root="$(dirname "$root")"
done
echo "version=$version" | tee -a $GITHUB_OUTPUT
tag="${prefix}${version}"
echo "tag=$tag" | tee -a $GITHUB_OUTPUT
suffix="${version#*[-+]}"
suffix="${suffix#"$version"}"
echo "suffix=$suffix" | tee -a $GITHUB_OUTPUT
- id: latest
if: steps.version.outputs.version != ''
name: Determine latest version
env:
PREFIX: ${{ steps.version.outputs.prefix }}
TAG: ${{ steps.version.outputs.tag }}
run: |
git fetch origin --tags
echo -e "${TAG}\n$(git tag)" | grep "^${PREFIX}" | sort -V | tail -n1 | xargs -I{} echo "latest={}" | tee -a $GITHUB_OUTPUT
- id: branch
name: Check if the branch is a release branch
if: steps.version.outputs.version != ''
env:
BRANCHES: ${{ inputs.branches }}
REF: ${{ github.ref }}
uses: actions/github-script@v7
with:
script: |
const branches = JSON.parse(process.env.BRANCHES);
const ref = process.env.REF.replace(/^refs\/heads\//, '');
const release = branches.some(b => {
const regexPattern = b.replace(/\*/g, '.*');
const regex = new RegExp(`^${regexPattern}$`);
return regex.test(ref);
});
console.log(`This is a release branch: ${release}`);
core.setOutput('release', release);
- id: pr
if: steps.version.outputs.version != '' && steps.branch.outputs.release == 'false'
name: Check if this is a merge commit of a release PR
env:
GITHUB_TOKEN: ${{ github.token }}
REPOSITORY: ${{ github.repository }}
REF: ${{ github.ref }}
SHA: ${{ github.sha }}
uses: actions/github-script@v7
with:
script: |
const [owner, repo] = process.env.REPOSITORY.split('/');
const ref = process.env.REF.replace(/^refs\/heads\//, '');
const candidates = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: 'closed',
base: ref,
});
let pr;
for (const candidate of candidates) {
if (candidate.merge_commit_sha === process.env.SHA) {
pr = candidate;
break;
}
}
if (pr !== undefined) {
console.log(`Found PR: ${pr.html_url}`);
const labels = pr.labels.map(l => l.name);
const release = labels.includes('release');
console.log(`This is a release PR: ${release}`);
core.setOutput('release', release);
} else {
console.log('No PR found');
core.setOutput('release', false);
}
- id: tag
name: Check if tag already exists
if: steps.branch.outputs.release == 'true' || steps.pr.outputs.release == 'true'
env:
TAG: ${{ steps.version.outputs.tag }}
run: |
git fetch origin --tags
status=0
git rev-list "$TAG" &> /dev/null || status=$?
if [[ $status == 0 ]]; then
echo "exists=true" >> $GITHUB_OUTPUT
else
echo "exists=false" >> $GITHUB_OUTPUT
fi
- id: release
name: Create release
if: steps.tag.outputs.exists == 'false'
uses: galargh/action-gh-release@571276229e7c9e6ea18f99bad24122a4c3ec813f # https://github.com/galargh/action-gh-release/pull/1
with:
draft: false
tag_name: ${{ steps.version.outputs.tag }}
generate_release_notes: true
target_commitish: ${{ github.sha }}
make_latest: ${{ steps.version.outputs.prefix == 'v' && steps.version.outputs.suffix == '' && steps.version.outputs.tag == steps.latest.outputs.latest }}
token: ${{ secrets.UCI_GITHUB_TOKEN || github.token }}
- name: Create release.json
if: steps.release.outputs.id != ''
id: json
env:
SOURCE: ${{ matrix.source }}
RELEASE: |
{
"draft": false,
"version": "${{ steps.version.outputs.tag }}",
"url": "${{ steps.release.outputs.url }}",
"id": "${{ steps.release.outputs.id }}",
"upload_url": "${{ steps.release.outputs.upload_url }}",
"assets": ${{ steps.release.outputs.assets }},
"make_latest": ${{ steps.version.outputs.prefix == 'v' && steps.version.outputs.suffix == '' && steps.version.outputs.tag == steps.latest.outputs.latest }},
"source": "${{ matrix.source }}"
}
run: |
jq . <<< "$RELEASE" > release.json
sed 's/[":<>|*?\r\n\\\/]/_/g' <<< "$SOURCE" | xargs -I {} -0 echo "name={}" | tee -a $GITHUB_OUTPUT
- name: Upload release.json
if: steps.release.outputs.id != ''
uses: actions/upload-artifact@v4
with:
name: ${{ steps.json.outputs.name }}
path: release.json
aggregate:
needs: [releaser]
runs-on: ubuntu-latest
outputs:
json: ${{ toJSON(fromJSON(steps.aggregate.outputs.json)) }}
steps:
- uses: actions/download-artifact@v4
- id: aggregate
run: |
echo "json<<EOF" >> $GITHUB_OUTPUT
echo "{" | tee -a $GITHUB_OUTPUT
for d in *; do
f="$d/release.json"
if [[ -d "$d" && -f "$f" ]]; then
echo "$comma" | tee -a $GITHUB_OUTPUT
jq .source "$f" | tee -a $GITHUB_OUTPUT
echo ":" | tee -a $GITHUB_OUTPUT
jq . "$f" | tee -a $GITHUB_OUTPUT
comma=","
fi
done
echo "}" | tee -a $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT