Repository navigation
546 lines (476 loc) · 21.6 KB
/
Copy pathrelease.yml
File metadata and controls
546 lines (476 loc) · 21.6 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
name: Release CI
on:
push:
branches:
- 'release/[0-9]+.[0-9]+.[0-9]+'
- 'hotfix/[0-9]+.[0-9]+.[0-9]+'
- 'pre-release/[0-9]+.[0-9]+.[0-9]+-alpha.[0-9]+'
- 'pre-release/[0-9]+.[0-9]+.[0-9]+-beta.[0-9]+'
- 'pre-release/[0-9]+.[0-9]+.[0-9]+-rc.[0-9]+'
- 'pre-release/[0-9]+.[0-9]+.[0-9]+-hotfix.[0-9]+'
concurrency:
group: vtable-release-${{ github.ref_name }}
cancel-in-progress: false
jobs:
release:
# 原始 tarball 只构建一次。恢复须选择重新运行失败的作业。
if: github.run_attempt == 1 || !(startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.')))
runs-on: macos-latest
permissions:
id-token: write # OIDC for npm publish (single entry)
contents: write # push commits / tags
pull-requests: write # create PR from release/* to main
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Configure Git identity
run: |
git config user.name ${{ github.actor }}
git config user.email ${{ github.actor }}@users.noreply.github.com
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: ${{ (startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))) && '22.22.2' || '20.x' }}
registry-url: 'https://registry.npmjs.org'
cache: 'npm'
cache-dependency-path: './common/config/rush/pnpm-lock.yaml'
- name: Update npm
run: npm install -g npm@11.18.0
- name: Install latest pnpm
run: npm install -g pnpm@10.7.0
- name: Install Python distutils (macOS)
if: runner.os == 'macOS'
run: |
python3 -m pip install setuptools --break-system-packages
- name: Install Python distutils (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y python3-distutils
python3 -m pip install setuptools --break-system-packages
- name: Install native deps for node-canvas (macOS)
if: runner.os == 'macOS'
run: |
brew update
brew install pkg-config cairo pango libpng jpeg giflib librsvg
- name: Install native deps for node-canvas (Linux)
if: runner.os == 'Linux'
run: |
sudo apt-get update
sudo apt-get install -y build-essential libcairo2-dev libpango1.0-dev libjpeg-dev libgif-dev librsvg2-dev pkg-config
- name: Install rush
run: node common/scripts/install-run-rush.js install --bypass-policy
# ===== Release flow (stable) =====
- name: Parse semver (release)
if: startsWith(github.ref_name, 'release/')
id: semver_release
uses: xile611/read-package-version-action@main
with:
path: packages/vtable
semver_string: ${{ github.ref_name }}
semver_pattern: '^release/(.*)$'
- name: Check version-policy version (release)
if: startsWith(github.ref_name, 'release/')
id: release_version_policy
env:
RELEASE_VERSION: ${{ steps.semver_release.outputs.full }}
run: |
node <<'NODE'
const fs = require('fs');
const policies = JSON.parse(fs.readFileSync('common/config/rush/version-policies.json', 'utf8'));
const policyVersion = policies[0] && policies[0].version;
const releaseVersion = process.env.RELEASE_VERSION;
const parseVersion = version => String(version).split('-')[0].split('.').map(Number);
const compareVersion = (a, b) => {
const av = parseVersion(a);
const bv = parseVersion(b);
for (let i = 0; i < Math.max(av.length, bv.length); i++) {
const diff = (av[i] || 0) - (bv[i] || 0);
if (diff !== 0) {
return diff;
}
}
return 0;
};
if (!releaseVersion || !policyVersion) {
console.error('Missing release version or version-policy version.', { releaseVersion, policyVersion });
process.exit(1);
}
const result = compareVersion(releaseVersion, policyVersion);
if (result < 0) {
console.error(
`Release version ${releaseVersion} is lower than version-policy version ${policyVersion}.`
);
process.exit(1);
}
const shouldSkip = result === 0 ? 'true' : 'false';
fs.appendFileSync(process.env.GITHUB_OUTPUT, `skip_next_bump=${shouldSkip}\n`);
console.log(
result === 0
? `Release version ${releaseVersion} equals version-policy version ${policyVersion}, skip nextBump update.`
: `Release version ${releaseVersion} is greater than version-policy version ${policyVersion}, update nextBump.`
);
NODE
- name: update nextBump (release)
if: startsWith(github.ref_name, 'release/') && steps.release_version_policy.outputs.skip_next_bump != 'true'
uses: xile611/set-next-bump-of-rush@main
with:
release_version: ${{ steps.semver_release.outputs.full }}
write_next_bump: true
- name: Generate changelog blocks from changefiles (release)
if: startsWith(github.ref_name, 'release/')
env:
CHANGELOG_API_URL: ${{ secrets.VCHART_CHANGELOG_API_URL }}
CHANGELOG_API_TOKEN: ${{ secrets.VCHART_CHANGELOG_API_TOKEN }}
RELEASE_VERSION: ${{ steps.semver_release.outputs.main }}
run: |
node <<'NODE'
const fs = require('fs');
const path = require('path');
const { execSync } = require('child_process');
const baseDir = path.join(process.cwd(), 'common', 'changes', '@visactor', 'vtable');
let changefiles = [];
try {
const files = fs.readdirSync(baseDir).filter((name) => name.endsWith('.json'));
for (const file of files) {
const fullPath = path.join(baseDir, file);
const json = JSON.parse(fs.readFileSync(fullPath, 'utf8'));
changefiles.push(json);
}
console.log('Collected changefiles:', changefiles.length);
} catch (e) {
console.log('No changefiles found or unable to read, fallback to empty list.', e.message || e);
}
const version = process.env.RELEASE_VERSION;
if (!version) {
console.error('Missing RELEASE_VERSION env.');
process.exit(1);
}
let prevVersion = process.env.PREV_VERSION || '';
try {
if (!prevVersion) {
const out = execSync('git tag --list "v*.*.*" --sort=-v:refname', { encoding: 'utf8' });
const tags = out.split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
if (tags.length > 0) {
const first = tags[0].replace(/^v/i, '');
if (first !== version && !first.startsWith(version + '-')) {
prevVersion = first;
} else if (tags.length > 1) {
prevVersion = tags[1].replace(/^v/i, '');
}
}
}
} catch (e) {
console.log('Failed to detect previous version from tags, continue without it:', e.message || e);
}
const payload = {
version,
prevVersion: prevVersion || undefined,
date: new Date().toISOString().slice(0, 10),
changefiles,
langs: ['en', 'zh'],
template: 'default',
};
const baseUrl = process.env.CHANGELOG_API_URL;
const token = process.env.CHANGELOG_API_TOKEN;
function failChangelog(message) {
console.error(message);
process.exit(1);
}
function validateBlock(block, lang) {
if (!block || /TODO/i.test(block)) {
throw new Error(`Invalid ${lang} changelog block: empty or contains TODO`);
}
}
if (!baseUrl || !token) {
failChangelog('CHANGELOG_API_URL or CHANGELOG_API_TOKEN not configured.');
}
const url = new URL('/api/changelog/vtable/generate', baseUrl);
const body = JSON.stringify(payload);
const isHttps = url.protocol === 'https:';
const httpModule = isHttps ? require('https') : require('http');
const options = {
method: 'POST',
hostname: url.hostname,
port: url.port || (isHttps ? 443 : 80),
path: url.pathname + url.search,
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
'Content-Length': Buffer.byteLength(body),
},
};
const req = httpModule.request(options, (res) => {
let data = '';
res.on('data', (chunk) => (data += chunk));
res.on('end', () => {
if (res.statusCode && res.statusCode >= 200 && res.statusCode < 300) {
try {
const parsed = JSON.parse(data || '{}');
const en = parsed.blocks && parsed.blocks.en;
const zh = parsed.blocks && parsed.blocks.zh;
if (!en || !zh) {
throw new Error('Missing en/zh blocks in response');
}
validateBlock(String(en), 'en');
validateBlock(String(zh), 'zh');
fs.mkdirSync('.changelog', { recursive: true });
fs.writeFileSync('.changelog/en.md', String(en).trimEnd() + '\n', 'utf8');
fs.writeFileSync('.changelog/zh.md', String(zh).trimEnd() + '\n', 'utf8');
console.log('Changelog blocks generated via API. traceId:', parsed.traceId || '(none)');
} catch (e) {
failChangelog(`Failed to parse API response: ${e.message || e}`);
}
} else {
failChangelog(`Changelog API returned non-2xx status: ${res.statusCode} ${data}`);
}
});
});
req.on('error', (err) => {
failChangelog(`Changelog API request failed: ${err.message || err}`);
});
req.write(body);
req.end();
NODE
- name: Prepend changelog blocks into release docs (release)
if: startsWith(github.ref_name, 'release/')
env:
RELEASE_VERSION: ${{ steps.semver_release.outputs.main }}
run: |
set -euo pipefail
mkdir -p docs/assets/changelog/en docs/assets/changelog/zh
for lang in en zh; do
block_file=".changelog/${lang}.md"
target_file="docs/assets/changelog/${lang}/release.md"
if [ -f "$block_file" ]; then
node -e '
const fs = require("fs");
const [blockFile, targetFile, version] = process.argv.slice(1);
const block = fs.readFileSync(blockFile, "utf8").trimEnd() + "\n\n";
const oldContent = fs.existsSync(targetFile) ? fs.readFileSync(targetFile, "utf8") : "";
const heading = `# v${version}`;
let contentWithoutSameVersion = oldContent;
if (oldContent.startsWith(`${heading}\n`) || oldContent.startsWith(`${heading}\r\n`)) {
const nextVersionIndex = oldContent.slice(1).search(/\n# v\d/);
contentWithoutSameVersion = nextVersionIndex >= 0 ? oldContent.slice(nextVersionIndex + 2) : "";
}
fs.writeFileSync(targetFile, block + contentWithoutSameVersion.replace(/^\n+/, ""), "utf8");
' "$block_file" "$target_file" "$RELEASE_VERSION"
else
echo "Missing changelog block for $lang, skip."
fi
done
- name: Reset version-policy base version (release)
if: startsWith(github.ref_name, 'release/')
run: |
node <<'NODE'
const fs = require('fs');
const packageJson = JSON.parse(fs.readFileSync('packages/vtable/package.json', 'utf8'));
const policiesPath = 'common/config/rush/version-policies.json';
const policies = JSON.parse(fs.readFileSync(policiesPath, 'utf8'));
if (!policies[0]) {
throw new Error('Missing version policy.');
}
policies[0].version = packageJson.version;
fs.writeFileSync(policiesPath, JSON.stringify(policies));
NODE
- name: Generate rush version (release)
if: startsWith(github.ref_name, 'release/')
run: node common/scripts/install-run-rush.js version --bump
# Alpha 在构建前更新为目标版本,其他预发布沿用原流程。
- name: Parse Alpha version
if: startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.')
env:
RELEASE_REF: ${{ github.ref_name }}
run: |
set -euo pipefail
version="${RELEASE_REF#pre-release/}"
node common/scripts/apply-release-version.js "${version#*-}" "${version%%-*}"
- name: Build packages
env:
NODE_OPTIONS: '--max_old_space_size=4096'
run: |
set -euo pipefail
package_names=()
while IFS= read -r package_name; do
[ -n "$package_name" ] && package_names+=("$package_name")
done < <(node <<'NODE'
const fs = require('fs');
const path = require('path');
const packagesDir = path.join(process.cwd(), 'packages');
const packageNames = fs
.readdirSync(packagesDir, { withFileTypes: true })
.filter((entry) => entry.isDirectory())
.map((entry) => path.join(packagesDir, entry.name, 'package.json'))
.filter((packageJsonPath) => fs.existsSync(packageJsonPath))
.map((packageJsonPath) => JSON.parse(fs.readFileSync(packageJsonPath, 'utf8')).name)
.filter(Boolean)
.sort();
console.log(packageNames.join('\n'));
NODE
)
build_args=()
for package_name in "${package_names[@]}"; do
build_args+=(--to "$package_name")
done
echo "Build packages: ${package_names[*]}"
node common/scripts/install-run-rush.js build "${build_args[@]}"
- name: Update shrinkwrap
run: node common/scripts/install-run-rush.js update
- name: Freeze first release artifacts
if: startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))
env:
RELEASE_ARTIFACT_DIR: ${{ runner.temp }}/vtable-release
run: |
set -euo pipefail
rm -rf .changelog
find docs/assets/changelog -name '*.bak' -delete
node .github/scripts/release-frozen.cjs prepare
- uses: actions/upload-artifact@v4
if: startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))
with:
name: vtable-release-manifest-${{ github.run_id }}-1
path: ${{ runner.temp }}/vtable-release/manifest.json
if-no-files-found: error
- uses: actions/upload-artifact@v4
if: startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))
with:
name: vtable-release-prepared-${{ github.run_id }}-1
path: |
${{ runner.temp }}/vtable-release/*.tgz
${{ runner.temp }}/vtable-release/prepared.bundle
${{ runner.temp }}/vtable-release/manifest.json
if-no-files-found: error
# ===== Hotfix flow =====
- name: Parse semver (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
id: semver_hotfix
uses: xile611/read-package-version-action@main
with:
path: packages/vtable
semver_string: ${{ github.ref_name }}
semver_pattern: '^hotfix/(.*)$'
- name: update nextBump (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
uses: xile611/set-next-bump-of-rush@main
with:
release_version: ${{ steps.semver_hotfix.outputs.full }}
write_next_bump: true
- name: Generate rush version (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
run: node common/scripts/install-run-rush.js version --bump
- name: Update version (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
run: node common/scripts/apply-release-version.js 'none' ${{ steps.semver_hotfix.outputs.main }}
- name: Publish to npm (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
run: node common/scripts/install-run-rush.js publish --publish --include-all --tag hotfix
- name: Get npm version (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
id: package_version_hotfix
uses: xile611/read-package-version-action@main
with:
path: packages/vtable
- name: Commit & Push changes (hotfix)
if: startsWith(github.ref_name, 'hotfix/')
run: |
set -euo pipefail
if git diff --quiet; then
echo 'No changes to commit for hotfix branch.'
else
git add .
git commit -m "build: hotfix version ${{ steps.package_version_hotfix.outputs.current_version }} [skip ci]" -n
git push --no-verify origin ${{ github.ref_name }}
fi
# ===== Pre-release flow =====
- name: Parse semver (pre-release)
if: startsWith(github.ref_name, 'pre-release/') && !contains(github.ref_name, '-alpha.')
id: semver_prerelease
uses: xile611/read-package-version-action@main
with:
path: packages/vtable
semver_string: ${{ github.ref_name }}
semver_pattern: '^pre-release/(.*)$'
- name: Apply prereleaseName (pre-release)
if: startsWith(github.ref_name, 'pre-release/') && !contains(github.ref_name, '-alpha.')
run: node common/scripts/apply-release-version.js ${{ steps.semver_prerelease.outputs.pre_release_name }} ${{ steps.semver_prerelease.outputs.main }}
- name: Publish to npm (pre-release)
if: startsWith(github.ref_name, 'pre-release/') && !contains(github.ref_name, '-alpha.')
run: node common/scripts/install-run-rush.js publish --publish --include-all --tag ${{ steps.semver_prerelease.outputs.pre_release_type }}
- name: Get npm version (pre-release)
if: startsWith(github.ref_name, 'pre-release/') && !contains(github.ref_name, '-alpha.')
id: package_version_prerelease
uses: xile611/read-package-version-action@main
with:
path: packages/vtable
- name: Commit & Push changes (pre-release)
if: startsWith(github.ref_name, 'pre-release/') && !contains(github.ref_name, '-alpha.')
run: |
set -euo pipefail
if git diff --quiet; then
echo 'No changes to commit for pre-release branch.'
else
git add .
git commit -m "build: prerelease version ${{ steps.package_version_prerelease.outputs.current_version }} [skip ci]" -n
git push --no-verify origin ${{ github.ref_name }}
fi
publish-frozen:
if: startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))
needs: release
runs-on: ubuntu-latest
timeout-minutes: 35
# 重跑作业须由恢复环境的审核者批准,原始 push 仍沿用现有 GitHub App。
environment: ${{ github.run_attempt == 1 && 'vtable-release-push' || 'vtable-release-recovery' }}
permissions:
actions: read
contents: read
id-token: write
steps:
- name: Set frozen artifact directory
run: echo "RELEASE_ARTIFACT_DIR=$RUNNER_TEMP/vtable-release" >> "$GITHUB_ENV"
- uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
persist-credentials: false
- uses: actions/setup-node@v4
with:
node-version: '22.22.2'
registry-url: https://registry.npmjs.org
- run: npm install -g npm@11.18.0
- uses: actions/download-artifact@v4
with:
name: vtable-release-prepared-${{ github.run_id }}-1
path: ${{ env.RELEASE_ARTIFACT_DIR }}
- name: Publish missing frozen tarballs
env:
GH_TOKEN: ${{ github.token }}
run: node .github/scripts/release-frozen.cjs publish
finalize-frozen:
if: startsWith(github.ref_name, 'release/') || (startsWith(github.ref_name, 'pre-release/') && contains(github.ref_name, '-alpha.'))
needs: publish-frozen
runs-on: ubuntu-latest
timeout-minutes: 10
environment: ${{ github.run_attempt == 1 && 'vtable-release-push' || 'vtable-release-recovery' }}
permissions:
actions: read
contents: write
pull-requests: write
steps:
- name: Set frozen artifact directory
run: echo "RELEASE_ARTIFACT_DIR=$RUNNER_TEMP/vtable-release" >> "$GITHUB_ENV"
- uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
- uses: actions/setup-node@v4
with:
node-version: '22.22.2'
- uses: actions/download-artifact@v4
with:
name: vtable-release-prepared-${{ github.run_id }}-1
path: ${{ env.RELEASE_ARTIFACT_DIR }}
- name: Advance original branch and reconcile product PR
env:
GH_TOKEN: ${{ github.token }}
run: node .github/scripts/release-frozen.cjs finalize