-
Notifications
You must be signed in to change notification settings - Fork 0
298 lines (254 loc) · 9.92 KB
/
Copy pathci.yml
File metadata and controls
298 lines (254 loc) · 9.92 KB
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
name: CI
on:
pull_request:
types:
- opened
- synchronize
- reopened
- closed
branches:
- master
- main
push:
branches:
- master
- main
permissions:
contents: read
jobs:
test:
name: Test and coverage
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v4
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 24
cache: yarn
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Run lint and tests with coverage
run: yarn lint && yarn test:coverage
- name: Build package
run: yarn build
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v5
with:
files: ./tests/coverage/lcov.info
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
publish:
name: Publish to npm
runs-on: ubuntu-latest
needs: test
concurrency:
group: publish-${{ github.repository }}-${{ github.event.pull_request.base.ref }}
cancel-in-progress: false
permissions:
contents: write
id-token: write
if: >-
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.merged == true &&
(
github.event.pull_request.base.ref == 'master' ||
github.event.pull_request.base.ref == 'main'
) &&
(
github.event.pull_request.head.ref == 'feat' ||
startsWith(github.event.pull_request.head.ref, 'feat/') ||
github.event.pull_request.head.ref == 'fix' ||
startsWith(github.event.pull_request.head.ref, 'fix/') ||
github.event.pull_request.head.ref == 'break' ||
startsWith(github.event.pull_request.head.ref, 'break/')
)
steps:
- name: Checkout repository
uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.base.ref }}
persist-credentials: false
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 24
cache: yarn
registry-url: https://registry.npmjs.org/
- name: Install dependencies
run: yarn install --frozen-lockfile
- name: Build package
run: yarn build
- name: Compute release version
id: release
shell: bash
run: |
set -euo pipefail
package_name="$(node -p "require('./package.json').name")"
current_version="$(node -p "require('./package.json').version")"
published_version="$(npm view "$package_name" version)"
branch_name="${HEAD_BRANCH}"
release_marker="for #${PR_NUMBER}"
echo "already_released=false" >> "$GITHUB_OUTPUT"
case "$branch_name" in
fix|fix/*)
bump="patch"
;;
feat|feat/*)
bump="minor"
;;
break|break/*)
bump="major"
;;
*)
echo "Unsupported release branch: $branch_name"
exit 1
;;
esac
release_subject="$(git log --format=%s --grep="$release_marker" -n 1 HEAD || true)"
if [ -n "$release_subject" ] && grep -q "$release_marker" <<< "$release_subject"; then
next_version="$(
node - "$release_subject" "$current_version" <<'NODE'
const [subject, fallbackVersion] = process.argv.slice(2);
const match = subject.match(/Release v([^ ]+) for #\d+/);
process.stdout.write(match?.[1] ?? fallbackVersion);
NODE
)"
echo "Release for PR #${PR_NUMBER} already exists."
echo "already_released=true" >> "$GITHUB_OUTPUT"
echo "package_name=$package_name" >> "$GITHUB_OUTPUT"
echo "current_version=$current_version" >> "$GITHUB_OUTPUT"
echo "published_version=$published_version" >> "$GITHUB_OUTPUT"
echo "next_version=$next_version" >> "$GITHUB_OUTPUT"
echo "bump=$bump" >> "$GITHUB_OUTPUT"
echo "should_publish=false" >> "$GITHUB_OUTPUT"
exit 0
fi
next_version="$(
node - "$current_version" "$bump" <<'NODE'
const [version, bump] = process.argv.slice(2);
const parts = version.split('.').map(Number);
if (parts.length !== 3 || parts.some((part) => !Number.isInteger(part))) {
throw new Error(`Unsupported npm version: ${version}`);
}
if (bump === 'patch') parts[2] += 1;
if (bump === 'minor') {
parts[1] += 1;
parts[2] = 0;
}
if (bump === 'major') {
parts[0] += 1;
parts[1] = 0;
parts[2] = 0;
}
process.stdout.write(parts.join('.'));
NODE
)"
npm version "$next_version" --no-git-tag-version
should_publish="true"
if [ "$published_version" = "$next_version" ]; then
should_publish="false"
fi
if [ "$should_publish" = "true" ]; then
node - "$published_version" "$next_version" <<'NODE'
const [publishedVersion, nextVersion] = process.argv.slice(2);
const parse = (version) => version.split('.').map(Number);
const [publishedMajor, publishedMinor, publishedPatch] = parse(publishedVersion);
const [nextMajor, nextMinor, nextPatch] = parse(nextVersion);
const published = [publishedMajor, publishedMinor, publishedPatch];
const next = [nextMajor, nextMinor, nextPatch];
const comparison = published.findIndex((part, index) => part !== next[index]);
if (comparison !== -1 && published[comparison] > next[comparison]) {
throw new Error(
`Published npm version ${publishedVersion} is newer than computed release ${nextVersion}`,
);
}
NODE
fi
echo "package_name=$package_name" >> "$GITHUB_OUTPUT"
echo "current_version=$current_version" >> "$GITHUB_OUTPUT"
echo "published_version=$published_version" >> "$GITHUB_OUTPUT"
echo "next_version=$next_version" >> "$GITHUB_OUTPUT"
echo "bump=$bump" >> "$GITHUB_OUTPUT"
echo "should_publish=$should_publish" >> "$GITHUB_OUTPUT"
env:
HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
PR_NUMBER: ${{ github.event.pull_request.number }}
- name: Publish package
if: >-
steps.release.outputs.already_released != 'true' &&
steps.release.outputs.should_publish == 'true'
run: npm publish --access public --tag latest
- name: Commit release version
if: steps.release.outputs.already_released != 'true'
shell: bash
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add package.json
if git diff --cached --quiet; then
echo "No package.json version changes to commit."
else
git commit -m "chore(release): 🔖 Release v${NEXT_VERSION} for #${PR_NUMBER}"
fi
if git rev-parse "v${NEXT_VERSION}" >/dev/null 2>&1; then
echo "Tag v${NEXT_VERSION} already exists."
else
git tag -a "v${NEXT_VERSION}" -m "Release v${NEXT_VERSION} for #${PR_NUMBER}"
fi
auth_header="$(
node -e "process.stdout.write('AUTHORIZATION: basic ' + Buffer.from('x-access-token:' + process.env.GITHUB_TOKEN).toString('base64'))"
)"
git -c "http.https://github.com/.extraheader=$auth_header" push origin "HEAD:${BASE_BRANCH}"
git -c "http.https://github.com/.extraheader=$auth_header" push origin "v${NEXT_VERSION}"
env:
BASE_BRANCH: ${{ github.event.pull_request.base.ref }}
NEXT_VERSION: ${{ steps.release.outputs.next_version }}
PR_NUMBER: ${{ github.event.pull_request.number }}
GITHUB_TOKEN: ${{ github.token }}
- name: Create GitHub release notes
if: steps.release.outputs.next_version != ''
shell: bash
run: |
set -euo pipefail
tag="v${NEXT_VERSION}"
release_notes="$(mktemp)"
pr_title="$(gh pr view "$PR_NUMBER" --json title --jq '.title')"
pr_body="$(gh pr view "$PR_NUMBER" --json body --jq '.body // ""')"
pr_url="$(gh pr view "$PR_NUMBER" --json url --jq '.url')"
{
echo "## Changes"
echo
printf -- "- %s\n" "$pr_title"
echo
echo "## Release details"
echo
printf -- "- npm: https://www.npmjs.com/package/%s/v/%s\n" "$PACKAGE_NAME" "$NEXT_VERSION"
printf -- "- Pull request: %s\n" "$pr_url"
printf -- "- Source branch: \`%s\`\n" "$HEAD_BRANCH"
printf -- "- Version bump: \`%s\`\n" "$BUMP"
} > "$release_notes"
if [ -n "$pr_body" ]; then
{
echo
echo "## Pull request notes"
echo
printf '%s\n' "$pr_body" | sed '/^------$/,$d'
} >> "$release_notes"
fi
if gh release view "$tag" >/dev/null 2>&1; then
gh release edit "$tag" --title "$tag" --notes-file "$release_notes"
else
gh release create "$tag" --verify-tag --title "$tag" --notes-file "$release_notes"
fi
env:
BUMP: ${{ steps.release.outputs.bump }}
GH_TOKEN: ${{ github.token }}
HEAD_BRANCH: ${{ github.event.pull_request.head.ref }}
NEXT_VERSION: ${{ steps.release.outputs.next_version }}
PACKAGE_NAME: ${{ steps.release.outputs.package_name }}
PR_NUMBER: ${{ github.event.pull_request.number }}