Repository navigation
249 lines (232 loc) · 9.45 KB
/
Copy pathrelease.yml
File metadata and controls
249 lines (232 loc) · 9.45 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
name: Release
# Publishes @diffbot/typescript to npm (with provenance) and creates the matching
# GitHub Release. Triggered by pushing a version tag (e.g. v0.6.0); also runnable
# manually, which runs every gate plus `npm publish --dry-run` and publishes nothing.
#
# npm auth: trusted publishing (OIDC) only. No token is passed to npm, so a
# misconfigured trusted publisher fails the publish instead of silently using a token.
#
# Release candidates: set package.json and src/constants.ts to a prerelease version
# (e.g. 0.6.0-rc.0) and push the matching tag (v0.6.0-rc.0). Any version containing
# "-" is published under the "next" dist-tag (so "latest" is untouched) and its GitHub
# Release is marked as a prerelease. Its notes come from a "## [0.6.0-rc.0]" CHANGELOG
# section if there is one, otherwise from the base version's "## [0.6.0]" section.
# Live tests are deliberately NOT run here; see live-tests.yml.
on:
push:
tags: ["v*"]
workflow_dispatch:
permissions:
contents: read
# Re-pushing a tag (or re-dispatching) queues behind an in-flight run for the same
# ref. Unlike a binaries build, a publish must never be cancelled halfway.
concurrency:
group: release-${{ github.ref }}
cancel-in-progress: false
jobs:
verify:
name: Verify version
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
version: ${{ steps.version.outputs.version }}
steps:
- uses: actions/checkout@v4
# Fail fast if package.json and src/constants.ts disagree, or the tag doesn't
# match them, so npm, the User-Agent (VERSION) and the GitHub Release can never
# report different versions.
- name: Check tag matches package version
id: version
run: |
set -euo pipefail
pkg=$(node -p 'require("./package.json").version')
const=$(sed -n 's/^export const VERSION = "\([^"]*\)";.*/\1/p' src/constants.ts)
if [ -z "$const" ]; then
echo "::error::Could not read VERSION from src/constants.ts."
exit 1
fi
if [ "$pkg" != "$const" ]; then
echo "::error::package.json version ${pkg} does not match src/constants.ts VERSION ${const}."
exit 1
fi
if [ "${GITHUB_REF_TYPE}" = "tag" ]; then
tag=${GITHUB_REF_NAME#v}
if [ "$pkg" != "$tag" ]; then
echo "::error::Tag v${tag} does not match package.json version ${pkg}. Bump package.json and src/constants.ts, then re-tag."
exit 1
fi
fi
echo "version=${pkg}" >> "$GITHUB_OUTPUT"
echo "Versions agree: ${pkg}"
# Checked before publishing so a tag without release notes fails before
# anything reaches npm.
- name: Check CHANGELOG.md has a section for this version
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
set -euo pipefail
# A prerelease (0.6.0-rc.0) may reuse its base version's section (0.6.0).
if grep -qF "## [${VERSION}]" CHANGELOG.md; then
echo "Found '## [${VERSION}]'."
elif [ "${VERSION%%-*}" != "${VERSION}" ] && grep -qF "## [${VERSION%%-*}]" CHANGELOG.md; then
echo "Prerelease ${VERSION}: using the '## [${VERSION%%-*}]' section."
else
echo "::error::CHANGELOG.md has no '## [${VERSION}]' section."
exit 1
fi
test:
name: Gates (Node ${{ matrix.node-version }})
needs: verify
runs-on: ubuntu-latest
timeout-minutes: 20
strategy:
matrix:
node-version: [18, 20, 22]
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm typecheck
- run: pnpm lint
- run: pnpm test
- run: pnpm test:types
# `build` runs the `postbuild` check (scripts/assert-no-node-builtins.mjs).
- run: pnpm build
workers:
name: cloudflare worker fixture
needs: verify
runs-on: ubuntu-latest
timeout-minutes: 20
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
- run: pnpm install --frozen-lockfile
- run: pnpm build
- run: pnpm install --frozen-lockfile
working-directory: fixtures/worker
- run: pnpm exec wrangler deploy --dry-run --outdir .wrangler-build
working-directory: fixtures/worker
- run: pnpm exec vitest run
working-directory: fixtures/worker
publish:
name: Publish to npm
needs: [verify, test, workers]
runs-on: ubuntu-latest
timeout-minutes: 15
permissions:
contents: read
id-token: write # npm trusted publishing (OIDC) and provenance
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 22
cache: pnpm
registry-url: https://registry.npmjs.org
# Trusted publishing needs npm >= 11.5.1; Node 22 bundles npm 10.
- run: npm install -g npm@^11.5.1
- run: pnpm install --frozen-lockfile
- run: pnpm build
- name: Publish
env:
VERSION: ${{ needs.verify.outputs.version }}
run: |
set -euo pipefail
name=$(node -p 'require("./package.json").name')
# Prereleases (v1.2.0-rc.1) go to the "next" dist-tag, never "latest".
dist_tag=latest
case "$VERSION" in *-*) dist_tag=next ;; esac
# Only a tag push publishes. A manual dispatch (even on a tag ref) is a dry run.
if [ "${GITHUB_EVENT_NAME}" != "push" ] || [ "${GITHUB_REF_TYPE}" != "tag" ]; then
echo "Not a tag push: dry run only."
npm publish --dry-run --access public --tag "$dist_tag"
exit 0
fi
# Lets a failed run be re-run after the npm step already succeeded.
if npm view "${name}@${VERSION}" version >/dev/null 2>&1; then
echo "${name}@${VERSION} is already on npm; skipping publish."
exit 0
fi
# Trusted publishing (OIDC) only; no token is exported.
echo "Publishing ${name}@${VERSION} with dist-tag ${dist_tag} via npm trusted publishing."
npm publish --access public --provenance --tag "$dist_tag"
# A final release also moves `next`, so `next` is never behind `latest`.
# Uses npm trusted publishing (OIDC) for `npm dist-tag`, which needs npm
# >= 11.21.0 / 12.2.0 and "Allow npm dist-tag" enabled on this package's
# trusted publisher at npmjs.com (off by default). The version is already
# published at this point, so a failure only warns and never fails the run.
- name: Point next at final release
if: github.event_name == 'push' && github.ref_type == 'tag'
run: |
set -uo pipefail
name=$(node -p 'require("./package.json").name')
version=$(node -p 'require("./package.json").version')
case "$version" in
*-*) echo "Prerelease ${version}: next was set by publish."; exit 0 ;;
esac
if npm dist-tag add "${name}@${version}" next; then
echo "next -> ${name}@${version}"
else
echo "::warning::Could not point next at ${name}@${version}. Enable 'Allow npm dist-tag' on the trusted publisher for ${name} at npmjs.com, or run: npm dist-tag add ${name}@${version} next"
fi
release:
name: Publish GitHub Release
needs: [verify, publish]
runs-on: ubuntu-latest
timeout-minutes: 5
# Only for real tag pushes; workflow_dispatch runs just the gates + dry run.
if: github.event_name == 'push' && github.ref_type == 'tag'
permissions:
contents: write
steps:
- uses: actions/checkout@v4
- name: Extract release notes from CHANGELOG.md
env:
VERSION: ${{ needs.verify.outputs.version }}
run: |
set -euo pipefail
# Everything after "## [VERSION]" up to the next "## " heading. A prerelease
# without its own section uses its base version's section.
hdr_version="${VERSION}"
if ! grep -qF "## [${VERSION}]" CHANGELOG.md; then
hdr_version="${VERSION%%-*}"
fi
awk -v hdr="## [${hdr_version}]" '
index($0, hdr) == 1 { found = 1; next }
found && /^## / { exit }
found { print }
' CHANGELOG.md > "${RUNNER_TEMP}/notes.md"
if ! grep -q '[^[:space:]]' "${RUNNER_TEMP}/notes.md"; then
echo "::error::CHANGELOG.md section for ${VERSION} is empty."
exit 1
fi
wc -l "${RUNNER_TEMP}/notes.md"
- name: Publish release
env:
GH_TOKEN: ${{ github.token }}
VERSION: ${{ needs.verify.outputs.version }}
run: |
set -euo pipefail
tag="${GITHUB_REF_NAME}"
repo="${GITHUB_REPOSITORY}"
if gh release view "$tag" --repo "$repo" >/dev/null 2>&1; then
echo "Release $tag already exists; leaving its notes as they are."
exit 0
fi
prerelease=()
case "$VERSION" in *-*) prerelease=(--prerelease) ;; esac
gh release create "$tag" \
--repo "$repo" \
--title "$tag" \
--verify-tag \
--notes-file "${RUNNER_TEMP}/notes.md" \
"${prerelease[@]}"