Repository navigation
310 lines (255 loc) · 12.5 KB
/
Copy pathnpm-prepare-release.yml
File metadata and controls
310 lines (255 loc) · 12.5 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
299
300
301
302
303
304
305
306
307
308
309
310
name: Prepare npm release
on:
workflow_dispatch:
inputs:
version:
description: Target version to prepare, for example 0.2.3
required: true
type: string
permissions:
actions: write
contents: write
concurrency:
group: npm-prepare-release
cancel-in-progress: false
jobs:
prepare:
runs-on: ubuntu-latest
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: true
steps:
- uses: actions/checkout@v7
with:
ref: ${{ github.ref_name }}
fetch-depth: 0
token: ${{ secrets.AUTOFIX_TOKEN }}
- uses: actions/setup-node@v7
with:
node-version: '24.x'
package-manager-cache: false
- name: Install dependencies
run: npm ci
- name: Configure Git identity
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git branch --set-upstream-to="origin/${GITHUB_REF_NAME}"
- name: Build admin UI
run: npm run build:admin
- name: Check syntax
run: npm run check
- name: Lint
run: npm run lint
- name: Test
run: npm test
- name: Remember current version
run: echo "OLD_VERSION=$(node -p 'require(\"./package.json\").version')" >> "$GITHUB_ENV"
- name: Determine version bump
env:
TARGET_VERSION: ${{ inputs.version }}
run: |
BUMP="$(node <<'NODE'
const semver = require("semver");
const current = require("./package.json").version;
const target = process.env.TARGET_VERSION;
if (!semver.valid(target)) {
throw new Error(`Target version ${target} is not valid semver`);
}
if (!semver.gt(target, current)) {
throw new Error(`Target version ${target} must be greater than current version ${current}`);
}
const currentVersion = semver.parse(current);
const targetVersion = semver.parse(target);
let bump;
if (
targetVersion.major > currentVersion.major &&
targetVersion.prerelease.length === 0
) {
bump = "major";
} else if (
targetVersion.major === currentVersion.major &&
targetVersion.minor > currentVersion.minor &&
targetVersion.prerelease.length === 0
) {
bump = "minor";
} else if (
targetVersion.major === currentVersion.major &&
targetVersion.minor === currentVersion.minor &&
targetVersion.patch > currentVersion.patch &&
targetVersion.prerelease.length === 0
) {
bump = "patch";
} else {
throw new Error(
`Target version ${target} must be a later patch, minor, or major version after ${current}`,
);
}
console.log(bump);
NODE
)"
echo "TARGET_VERSION=${TARGET_VERSION}" >> "$GITHUB_ENV"
echo "BUMP=${BUMP}" >> "$GITHUB_ENV"
- name: Prepare release commit
run: |
node <<'NODE'
const fs = require("node:fs");
const target = process.env.TARGET_VERSION;
const ioPackage = require("./io-package.json");
const news = ioPackage.common.news ?? {};
if (Object.prototype.hasOwnProperty.call(news, target)) {
throw new Error(
`io-package.json already contains common.news.${target}. ` +
"Add pending release notes below README.md's WORK IN PROGRESS heading instead.",
);
}
const readme = fs.readFileSync("README.md", "utf8");
const match = readme.match(
/^### \*\*WORK IN PROGRESS\*\*\n\n([\s\S]*?)(?=\n### |\n\[Older changelogs|\n## |$)/m,
);
if (!match || !match[1].trim()) {
throw new Error(
"README.md must contain pending release notes below the WORK IN PROGRESS heading.",
);
}
NODE
npm run release -- "$BUMP" --yes --all --noPush
- name: Ensure release commit is complete
run: |
PREPARED_VERSION="$(node -p 'require("./package.json").version')"
export PREPARED_VERSION
node <<'NODE'
const fs = require("node:fs");
const target = process.env.TARGET_VERSION;
const prepared = require("./package.json").version;
function writeJson(path, data) {
fs.writeFileSync(path, `${JSON.stringify(data, null, 4)}\n`);
}
const packageJson = require("./package.json");
packageJson.version = target;
writeJson("package.json", packageJson);
const packageLock = require("./package-lock.json");
packageLock.version = target;
packageLock.packages[""].version = target;
writeJson("package-lock.json", packageLock);
const ioPackage = require("./io-package.json");
ioPackage.common.news ??= {};
const generatedNews = ioPackage.common.news?.[prepared];
ioPackage.common.version = target;
if (prepared !== target && generatedNews) {
delete ioPackage.common.news[prepared];
ioPackage.common.news[target] = generatedNews;
}
writeJson("io-package.json", ioPackage);
const readmePath = "README.md";
const readme = fs.readFileSync(readmePath, "utf8");
fs.writeFileSync(
readmePath,
readme.replace(new RegExp(`^### ${prepared} \\(([^\\n]+)\\)`, "m"), `### ${target} ($1)`),
);
NODE
VERSION="$(node -p 'require("./package.json").version')"
TAG="v${VERSION}"
if [[ "$VERSION" != "$TARGET_VERSION" ]]; then
echo "Prepared version ${VERSION}, expected ${TARGET_VERSION}" >&2
exit 1
fi
if [[ "$VERSION" == "$OLD_VERSION" ]]; then
echo "Release version did not change from ${OLD_VERSION}" >&2
exit 1
fi
node <<'NODE'
const target = process.env.TARGET_VERSION;
const prepared = process.env.PREPARED_VERSION;
const ioPackage = require("./io-package.json");
const news = ioPackage.common.news || {};
if (ioPackage.common.version !== target) {
throw new Error(`io-package version ${ioPackage.common.version} does not match ${target}`);
}
if (!Object.prototype.hasOwnProperty.call(news, target)) {
throw new Error(`io-package common.news is missing ${target}`);
}
if (prepared !== target && Object.prototype.hasOwnProperty.call(news, prepared)) {
throw new Error(
`io-package common.news still contains generated intermediate version ${prepared}`,
);
}
NODE
if grep -Fq "### **WORK IN PROGRESS**" README.md; then
echo "README.md still contains the WORK IN PROGRESS changelog placeholder" >&2
exit 1
fi
npm install --package-lock-only --ignore-scripts
npm run format
if [[ -n "$(git status --porcelain)" ]]; then
git add -A
git commit -m "chore: release ${TAG}"
fi
COMMITTED_VERSION="$(git show HEAD:package.json | node -e 'let s = ""; process.stdin.on("data", d => s += d).on("end", () => console.log(JSON.parse(s).version));')"
if [[ "$COMMITTED_VERSION" != "$VERSION" ]]; then
echo "HEAD contains package version ${COMMITTED_VERSION}, but working tree contains ${VERSION}" >&2
exit 1
fi
COMMITTED_LOCK_VERSION="$(git show HEAD:package-lock.json | node -e 'let s = ""; process.stdin.on("data", d => s += d).on("end", () => { const lock = JSON.parse(s); console.log(`${lock.version} ${lock.packages[""].version}`); });')"
if [[ "$COMMITTED_LOCK_VERSION" != "$VERSION $VERSION" ]]; then
echo "HEAD contains package-lock versions ${COMMITTED_LOCK_VERSION}, expected ${VERSION} ${VERSION}" >&2
exit 1
fi
- name: Push release branch
run: |
TAG="v$(node -p 'require("./package.json").version')"
BRANCH="release/${TAG}"
RELEASE_SHA="$(git rev-parse HEAD)"
if git rev-parse --verify --quiet "refs/tags/${TAG}"; then
git tag -d "$TAG"
fi
git fetch origin "${BRANCH}" || true
git push --force-with-lease origin "HEAD:refs/heads/${BRANCH}"
{
echo "TAG=${TAG}"
echo "BRANCH=${BRANCH}"
echo "RELEASE_SHA=${RELEASE_SHA}"
} >> "$GITHUB_ENV"
- name: Wait for release branch checks
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
started_at="$(date +%s)"
while true; do
run="$(gh run list \
--workflow test-and-release.yml \
--branch "${BRANCH}" \
--event push \
--json conclusion,createdAt,databaseId,headSha,status,url \
--limit 20 \
--jq ".[] | select(.headSha == \"${RELEASE_SHA}\" and (.createdAt | fromdateiso8601) >= ${started_at}) | @base64" \
| head -n 1)"
if [[ -n "${run}" ]]; then
status="$(echo "${run}" | base64 --decode | jq -r ".status")"
conclusion="$(echo "${run}" | base64 --decode | jq -r ".conclusion")"
url="$(echo "${run}" | base64 --decode | jq -r ".url")"
echo "test-and-release.yml for ${BRANCH} is ${status}: ${url}"
if [[ "${status}" == "completed" ]]; then
if [[ "${conclusion}" == "success" ]]; then
exit 0
fi
echo "test-and-release.yml for ${BRANCH} completed with ${conclusion}: ${url}" >&2
exit 1
fi
else
echo "Waiting for test-and-release.yml for ${BRANCH} at ${RELEASE_SHA}..."
fi
if (( $(date +%s) - started_at > 900 )); then
echo "Timed out waiting for test-and-release.yml for ${BRANCH}." >&2
exit 1
fi
sleep 10
done
- name: Write summary
run: |
{
echo "Prepared ${TAG} on branch ${BRANCH}."
echo "The test-and-release workflow completed successfully for ${BRANCH}."
echo ""
echo "Review and adjust the release branch before running the npm publish workflow."
echo "Use version ${TAG#v} and ref ${BRANCH} when publishing."
} >> "$GITHUB_STEP_SUMMARY"