-
-
Notifications
You must be signed in to change notification settings - Fork 244
609 lines (534 loc) · 21.5 KB
/
Copy pathrelease.yml
File metadata and controls
609 lines (534 loc) · 21.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
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
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
name: Release
on:
push:
branches:
- master
concurrency:
group: release
cancel-in-progress: false
env:
FORCE_JAVASCRIPT_ACTIONS_TO_NODE24: "true"
permissions:
contents: read
defaults:
run:
shell: bash
jobs:
# ─── Detect changed paths & pending releases ──────────────────────────
detect:
name: detect changed paths
runs-on: ubuntu-latest
outputs:
rust_changed: ${{ steps.detect.outputs.rust_changed }}
ts_changed: ${{ steps.detect.outputs.ts_changed }}
rust_version: ${{ steps.detect.outputs.rust_version }}
crates_needs_publish: ${{ steps.detect.outputs.crates_needs_publish }}
npm_needs_publish: ${{ steps.detect.outputs.npm_needs_publish }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 2
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v5
with:
node-version: "22"
package-manager-cache: false
- name: Sync versions from package.json
working-directory: rust
run: node scripts/sync-version.mjs
- name: Detect changed paths & pending releases
id: detect
working-directory: rust
run: |
changed_files=$(git -C .. diff --name-only HEAD~1 HEAD)
rust_changed=false
ts_changed=false
# ── File-change detection ──
if echo "$changed_files" | grep -q '^rust/'; then
rust_changed=true
fi
ts_files=$(echo "$changed_files" | grep -v '^rust/' || true)
if [ -n "$ts_files" ]; then
ts_changed=true
fi
# ── Registry-based release detection (Rust) ──
# If local version is not yet on crates.io or npm, we need a release
rust_version=$(node -p "require('./package.json').version")
crates_needs_publish=false
npm_needs_publish=false
for toml in crates/*/Cargo.toml; do
name=$(grep '^name = ' "$toml" | head -1 | sed 's/name = "\(.*\)"/\1/')
local_ver=$(grep '^version = ' "$toml" | head -1 | sed 's/version = "\(.*\)"/\1/')
if [ -z "$name" ] || [ -z "$local_ver" ]; then
continue
fi
# Use the crates.io HTTP API for exact version matching.
# cargo info crate@ver exits 0 if the crate exists at ANY version,
# not just the specified one — so we must check the versions list.
published_ver=$(curl -sfH "Accept: application/json" -H "User-Agent: jscpd-ci (github.com/kucherenko/jscpd)" "https://crates.io/api/v1/crates/${name}" 2>/dev/null \
| node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{const c=JSON.parse(d);process.stdout.write(c.crate.max_version)}catch(e){process.stdout.write('')}})" || echo "")
if [ "$published_ver" = "$local_ver" ] && [ -n "$published_ver" ]; then
echo "${name}@${local_ver} already on crates.io (max_version=${published_ver})"
else
echo "crates.io release needed: ${name}@${local_ver} (published=${published_ver:-none})"
crates_needs_publish=true
rust_changed=true
fi
done
# ── npm: publish if rust/package.json version differs from npm registry
npm_published=$(npm info cpd version 2>/dev/null || echo "")
if [ "$npm_published" != "$rust_version" ]; then
echo "npm release needed: cpd published=${npm_published:-none} local=${rust_version}"
npm_needs_publish=true
rust_changed=true
else
echo "cpd@${rust_version} already on npm"
fi
echo "rust_changed=${rust_changed}" >> "$GITHUB_OUTPUT"
echo "ts_changed=${ts_changed}" >> "$GITHUB_OUTPUT"
echo "rust_version=${rust_version}" >> "$GITHUB_OUTPUT"
echo "crates_needs_publish=${crates_needs_publish}" >> "$GITHUB_OUTPUT"
echo "npm_needs_publish=${npm_needs_publish}" >> "$GITHUB_OUTPUT"
echo "Detected: rust_changed=${rust_changed}, ts_changed=${ts_changed}, crates_needs_publish=${crates_needs_publish}, npm_needs_publish=${npm_needs_publish}"
# ─── Rust CI ─────────────────────────────────────────────────────
rust-ci:
name: rust ci (clippy, test, build)
needs: detect
if: needs.detect.outputs.rust_changed == 'true'
runs-on: ubuntu-latest
defaults:
run:
working-directory: rust
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 2
- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
toolchain: "1.93"
components: clippy
- name: Cache Cargo registry and target
uses: Swatinem/rust-cache@v2
with:
workspaces: "rust -> rust/target"
cache-on-failure: true
- name: Clippy
run: cargo clippy --workspace -- -D warnings
- name: Test
run: cargo test --workspace --locked
- name: Build
run: cargo build --release --workspace --locked
# ─── TypeScript CI ──────────────────────────────────────────────
ts-ci:
name: typescript ci (build, lint, test)
needs: detect
if: needs.detect.outputs.ts_changed == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
checks: write
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 2
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v5
with:
node-version: "lts/*"
package-manager-cache: false
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm build
- name: Lint
run: pnpm lint
- name: Test
run: pnpm test -- --reporter=default --reporter=junit --outputFile=test-results/junit.xml
- name: Upload test results
if: always()
uses: actions/upload-artifact@v4
with:
name: test-results-release-ts
path: "**/test-results/junit.xml"
if-no-files-found: warn
- name: Test Report
if: always()
uses: dorny/test-reporter@v1
with:
name: Node.js Tests (release)
path: "**/test-results/junit.xml"
reporter: java-junit
fail-on-error: false
# ─── Rust version bump detection ─────────────────────────────────
rust-version:
name: detect rust version bump
needs: [detect, rust-ci]
if: needs.detect.outputs.rust_changed == 'true' && needs.rust-ci.result == 'success'
runs-on: ubuntu-latest
outputs:
version: ${{ needs.detect.outputs.rust_version }}
crates_bumped: ${{ needs.detect.outputs.crates_needs_publish }}
npm_bumped: ${{ needs.detect.outputs.npm_needs_publish }}
steps:
- name: Confirm
run: |
echo "Releasing Rust ${{ needs.detect.outputs.rust_version }}"
echo "crates_needs_publish=${{ needs.detect.outputs.crates_needs_publish }}"
echo "npm_needs_publish=${{ needs.detect.outputs.npm_needs_publish }}"
# ─── TypeScript version bump detection ───────────────────────────
ts-version:
name: detect typescript version bump
needs: [detect, ts-ci]
if: needs.detect.outputs.ts_changed == 'true' && needs.ts-ci.result == 'success'
runs-on: ubuntu-latest
outputs:
version: ${{ steps.version.outputs.version }}
bumped: ${{ steps.version.outputs.bumped }}
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 2
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v5
with:
node-version: "22"
package-manager-cache: false
- name: Detect version bump
id: version
run: |
version=$(node -p "require('./package.json').version")
bumped=false
# Check root package.json
old_root=$(git show "HEAD~1:package.json" 2>/dev/null | node -e "
let d=''; process.stdin.on('data',c=>d+=c); process.stdin.on('end',()=>{
try{ process.stdout.write(JSON.parse(d).version) }catch(e){ process.exit(0) }
})" || true)
new_root=$(node -p "require('./package.json').version")
if [ -n "$old_root" ] && [ "$old_root" != "$new_root" ]; then
echo "Version bump in package.json: $old_root -> $new_root"
bumped=true
fi
# Check apps/*/package.json
for pkg in apps/*/package.json; do
old_v=$(git show "HEAD~1:$pkg" 2>/dev/null | node -e "
let d=''; process.stdin.on('data',c=>d+=c); process.stdin.on('end',()=>{
try{ process.stdout.write(JSON.parse(d).version) }catch(e){ process.exit(0) }
})" || true)
new_v=$(node -p "require('./$pkg').version")
if [ -n "$old_v" ] && [ "$old_v" != "$new_v" ]; then
echo "Version bump in $pkg: $old_v -> $new_v"
bumped=true
fi
done
# Check packages/*/package.json (skip private packages)
for pkg in packages/*/package.json; do
is_private=$(node -p "require('./$pkg').private || false" 2>/dev/null || echo "true")
if [ "$is_private" = "true" ]; then
continue
fi
old_v=$(git show "HEAD~1:$pkg" 2>/dev/null | node -e "
let d=''; process.stdin.on('data',c=>d+=c); process.stdin.on('end',()=>{
try{ process.stdout.write(JSON.parse(d).version) }catch(e){ process.exit(0) }
})" || true)
new_v=$(node -p "require('./$pkg').version")
if [ -n "$old_v" ] && [ "$old_v" != "$new_v" ]; then
echo "Version bump in $pkg: $old_v -> $new_v"
bumped=true
fi
done
echo "version=$version" >> "$GITHUB_OUTPUT"
echo "bumped=$bumped" >> "$GITHUB_OUTPUT"
# ─── Rust publish ────────────────────────────────────────────────
rust-publish-crates:
name: publish crates.io packages
needs: rust-version
if: needs.rust-version.outputs.crates_bumped == 'true'
uses: ./.github/workflows/crates-publish.yml
with:
version: ${{ needs.rust-version.outputs.version }}
secrets: inherit
permissions:
contents: read
id-token: write
checks: write
rust-publish-npm:
name: publish npm rust packages
needs: rust-version
if: needs.rust-version.outputs.npm_bumped == 'true'
uses: ./.github/workflows/npm-rust-publish.yml
with:
version: ${{ needs.rust-version.outputs.version }}
secrets: inherit
permissions:
contents: read
id-token: write
checks: write
# ─── TypeScript publish ──────────────────────────────────────────
ts-publish:
name: publish npm packages
needs: ts-version
if: needs.ts-version.outputs.bumped == 'true'
runs-on: ubuntu-latest
permissions:
contents: read
id-token: write
steps:
- name: Checkout
uses: actions/checkout@v5
with:
fetch-depth: 0
- name: Install pnpm
uses: pnpm/action-setup@v4
- name: Setup Node.js
uses: actions/setup-node@v5
with:
node-version: "lts/*"
registry-url: "https://registry.npmjs.org"
package-manager-cache: false
- name: Install dependencies
run: pnpm install
- name: Build
run: pnpm build
- name: Publish packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
run: pnpm publish-packages
# ─── GitHub releases ─────────────────────────────────────────────
rust-release:
name: create rust github release
needs: [rust-version, rust-publish-crates, rust-publish-npm]
if: always() && !cancelled() &&
(needs.rust-version.outputs.crates_bumped == 'true' || needs.rust-version.outputs.npm_bumped == 'true') &&
needs.rust-publish-crates.result != 'failure' &&
needs.rust-publish-npm.result != 'failure'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Download binary artifacts
uses: actions/download-artifact@v4
with:
pattern: jscpd-binary-*
path: release-assets
merge-multiple: true
continue-on-error: true
- name: Resolve published packages
id: packages
working-directory: rust
run: |
version="${{ needs.rust-version.outputs.version }}"
published=""
failed=""
# ── crates.io packages ──
for toml in crates/*/Cargo.toml; do
name=$(grep '^name = ' "$toml" | head -1 | sed 's/name = "\(.*\)"/\1/')
local_ver=$(grep '^version = ' "$toml" | head -1 | sed 's/version = "\(.*\)"/\1/')
if [ -z "$name" ] || [ -z "$local_ver" ]; then
continue
fi
max_ver=$(curl -sfH "Accept: application/json" -H "User-Agent: jscpd-ci (github.com/kucherenko/jscpd)" "https://crates.io/api/v1/crates/${name}" 2>/dev/null \
| node -e "let d='';process.stdin.on('data',c=>d+=c);process.stdin.on('end',()=>{try{const c=JSON.parse(d);process.stdout.write(c.crate.max_version)}catch(e){process.stdout.write('')}})" || echo "")
if [ "$max_ver" = "$local_ver" ]; then
published="${published} - \`${name}@${local_ver}\` on crates.io\n"
else
failed="${failed} - \`${name}@${local_ver}\` (published: ${max_ver:-none})\n"
fi
done
# ── npm packages ──
npm_pkgs=(cpd jscpd jscpd-darwin-arm64 jscpd-darwin-x64 jscpd-linux-x64-gnu jscpd-linux-arm64-gnu jscpd-linux-x64-musl jscpd-windows-x64-msvc)
for pkg in "${npm_pkgs[@]}"; do
pub_ver=$(npm view "${pkg}@${version}" version 2>/dev/null || echo "")
if [ "$pub_ver" = "$version" ]; then
published="${published} - \`${pkg}@${version}\` on npm\n"
else
# Platform packages may have different versions
latest_ver=$(npm view "${pkg}" version 2>/dev/null || echo "")
failed="${failed} - \`${pkg}@${version}\` (published: ${latest_ver:-none})\n"
fi
done
{
echo "published<<EOF"
echo -e "$published"
echo "EOF"
echo "failed<<EOF"
echo -e "$failed"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Extract release notes
id: notes
working-directory: rust
run: |
version="${{ needs.rust-version.outputs.version }}"
notes=$(node -e "
const fs = require('fs');
const ver = process.argv[1];
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
const escaped = ver.replace(/\./g, '\\\\.');
const re = new RegExp('^## .*' + escaped);
const lines = changelog.split('\n');
let found = false;
const out = [];
for (const line of lines) {
if (!found && re.test(line)) { found = true; continue; }
if (found && /^## /.test(line)) break;
if (found && /^---$/.test(line)) break;
if (found) out.push(line);
}
process.stdout.write(out.join('\n'));
" "$version")
if [ -z "$notes" ]; then
notes="Release v${version}"
fi
# ── Append published packages info ──
# Use single-quoted assignments so markdown backticks in package
# names are not interpreted as command substitution.
published='${{ steps.packages.outputs.published }}'
failed='${{ steps.packages.outputs.failed }}'
if [ -n "$published" ]; then
notes="${notes}
## Published Packages
${published}"
fi
if [ -n "$failed" ]; then
notes="${notes}
## Not Yet Published
${failed}"
fi
{
echo "notes<<EOF"
echo "$notes"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_NOTES: ${{ steps.notes.outputs.notes }}
run: |
version="${{ needs.rust-version.outputs.version }}"
tag="v${version}"
# Collect binary assets if they exist
assets=""
if [ -d release-assets ] && ls release-assets/*.tar.gz >/dev/null 2>&1; then
for f in release-assets/*.tar.gz; do
assets="$assets $f"
done
fi
if [ -n "$assets" ]; then
gh release create "${tag}" \
--title "Release v${version}" \
--notes "$RELEASE_NOTES" \
$assets
else
gh release create "${tag}" \
--title "Release v${version}" \
--notes "$RELEASE_NOTES"
fi
ts-release:
name: create typescript github release
needs: [ts-version, ts-publish]
if: needs.ts-version.outputs.bumped == 'true' && needs.ts-publish.result == 'success'
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout
uses: actions/checkout@v5
- name: Resolve published npm packages
id: packages
run: |
version="${{ needs.ts-version.outputs.version }}"
published=""
failed=""
# ── TypeScript workspace packages ──
for pkg_dir in apps/*/ packages/*/; do
pkg_json="${pkg_dir}package.json"
[ -f "$pkg_json" ] || continue
is_private=$(node -p "require('./${pkg_json}').private || false" 2>/dev/null || echo "true")
[ "$is_private" = "true" ] && continue
name=$(node -p "require('./${pkg_json}').name" 2>/dev/null || continue)
local_ver=$(node -p "require('./${pkg_json}').version" 2>/dev/null || continue)
pub_ver=$(npm view "${name}@${local_ver}" version 2>/dev/null || echo "")
if [ "$pub_ver" = "$local_ver" ]; then
published="${published} - \`${name}@${local_ver}\` on npm\n"
else
latest_ver=$(npm view "${name}" version 2>/dev/null || echo "")
failed="${failed} - \`${name}@${local_ver}\` (published: ${latest_ver:-none})\n"
fi
done
{
echo "published<<EOF"
echo -e "$published"
echo "EOF"
echo "failed<<EOF"
echo -e "$failed"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Extract release notes
id: notes
run: |
version="${{ needs.ts-version.outputs.version }}"
notes=$(node -e "
const fs = require('fs');
const ver = process.argv[1];
const changelog = fs.readFileSync('CHANGELOG.md', 'utf8');
const escaped = ver.replace(/\./g, '\\\\.');
const re = new RegExp('^## .*' + escaped);
const lines = changelog.split('\n');
let found = false;
const out = [];
for (const line of lines) {
if (!found && re.test(line)) { found = true; continue; }
if (found && /^## /.test(line)) break;
if (found && /^---$/.test(line)) break;
if (found) out.push(line);
}
process.stdout.write(out.join('\n'));
" "$version")
if [ -z "$notes" ]; then
notes="Release v${version}"
fi
# ── Append published packages info ──
# Use single-quoted assignments so markdown backticks in package
# names are not interpreted as command substitution.
published='${{ steps.packages.outputs.published }}'
failed='${{ steps.packages.outputs.failed }}'
if [ -n "$published" ]; then
notes="${notes}
## Published Packages
${published}"
fi
if [ -n "$failed" ]; then
notes="${notes}
## Not Yet Published
${failed}"
fi
{
echo "notes<<EOF"
echo "$notes"
echo "EOF"
} >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
RELEASE_NOTES: ${{ steps.notes.outputs.notes }}
run: |
version="${{ needs.ts-version.outputs.version }}"
tag="v${version}"
gh release create "${tag}" \
--title "Release v${version}" \
--notes "$RELEASE_NOTES"