Repository navigation
276 lines (269 loc) · 14.8 KB
/
Copy pathrelease.yml
File metadata and controls
276 lines (269 loc) · 14.8 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
name: release
on:
push:
branches: [main]
permissions:
contents: read
# Release runs take turns. Two in parallel would both find a new version missing and race
# to publish it, and the loser would fail red. One in progress is never cancelled.
concurrency:
group: release
cancel-in-progress: false
queue: max # a waiting run is kept rather than replaced by a newer one, though GitHub does not guarantee their order
jobs:
# The oldest and the newest published servers that depend on this package's major, each
# installed together with the packed index, page every item. scripts/oldest-consumer.ts says
# why. It runs on every push, not only on one that publishes, so an index that breaks either
# server shows red even when nothing is published.
oldest-consumer:
runs-on: ubuntu-latest
# The script's own bounds add up to 2190 s, summed beside the value: 120 s to pack the
# candidate and 30 s for the registry, then 120 s to pack, 300 s to install and 600 s to
# sweep each of two consumers. The rest is room for checkout and setup.
timeout-minutes: 45 # 120 + 30 + 2 * (120 + 300 + 600) = 2190 s, 36.5 minutes
permissions:
contents: read
steps:
# No ref: the commit that triggered the run, which is the one the release job publishes.
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
with:
node-version: '26'
# No dependency cache in the release workflow. This setup-node restores one by itself
# whenever package.json names a packageManager, so it is switched off by name.
package-manager-cache: false
# The npm the release job installs, so the gate packs and installs with that same npm.
- run: npm install -g npm@12.0.2
# Installs the pnpm that `packageManager` names, then runs `pnpm install --frozen-lockfile`.
# It leaves the pnpm store uncached, and restores and saves no lockfile-verification record
# either. pnpm/setup keys that record on the hash of the file cache-dependency-path names,
# and restores it whatever the other inputs say. drift's build job writes a new
# pnpm-lock.yaml when it moves the server pin and then runs the generator, so the first
# record under that lockfile's key can come from the generator. This gate decides whether
# the release job publishes, so it trusts no such record. The path names a file the
# repository never holds, so the hash is empty and the action neither restores nor saves one.
# require-lockfile still finds pnpm-lock.yaml by itself.
- uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
with:
install: true
require-lockfile: true
cache-dependency-path: .release-restores-no-cache
- run: pnpm oldest-consumer
release:
# Nothing is published unless the gate passed on this same commit.
needs: oldest-consumer
runs-on: ubuntu-latest
outputs:
released: ${{ steps.publish.outputs.released }} # 'true' once npm accepted the publish, else empty
version: ${{ steps.publish.outputs.version }} # the version it accepted
permissions:
contents: read # check out the merged commit
id-token: write # OIDC for npm trusted publishing
steps:
# No ref: a push run checks out the commit that triggered it, which is the commit npm
# provenance names.
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
persist-credentials: false
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
with:
node-version: '26'
# No dependency cache in the job that publishes. This setup-node restores one by
# itself whenever package.json names a packageManager, so it is switched off by name.
package-manager-cache: false
# setup-node brings the npm bundled with the Node release, and trusted publishing needs
# >= 11.5.1. Pinned exactly, not @latest: this job holds id-token: write. It is
# installed before the check, so the npm that reads the registry is the one that publishes.
- run: npm install -g npm@12.0.2
# A version is published once, when npm does not have it yet. This checks existence and
# never compares with latest: a revert leaves version below latest, and npm publish
# moves latest itself.
- name: Check whether npm already has this version
id: registry
run: |
name=$(node -p 'require("./package.json").name')
version=$(node -p 'require("./package.json").version')
# npm view name@version exits 1 both for a version npm lacks and for a registry it
# cannot read, so this reads the whole version list instead. npm exits non-zero when
# it cannot read the list, and node throws unless it got a non-empty one. Either
# assignment then ends this step red under bash -e, so an outage is never mistaken
# for a missing version.
versions=$(npm view "$name" versions --json)
published=$(node -e '
const [list, version] = process.argv.slice(1);
const versions = JSON.parse(list);
if (!Array.isArray(versions) || versions.length === 0) throw new Error("npm listed no versions: " + list);
console.log(versions.includes(version));
' "$versions" "$version")
if [ "$published" = true ]; then
echo "$name@$version is on npm already, so nothing is published."
else
echo "$name@$version is not on npm yet, so this run publishes it."
echo "publish=true" >> "$GITHUB_OUTPUT"
fi
# Installs the pnpm that `packageManager` names, then runs `pnpm install --frozen-lockfile`.
# No dependency cache in the job that publishes: it leaves the pnpm store uncached, and
# restores and saves no lockfile-verification record, which drift's generator could have
# planted under a lockfile its pin step wrote, as the gate above says. The path names a file
# the repository never holds, so the hash is empty and the action neither restores nor saves
# one.
- uses: pnpm/setup@703c52620218391530e48b9e8870d5c0082e1b9b # v2.1.0
if: ${{ steps.registry.outputs.publish == 'true' }}
with:
install: true
require-lockfile: true
cache-dependency-path: .release-restores-no-cache
# The suite is the major-version guard: SCHEMA_VERSION, the package major and the
# index's mcp_schema_version row must agree before anything is published.
- run: pnpm test
if: ${{ steps.registry.outputs.publish == 'true' }}
# Packs the committed index.db. prepublishOnly runs pnpm test once more first. Then it hands
# the hosting job what it published. The default bash -e stops the script at a failed publish,
# so released is 'true' or empty. The version is read before the publish, so a version this
# step cannot read publishes nothing.
- id: publish
if: ${{ steps.registry.outputs.publish == 'true' }}
run: |
version=$(node -p 'require("./package.json").version')
npm publish
echo "released=true" >> "$GITHUB_OUTPUT"
echo "version=$version" >> "$GITHUB_OUTPUT"
# Tags the commit this run published and creates its GitHub release. A job of its own, because it
# is the only job here that can write to the repository, and it holds no OIDC token: the job that
# publishes to npm cannot tag, and the job that tags cannot publish. It runs beside the hosting
# job and neither waits for the other. It runs only once the release job published, so a push that
# publishes nothing skips it, and a failed publish leaves no tag behind.
github-release:
needs: release
if: ${{ needs.release.outputs.released == 'true' }}
runs-on: ubuntu-latest
# Two steps that run a script, a checkout and a setup-node. 5 minutes is room for the checkout
# of the whole history, which is what takes the time here.
timeout-minutes: 5
permissions:
contents: write # create the tag and the release
env:
VERSION: ${{ needs.release.outputs.version }}
steps:
# No ref: the commit that triggered the run, which is the commit npm provenance names and the
# one this release targets. The whole history and every tag, because the notes read the
# previous release's index out of its tag.
- uses: actions/checkout@08c6903cd8c0fde910a37f88322edcfb5dd907a8 # v5.0.0
with:
fetch-depth: 0
fetch-tags: true
persist-credentials: false
- uses: actions/setup-node@a0853c24544627f65ddf259abe73b1d18a591444 # v5.0.0
with:
node-version: '26'
# No dependency cache in this job either. This setup-node restores one by itself whenever
# package.json names a packageManager, so it is switched off by name.
package-manager-cache: false
# The notes come from the repository's own script, which imports node builtins only, so this
# job installs nothing. The step holds no env of its own, so the script never sees a token.
# pipefail beside the runner's -e: git printing no tags would otherwise read as a first
# release, and the notes would quietly lose their change column.
- name: Write the release notes
id: notes
run: |
set -o pipefail
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::The published version must look like 1.2.3, and this run has \"$VERSION\"."
exit 1
fi
notes="$RUNNER_TEMP/release-notes.md"
previous=$(git tag --list 'v*' | node scripts/release-notes.ts --previous-tag "$VERSION")
if [ -n "$previous" ]; then
index="$RUNNER_TEMP/previous-index.db"
git show "$previous:index.db" > "$index"
# The script takes versions without the v the tag carries.
node scripts/release-notes.ts "$VERSION" index.db "${previous#v}" "$index" > "$notes"
else
node scripts/release-notes.ts "$VERSION" index.db > "$notes"
fi
{
echo "previous=$previous"
echo "notes=$notes"
} >> "$GITHUB_OUTPUT"
# gh reads the token from GH_TOKEN by itself, so the script never names it. gh release create
# creates the tag too, on --target. An existing tag stops the step before that call, because
# --target applies only to a tag gh creates, so a release on a tag pointing elsewhere would
# name the wrong commit. One attempt: a create is one API call, and docs/RELEASING.md gives
# the manual command.
- name: Create the GitHub release
id: create
env:
GH_TOKEN: ${{ github.token }}
PREVIOUS: ${{ steps.notes.outputs.previous }}
NOTES: ${{ steps.notes.outputs.notes }}
run: |
if git rev-parse -q --verify "refs/tags/v$VERSION" > /dev/null; then
echo "::error::The tag v$VERSION exists already, so this run created no release. Look at what it points at, as \"The GitHub release\" in docs/RELEASING.md describes."
exit 1
fi
create=(release create "v$VERSION" --target "$GITHUB_SHA" --title "v$VERSION" --notes-file "$NOTES" --generate-notes)
# The generated notes list the pull requests merged since the previous release. With no
# tag below this version there is nothing to count them from.
if [ -n "$PREVIOUS" ]; then
create+=(--notes-start-tag "$PREVIOUS")
fi
if ! gh "${create[@]}"; then
echo "::error::Could not create the GitHub release v$VERSION. Create it by hand, as \"The GitHub release\" in docs/RELEASING.md describes."
exit 1
fi
# Tells tibia-sh/mcp.tibia.sh about the release, so its bump.yml pins the version and deploys it.
# A job of its own, because it mints a token of the tibia-sh App in the release-trigger
# environment, which deploys from main only, and naming that environment in the release job would
# put an environment claim in its OIDC token, which npm's trusted publisher rejects. It runs only
# once the release job published, so a push that publishes nothing skips it.
hosting:
needs: release
if: ${{ needs.release.outputs.released == 'true' }}
runs-on: ubuntu-latest
timeout-minutes: 8
environment: release-trigger
permissions:
contents: read
env:
VERSION: ${{ needs.release.outputs.version }}
steps:
# A token of the tibia-sh App for mcp.tibia.sh alone, with contents write, which a
# repository_dispatch needs, and which expires within the hour.
- name: Get a token of the tibia-sh App
id: token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
with:
client-id: ${{ vars.TIBIA_SH_APP_CLIENT_ID }}
private-key: ${{ secrets.TIBIA_SH_APP_PRIVATE_KEY }}
owner: tibia-sh
repositories: mcp.tibia.sh
permission-contents: write
# gh api exits non-zero on an HTTP error and prints it, and timeout stops a request that hangs
# after 120 seconds, or kills it 10 seconds later if it ignores that. A dispatch that got through
# twice is harmless, because bump.yml finds the version pinned or its pull request open, so a
# failed attempt is tried again 30 seconds later, three attempts in all. Three attempts that all
# hang take 7.5 minutes, inside the job's 8. gh reads the token from GH_TOKEN by itself, so the
# script never names it.
- name: Tell mcp.tibia.sh about the release
id: dispatch
env:
GH_TOKEN: ${{ steps.token.outputs.token }}
run: |
if [[ ! "$VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::The published version must look like 1.2.3, and this run has \"$VERSION\"."
exit 1
fi
body="$(jq -n --arg version "$VERSION" '{event_type: "first-party-release", client_payload: {package: "@tibia.sh/tibiawiki-data", version: $version}}')"
for attempt in 1 2 3; do
if [ "$attempt" -gt 1 ]; then
sleep 30
fi
if timeout --kill-after=10 120 gh api repos/tibia-sh/mcp.tibia.sh/dispatches --input - <<< "$body"; then
echo "Told tibia-sh/mcp.tibia.sh about @tibia.sh/tibiawiki-data $VERSION in attempt $attempt."
exit 0
fi
done
echo "::error::Could not tell tibia-sh/mcp.tibia.sh about @tibia.sh/tibiawiki-data $VERSION in 3 attempts, 30 seconds apart. Run bump.yml there by hand, as \"The hosting dispatch\" in docs/RELEASING.md describes."
exit 1