-
Notifications
You must be signed in to change notification settings - Fork 11
1600 lines (1356 loc) · 67.2 KB
/
Copy pathpublish-packages.yml
File metadata and controls
1600 lines (1356 loc) · 67.2 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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
name: Publish Packages
# Unified publishing workflow that handles both CI (automatic) and force (manual) publishing.
# This consolidation is required due to npm OIDC trusted publishing constraints - the workflow_ref
# in the OIDC token must match the configured trusted workflow.
#
# MODES:
# - Auto (CI): Triggered on push to main/next, detects affected packages, publishes, generates
# changelog, sends notifications, and syncs branches.
# - Force: Manually triggered, publishes user-specified packages with prerelease versions.
#
# TRIGGERS:
# - Push to main: Auto mode with 'latest' npm tag, conventional versioning
# - Push to next: Auto mode with 'next' npm tag, prerelease versioning
# - workflow_dispatch: Force mode with 'next' npm tag, prerelease versioning
#
# WORKFLOW CHANGE DETECTION:
# In addition to package detection, this workflow also detects changes to reusable workflow
# files (files prefixed with '_' in .github/workflows/). When workflow files change:
# - The 'next' branch is synced with 'main' to propagate workflow updates
# - A Slack notification is sent about the workflow changes
# - These actions happen even when no npm packages need to be published
#
# REQUIRED SECRETS:
# - WORKFLOW_PAT: For pushing commits/tags
# - NODE_AUTH_TOKEN: For npm publishing
# - SERVICE_ACCOUNT_GPG_PRIVATE_KEY: For signing commits/tags
# - ANTHROPIC_API_KEY: For AI-powered changelog generation (auto mode only)
# - SLACK_WEBHOOK_URL: For release and error notifications
# - NOTION_API_KEY: For Notion publishing (auto mode only)
# - RELEASE_NOTES_NOTION_DATABASE_ID: For Notion database (auto mode only)
on:
push:
branches:
- main
- next
workflow_dispatch:
inputs:
packages:
description: 'Packages to publish (single name, comma-separated list, or "all")'
required: true
default: "all"
type: string
dryRun:
description: "Perform a dry run without publishing"
required: false
default: "false"
type: choice
options:
- "true"
- "false"
permissions: {}
# Ensure only one publish workflow runs at a time per branch
concurrency:
group: publish-${{ github.ref }}
cancel-in-progress: false
# Disable Bun's automatic bunfig.toml loading from CWD. `bun install` and
# `bun run` invocations in this workflow run after checkout; without this,
# a malicious bunfig.toml at the checkout root would have its preload array
# execute arbitrary code before the intended script (RCE with publish-token
# scope in this workflow).
env:
BUN_CONFIG_FILE: /dev/null
jobs:
# ============================================================================
# DETECT: Determine which packages to publish
# ============================================================================
# For push events: Uses Nx affected detection
# For workflow_dispatch: Uses user-specified packages (force mode)
# ============================================================================
detect:
name: Detect packages to publish
runs-on: ubuntu-24.04
permissions:
contents: read
environment: ${{ github.event_name == 'push' && 'Production' || null }}
outputs:
has_packages: ${{ steps.resolve.outputs.has_packages }}
projects: ${{ steps.resolve.outputs.projects }}
packages: ${{ steps.resolve.outputs.packages }}
package_count: ${{ steps.resolve.outputs.package_count }}
base_sha: ${{ steps.base-sha.outputs.base }}
npm_tag: ${{ steps.config.outputs.npm_tag }}
version_strategy: ${{ steps.config.outputs.version_strategy }}
is_dry_run: ${{ steps.config.outputs.is_dry_run }}
is_force_mode: ${{ steps.config.outputs.is_force_mode }}
is_prerelease: ${{ steps.config.outputs.is_prerelease }}
has_workflow_changes: ${{ steps.workflow-changes.outputs.has_changes }}
changed_workflows: ${{ steps.workflow-changes.outputs.changed_files }}
should_continue: ${{ steps.resolve.outputs.has_packages == 'true' || steps.workflow-changes.outputs.has_changes == 'true' }}
# Skip workflow for version bump commits and branch sync commits (prevents loops)
if: ${{ !startsWith(github.event.head_commit.message, 'chore(release):') && !startsWith(github.event.head_commit.message, 'chore(sync):') }}
steps:
- uses: bullfrogsec/bullfrog@1831f79cce8ad602eef14d2163873f27081ebfb3 # v0.8.4
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
persist-credentials: false
fetch-depth: 0
- name: Setup Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: ${{ vars.NODE_VERSION }}
- name: Setup Bun
uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: "1.3.12"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Determine workflow configuration
id: config
env:
EVENT_NAME: ${{ github.event_name }}
BRANCH_NAME: ${{ github.ref_name }}
DRY_RUN_INPUT: ${{ github.event.inputs.dryRun }}
run: |
# Determine if this is force mode (manual dispatch) or auto mode (push)
if [[ "$EVENT_NAME" == "workflow_dispatch" ]]; then
echo "is_force_mode=true" >> $GITHUB_OUTPUT
echo "📦 Mode: Force publish (manual trigger)"
# Force mode always uses next tag and prerelease versioning
echo "npm_tag=next" >> $GITHUB_OUTPUT
echo "version_strategy=prerelease" >> $GITHUB_OUTPUT
echo "is_prerelease=true" >> $GITHUB_OUTPUT
# Dry run based on user input
if [[ "$DRY_RUN_INPUT" == "true" ]]; then
echo "is_dry_run=true" >> $GITHUB_OUTPUT
echo "🔍 Dry run mode enabled"
else
echo "is_dry_run=false" >> $GITHUB_OUTPUT
fi
else
echo "is_force_mode=false" >> $GITHUB_OUTPUT
echo "🚀 Mode: Auto publish (CI trigger)"
echo "is_dry_run=false" >> $GITHUB_OUTPUT
# Auto mode configuration based on branch
if [[ "$BRANCH_NAME" == "next" ]]; then
echo "npm_tag=next" >> $GITHUB_OUTPUT
echo "version_strategy=prerelease" >> $GITHUB_OUTPUT
echo "is_prerelease=true" >> $GITHUB_OUTPUT
echo "📦 Branch: next → npm tag: next, prerelease versioning"
else
echo "npm_tag=latest" >> $GITHUB_OUTPUT
echo "version_strategy=conventional" >> $GITHUB_OUTPUT
echo "is_prerelease=false" >> $GITHUB_OUTPUT
echo "📦 Branch: main → npm tag: latest, conventional versioning"
fi
fi
- name: Validate branch (force mode only)
if: github.event_name == 'workflow_dispatch'
env:
BRANCH_NAME: ${{ github.ref_name }}
run: |
if [[ "$BRANCH_NAME" != "next" ]]; then
echo "❌ ERROR: Force publish is only allowed on the 'next' branch"
echo "Current branch: $BRANCH_NAME"
echo "This workflow publishes with the 'next' npm tag and should only run on the next branch."
exit 1
fi
echo "✅ Branch validation passed - running on 'next' branch"
- name: Determine base SHA for affected detection
id: base-sha
if: github.event_name == 'push'
env:
BEFORE_SHA: ${{ github.event.before }}
run: |
BASE_SHA="$BEFORE_SHA"
# Handle edge case: first push to a new branch (null SHA)
if [[ "$BASE_SHA" == "0000000000000000000000000000000000000000" ]]; then
echo "First push to branch - using HEAD~1 as base"
echo "base=HEAD~1" >> $GITHUB_OUTPUT
else
echo "Using commit before push: $BASE_SHA"
echo "base=$BASE_SHA" >> $GITHUB_OUTPUT
fi
- name: Detect reusable workflow changes
id: workflow-changes
if: github.event_name == 'push'
env:
BASE_SHA: ${{ steps.base-sha.outputs.base }}
run: |
echo "🔍 Checking for reusable workflow changes..."
echo "Base SHA: $BASE_SHA"
echo "Head SHA: HEAD"
# Get list of changed files matching the reusable workflow pattern
CHANGED_WORKFLOWS=$(git diff --name-only "$BASE_SHA" HEAD -- '.github/workflows/_*.yml' 2>/dev/null || echo "")
if [ -n "$CHANGED_WORKFLOWS" ]; then
echo "✅ Found reusable workflow changes:"
echo "$CHANGED_WORKFLOWS" | while read -r file; do
echo " - $file"
done
# Output as JSON array for downstream consumption
CHANGED_JSON=$(echo "$CHANGED_WORKFLOWS" | jq -R . | jq -s -c .)
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "changed_files=$CHANGED_JSON" >> $GITHUB_OUTPUT
echo "change_count=$(echo "$CHANGED_WORKFLOWS" | wc -l | tr -d ' ')" >> $GITHUB_OUTPUT
else
echo "ℹ️ No reusable workflow changes detected"
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "changed_files=[]" >> $GITHUB_OUTPUT
echo "change_count=0" >> $GITHUB_OUTPUT
fi
- name: Resolve packages to publish
id: resolve
env:
EVENT_NAME: ${{ github.event_name }}
INPUT_PACKAGES: ${{ github.event.inputs.packages }}
BASE_SHA: ${{ steps.base-sha.outputs.base }}
run: |
# Get all release-configured packages from nx.json
echo "Scanning for release-configured packages..."
ALL_PACKAGES=$(bunx nx show projects --json 2>/dev/null | jq -r '.[]' | while read project; do
PROJECT_ROOT=$(bunx nx show project "$project" --json 2>/dev/null | jq -r '.root' || echo "")
if [ -n "$PROJECT_ROOT" ] && [ -f "$PROJECT_ROOT/package.json" ]; then
IS_PRIVATE=$(jq -r '.private // false' "$PROJECT_ROOT/package.json")
if [ "$IS_PRIVATE" != "true" ]; then
PACKAGE_NAME=$(jq -r '.name // ""' "$PROJECT_ROOT/package.json")
if [ -n "$PACKAGE_NAME" ]; then
# Check if this package is in the nx.json release.projects array
if jq -e --arg pkg "$PACKAGE_NAME" '.release.projects | index($pkg)' nx.json > /dev/null 2>&1; then
echo "$project:$PACKAGE_NAME"
fi
fi
fi
fi
done)
if [[ "$EVENT_NAME" == "workflow_dispatch" ]]; then
# FORCE MODE: Use user-specified packages
echo "🔧 Force mode: resolving user-specified packages"
echo "Input packages: \"$INPUT_PACKAGES\""
if [[ "$INPUT_PACKAGES" == "all" ]]; then
# Use all release-configured packages
SELECTED_PROJECTS=$(echo "$ALL_PACKAGES" | cut -d: -f1 | tr '\n' ',' | sed 's/,$//')
SELECTED_PACKAGES=$(echo "$ALL_PACKAGES" | cut -d: -f2 | tr '\n' ',' | sed 's/,$//')
else
# Parse comma-separated input and match to projects
SELECTED_PROJECTS=""
SELECTED_PACKAGES=""
IFS=',' read -ra INPUT_PKG_ARRAY <<< "$INPUT_PACKAGES"
for input_pkg in "${INPUT_PKG_ARRAY[@]}"; do
# Trim whitespace
input_pkg=$(echo "$input_pkg" | xargs)
# Find the matching project
MATCH=$(echo "$ALL_PACKAGES" | grep ":${input_pkg}$" || echo "")
if [ -z "$MATCH" ]; then
echo "❌ ERROR: Package '$input_pkg' not found in release configuration"
echo "Available packages:"
echo "$ALL_PACKAGES" | cut -d: -f2
exit 1
fi
PROJECT=$(echo "$MATCH" | cut -d: -f1)
PACKAGE=$(echo "$MATCH" | cut -d: -f2)
if [ -n "$SELECTED_PROJECTS" ]; then
SELECTED_PROJECTS="${SELECTED_PROJECTS},${PROJECT}"
SELECTED_PACKAGES="${SELECTED_PACKAGES},${PACKAGE}"
else
SELECTED_PROJECTS="$PROJECT"
SELECTED_PACKAGES="$PACKAGE"
fi
done
fi
else
# AUTO MODE: Use Nx affected detection
echo "🔍 Auto mode: detecting affected packages"
echo "Running: bunx nx show projects --affected --base=$BASE_SHA --head=HEAD --type=lib --json"
# Capture stdout (JSON) and stderr (warnings/banners) separately so any
# extra noise on stderr can't contaminate the JSON parse below.
# The `|| NX_EXIT=$?` form is required because GitHub Actions runs
# bash with `-e`; a bare assignment would abort the step before
# `NX_EXIT=$?` could capture the failure code.
NX_STDERR_LOG="$(mktemp)"
NX_EXIT=0
RAW_OUTPUT=$(bunx nx show projects --affected --base="$BASE_SHA" --head=HEAD --type=lib --json 2>"$NX_STDERR_LOG") || NX_EXIT=$?
if [ $NX_EXIT -ne 0 ]; then
echo "::error::nx show projects failed (exit $NX_EXIT)"
echo "stderr:"
cat "$NX_STDERR_LOG"
rm -f "$NX_STDERR_LOG"
exit 1
fi
if echo "$RAW_OUTPUT" | jq empty 2>/dev/null; then
AFFECTED_JSON="$RAW_OUTPUT"
else
echo "::error::nx returned non-JSON stdout — refusing to fall back to an empty release matrix"
echo "stdout:"
echo "$RAW_OUTPUT"
echo "stderr:"
cat "$NX_STDERR_LOG"
rm -f "$NX_STDERR_LOG"
exit 1
fi
rm -f "$NX_STDERR_LOG"
# Filter to only publishable packages
SELECTED_PROJECTS=""
SELECTED_PACKAGES=""
for project in $(echo "$AFFECTED_JSON" | jq -r '.[]'); do
PROJECT_ROOT=$(bunx nx show project "$project" --json 2>/dev/null | jq -r '.root' || echo "")
if [ -n "$PROJECT_ROOT" ] && [ -f "$PROJECT_ROOT/package.json" ]; then
IS_PRIVATE=$(jq -r '.private // false' "$PROJECT_ROOT/package.json")
if [ "$IS_PRIVATE" = "true" ]; then
echo " ⏭️ Skipping $project (private: true)"
else
PACKAGE_NAME=$(jq -r '.name // ""' "$PROJECT_ROOT/package.json")
if [ -n "$PACKAGE_NAME" ]; then
echo " ✅ Including $project → $PACKAGE_NAME (publishable)"
if [ -n "$SELECTED_PROJECTS" ]; then
SELECTED_PROJECTS="${SELECTED_PROJECTS},${project}"
SELECTED_PACKAGES="${SELECTED_PACKAGES},${PACKAGE_NAME}"
else
SELECTED_PROJECTS="$project"
SELECTED_PACKAGES="$PACKAGE_NAME"
fi
fi
fi
fi
done
fi
echo "Selected Nx projects: $SELECTED_PROJECTS"
echo "Selected npm packages: $SELECTED_PACKAGES"
# Count packages
if [ -z "$SELECTED_PACKAGES" ]; then
PACKAGE_COUNT=0
else
PACKAGE_COUNT=$(echo "$SELECTED_PACKAGES" | tr ',' '\n' | grep -c '.' || echo "0")
fi
echo "projects=$SELECTED_PROJECTS" >> $GITHUB_OUTPUT
echo "packages=$SELECTED_PACKAGES" >> $GITHUB_OUTPUT
echo "package_count=$PACKAGE_COUNT" >> $GITHUB_OUTPUT
if [ "$PACKAGE_COUNT" -eq "0" ]; then
echo "has_packages=false" >> $GITHUB_OUTPUT
echo "ℹ️ No packages to publish"
else
echo "has_packages=true" >> $GITHUB_OUTPUT
echo "✅ Found $PACKAGE_COUNT package(s) to publish"
fi
# ============================================================================
# PUBLISH: Build, version, and publish packages to npm
# ============================================================================
publish:
name: Build and publish packages
runs-on:
group: npm-deploy
environment: Production
needs: detect
if: needs.detect.outputs.has_packages == 'true'
permissions:
id-token: write
contents: write
packages: write
pull-requests: write
issues: write
outputs:
successful_packages: ${{ steps.publish.outputs.successful_packages }}
failed_packages: ${{ steps.publish.outputs.failed_packages }}
successful_count: ${{ steps.publish.outputs.successful_count }}
failed_count: ${{ steps.publish.outputs.failed_count }}
has_failures: ${{ steps.publish.outputs.has_failures }}
has_successes: ${{ steps.publish.outputs.has_successes }}
steps:
- uses: bullfrogsec/bullfrog@1831f79cce8ad602eef14d2163873f27081ebfb3 # v0.8.4
- name: Import GPG key
uses: crazy-max/ghaction-import-gpg@e89d40939c28e39f97cf32126055eeae86ba74ec # v6
with:
gpg_private_key: ${{ secrets.SERVICE_ACCOUNT_GPG_PRIVATE_KEY }}
git_user_signingkey: true
git_commit_gpgsign: true
git_tag_gpgsign: true
git_config_global: true
- name: Checkout repository
uses: actions/checkout@de0fac2e4500dabe0009e67214ff5f5447ce83dd # v6.0.2
with:
fetch-depth: 0
token: ${{ secrets.WORKFLOW_PAT }}
persist-credentials: false
- name: Configure git credentials for push
env:
GITHUB_TOKEN: ${{ secrets.WORKFLOW_PAT }}
GITHUB_REPOSITORY: ${{ github.repository }}
run: git remote set-url origin "https://x-access-token:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
- name: Setup Node.js
uses: actions/setup-node@53b83947a5a98c8d113130e565377fae1a50d02f # v6.3.0
with:
node-version: ${{ vars.NODE_VERSION }}
registry-url: "https://registry.npmjs.org"
scope: "@uniswap"
- name: Setup Bun
uses: oven-sh/setup-bun@0c5077e51419868618aeaa5fe8019c62421857d6 # v2.2.0
with:
bun-version: "1.3.12"
- name: Install npm (for OIDC publishing only)
env:
NPM_VERSION: ${{ vars.NPM_VERSION }}
run: npm install -g npm@$NPM_VERSION
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Verify bun.lock unchanged
run: |
if ! git diff --exit-code bun.lock; then
echo "❌ ERROR: bun install modified bun.lock!"
echo "This should not happen. Please investigate why bun install is modifying the lock file."
git diff bun.lock
exit 1
fi
echo "✅ bun.lock unchanged after install"
- name: Configure Git identity for service account
run: |
git config user.name "Uniswap Labs Service Account"
git config user.email "hello-happy-puppy@uniswap.org"
- name: Verify GPG configuration
run: |
echo "GPG signing enabled: $(git config commit.gpgsign)"
echo "GPG signing key: $(git config user.signingkey)"
echo "Git user name: $(git config user.name)"
echo "Git user email: $(git config user.email)"
- name: Build packages
env:
INPUT_PROJECTS: ${{ needs.detect.outputs.projects }}
INPUT_BASE_SHA: ${{ needs.detect.outputs.base_sha }}
run: |
echo "Building packages: $INPUT_PROJECTS"
if [ -n "$INPUT_BASE_SHA" ]; then
echo "Using affected build with base: $INPUT_BASE_SHA"
bunx nx affected --target=build --base="$INPUT_BASE_SHA" --head=HEAD
else
IFS=',' read -ra PROJECT_ARRAY <<< "$INPUT_PROJECTS"
for project in "${PROJECT_ARRAY[@]}"; do
echo "Building $project..."
bunx nx build "$project"
done
fi
- name: Clean up orphaned tags (auto mode only)
if: github.event_name == 'push' && needs.detect.outputs.is_dry_run != 'true'
env:
INPUT_PACKAGES: ${{ needs.detect.outputs.packages }}
GITHUB_TOKEN: ${{ secrets.WORKFLOW_PAT }}
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
INPUT_BRANCH: ${{ github.ref_name }}
INPUT_NPM_TAG: ${{ needs.detect.outputs.npm_tag }}
run: |
# Configure npm authentication for viewing restricted packages
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > ~/.npmrc
echo "=========================================="
echo "Checking for orphaned Git tags"
echo "=========================================="
IFS=',' read -ra PACKAGES <<< "$INPUT_PACKAGES"
CLEANED_TAGS=()
for package in "${PACKAGES[@]}"; do
if [ -z "$package" ]; then continue; fi
echo "Checking package: $package"
PACKAGE_NAME=$(echo "$package" | sed 's/@[^/]*\///')
# Find highest stable tag (sort -V doesn't handle semver prereleases correctly)
# Filter to stable versions: @X.Y.Z (no hyphen after version = no prerelease suffix)
LATEST_TAG=$(git tag -l "${PACKAGE_NAME}@*" "${package}@*" 2>/dev/null | grep -E '@[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1)
if [ -z "$LATEST_TAG" ]; then
echo " ℹ️ No stable tags found - skipping orphaned check"
continue
fi
VERSION="${LATEST_TAG##*@}"
echo " Latest stable tag: $LATEST_TAG (version: $VERSION)"
if npm view "${package}@${VERSION}" version --registry=https://registry.npmjs.org &>/dev/null; then
echo " ✅ Version $VERSION exists on npm - tag is valid"
else
echo " ⚠️ Version $VERSION NOT found on npm - tag is orphaned!"
echo " 🧹 Deleting orphaned tag: $LATEST_TAG"
git tag -d "$LATEST_TAG" 2>/dev/null && echo " ✅ Deleted local tag" || echo " ⚠️ Local tag already deleted"
git push --delete origin "$LATEST_TAG" 2>/dev/null && CLEANED_TAGS+=("$LATEST_TAG") && echo " ✅ Deleted remote tag" || echo " ⚠️ Remote tag already deleted"
fi
done
echo ""
echo "=========================================="
echo "Checking for missing Git tags"
echo "=========================================="
CREATED_TAGS=()
for package in "${PACKAGES[@]}"; do
if [ -z "$package" ]; then continue; fi
echo "Checking package: $package"
if [[ "$INPUT_NPM_TAG" == "next" ]]; then
LATEST_NPM=$(npm view "$package" dist-tags.next --registry=https://registry.npmjs.org 2>/dev/null || echo "")
else
LATEST_NPM=$(npm view "$package" version --registry=https://registry.npmjs.org 2>/dev/null || echo "")
fi
if [ -z "$LATEST_NPM" ]; then
echo " ℹ️ Package not on npm yet (will be first release)"
continue
fi
EXPECTED_TAG="${package}@${LATEST_NPM}"
echo " Latest npm version: $LATEST_NPM"
if git rev-parse "$EXPECTED_TAG" >/dev/null 2>&1; then
echo " ✅ Git tag exists: $EXPECTED_TAG (in sync)"
else
echo " 🔧 Git tag missing for npm version $LATEST_NPM"
echo " 🏷️ Creating tag: $EXPECTED_TAG"
git tag "$EXPECTED_TAG" HEAD 2>/dev/null && CREATED_TAGS+=("$EXPECTED_TAG") && echo " ✅ Created local tag" || echo " ⚠️ Failed to create tag"
fi
done
# Push created tags
for tag in "${CREATED_TAGS[@]}"; do
echo " 🏷️ Pushing tag: $tag"
git push origin "$tag" 2>/dev/null && echo " ✅ Pushed $tag" || echo " ⚠️ Failed to push $tag"
done
echo ""
echo "✅ Git/npm synchronization complete."
- name: Version and publish packages (dry run)
if: needs.detect.outputs.is_dry_run == 'true'
env:
INPUT_PROJECTS: ${{ needs.detect.outputs.projects }}
INPUT_PACKAGES: ${{ needs.detect.outputs.packages }}
INPUT_NPM_TAG: ${{ needs.detect.outputs.npm_tag }}
INPUT_VERSION_STRATEGY: ${{ needs.detect.outputs.version_strategy }}
INPUT_PREID: "next"
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
run: |
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > ~/.npmrc
echo "=========================================="
echo "DRY RUN - Publishing Preview"
echo "=========================================="
echo "Projects: $INPUT_PROJECTS"
echo "npm tag: $INPUT_NPM_TAG"
echo "Version strategy: $INPUT_VERSION_STRATEGY"
# Source shared validation function (defense-in-depth for bash arithmetic)
source .github/scripts/validate-numeric.sh
calculate_prerelease_version() {
local package="$1"
local preid="$2"
local latest_version
latest_version=$(npm view "$package" dist-tags.latest --registry=https://registry.npmjs.org 2>/dev/null || echo "")
if [ -z "$latest_version" ]; then
latest_version="0.0.0"
fi
local major minor patch
IFS='.' read -r major minor patch <<< "$latest_version"
patch="${patch%%-*}"
# Validate version components before arithmetic (defense-in-depth)
major=$(validate_numeric "$major" "major") || return 1
minor=$(validate_numeric "$minor" "minor") || return 1
patch=$(validate_numeric "$patch" "patch") || return 1
local base_version="${major}.${minor}.$((patch + 1))"
local npm_prerelease=-1
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
if [ "$npm_versions" != "[]" ]; then
local highest_npm
highest_npm=$(echo "$npm_versions" | jq -r --arg base "$base_version" --arg preid "$preid" '
.[] |
select(startswith($base + "-" + $preid + ".")) |
split("-" + $preid + ".")[1] |
tonumber
' 2>/dev/null | sort -n | tail -1)
[ -n "$highest_npm" ] && npm_prerelease=$highest_npm
fi
local git_prerelease=-1
local git_tags
git_tags=$(git tag -l "${package}@${base_version}-${preid}.*" 2>/dev/null || echo "")
if [ -n "$git_tags" ]; then
local highest_git
highest_git=$(echo "$git_tags" | sed "s|${package}@${base_version}-${preid}\.||" | sort -n | tail -1)
[[ "$highest_git" =~ ^[0-9]+$ ]] && git_prerelease=$highest_git
fi
local max_prerelease=$(( npm_prerelease > git_prerelease ? npm_prerelease : git_prerelease ))
local new_version="${base_version}-${preid}.$((max_prerelease + 1))"
echo "$new_version"
}
# Calculate stable version for main branch releases (dry run version)
calculate_stable_version() {
local package="$1"
local latest_version
latest_version=$(npm view "$package" dist-tags.latest --registry=https://registry.npmjs.org 2>/dev/null || echo "")
if [ -z "$latest_version" ]; then
echo "0.0.1"
return
fi
local base_version="${latest_version%%-*}"
if [[ "$latest_version" == *"-"* ]]; then
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
local base_exists_on_npm=false
if echo "$npm_versions" | jq -e --arg v "$base_version" 'index($v)' > /dev/null 2>&1; then
base_exists_on_npm=true
fi
local base_exists_as_tag=false
if git rev-parse "${package}@${base_version}" >/dev/null 2>&1; then
base_exists_as_tag=true
fi
if [ "$base_exists_on_npm" = false ] && [ "$base_exists_as_tag" = false ]; then
echo "$base_version"
return
fi
fi
local major minor patch
IFS='.' read -r major minor patch <<< "$base_version"
# Validate version components before arithmetic (defense-in-depth)
major=$(validate_numeric "$major" "major") || return 1
minor=$(validate_numeric "$minor" "minor") || return 1
patch=$(validate_numeric "$patch" "patch") || return 1
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
local highest_stable_patch=$patch
if [ "$npm_versions" != "[]" ]; then
local highest_npm_stable
highest_npm_stable=$(echo "$npm_versions" | jq -r --arg maj "$major" --arg min "$minor" '
.[] |
select(test("^" + $maj + "\\." + $min + "\\.[0-9]+$")) |
split(".")[2] |
tonumber
' 2>/dev/null | sort -n | tail -1)
if [ -n "$highest_npm_stable" ]; then
highest_stable_patch=$highest_npm_stable
fi
fi
local git_tags
git_tags=$(git tag -l "${package}@${major}.${minor}.*" 2>/dev/null | grep -v '-' || echo "")
if [ -n "$git_tags" ]; then
local highest_git_patch
highest_git_patch=$(echo "$git_tags" | sed "s|${package}@${major}\.${minor}\.||" | sort -n | tail -1)
if [[ "$highest_git_patch" =~ ^[0-9]+$ ]] && [ "$highest_git_patch" -gt "$highest_stable_patch" ]; then
highest_stable_patch=$highest_git_patch
fi
fi
local new_version="${major}.${minor}.$((highest_stable_patch + 1))"
echo "$new_version"
}
IFS=',' read -ra PROJECT_ARRAY <<< "$INPUT_PROJECTS"
IFS=',' read -ra PACKAGE_ARRAY <<< "$INPUT_PACKAGES"
for i in "${!PROJECT_ARRAY[@]}"; do
project="${PROJECT_ARRAY[$i]}"
package="${PACKAGE_ARRAY[$i]}"
echo "-------------------------------------------"
echo "Processing: $project → $package"
echo "-------------------------------------------"
if [[ "$INPUT_VERSION_STRATEGY" == "prerelease" ]]; then
CALCULATED_VERSION=$(calculate_prerelease_version "$package" "$INPUT_PREID")
echo "📋 WOULD SET VERSION TO: $CALCULATED_VERSION"
echo "📋 WOULD CREATE TAG: ${package}@${CALCULATED_VERSION}"
else
CALCULATED_VERSION=$(calculate_stable_version "$package")
echo "📋 WOULD SET VERSION TO: $CALCULATED_VERSION"
echo "📋 WOULD CREATE TAG: ${package}@${CALCULATED_VERSION}"
fi
echo "📋 WOULD PUBLISH: $package with tag '$INPUT_NPM_TAG'"
echo ""
done
echo "=========================================="
echo "DRY RUN COMPLETE - No changes made"
echo "=========================================="
- name: Version, publish, and push packages
if: needs.detect.outputs.is_dry_run != 'true'
id: publish
env:
INPUT_PROJECTS: ${{ needs.detect.outputs.projects }}
INPUT_PACKAGES: ${{ needs.detect.outputs.packages }}
INPUT_NPM_TAG: ${{ needs.detect.outputs.npm_tag }}
INPUT_VERSION_STRATEGY: ${{ needs.detect.outputs.version_strategy }}
INPUT_PREID: "next"
INPUT_BRANCH: ${{ github.ref_name }}
NODE_AUTH_TOKEN: ${{ secrets.NODE_AUTH_TOKEN }}
GITHUB_TOKEN: ${{ secrets.WORKFLOW_PAT }}
run: |
echo "//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}" > ~/.npmrc
echo "Processing packages with tag: $INPUT_NPM_TAG"
echo "=========================================="
echo "ATOMIC PUBLISH STRATEGY"
echo "=========================================="
echo "For each package: version → publish → push (or revert on failure)"
echo ""
# Source shared validation function (defense-in-depth for bash arithmetic)
source .github/scripts/validate-numeric.sh
calculate_prerelease_version() {
local package="$1"
local preid="$2"
echo " Calculating prerelease version for $package with preid '$preid'..." >&2
local latest_version
latest_version=$(npm view "$package" dist-tags.latest --registry=https://registry.npmjs.org 2>/dev/null || echo "")
if [ -z "$latest_version" ]; then
latest_version="0.0.0"
echo " 📦 Package not yet published, using base 0.0.0" >&2
else
echo " 📦 Latest version on npm: $latest_version" >&2
fi
local major minor patch
IFS='.' read -r major minor patch <<< "$latest_version"
patch="${patch%%-*}"
# Validate version components before arithmetic (defense-in-depth)
major=$(validate_numeric "$major" "major") || return 1
minor=$(validate_numeric "$minor" "minor") || return 1
patch=$(validate_numeric "$patch" "patch") || return 1
local base_version="${major}.${minor}.$((patch + 1))"
echo " 📐 Base version: $base_version" >&2
local npm_prerelease=-1
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
if [ "$npm_versions" != "[]" ]; then
local highest_npm
highest_npm=$(echo "$npm_versions" | jq -r --arg base "$base_version" --arg preid "$preid" '
.[] |
select(startswith($base + "-" + $preid + ".")) |
split("-" + $preid + ".")[1] |
tonumber
' 2>/dev/null | sort -n | tail -1)
if [ -n "$highest_npm" ]; then
npm_prerelease=$highest_npm
echo " 🔍 Highest npm prerelease: ${base_version}-${preid}.${npm_prerelease}" >&2
fi
fi
local git_prerelease=-1
local git_tags
git_tags=$(git tag -l "${package}@${base_version}-${preid}.*" 2>/dev/null || echo "")
if [ -n "$git_tags" ]; then
local highest_git
highest_git=$(echo "$git_tags" | sed "s|${package}@${base_version}-${preid}\.||" | sort -n | tail -1)
if [[ "$highest_git" =~ ^[0-9]+$ ]]; then
git_prerelease=$highest_git
echo " 🏷️ Highest git prerelease: ${base_version}-${preid}.${git_prerelease}" >&2
fi
fi
local max_prerelease=$(( npm_prerelease > git_prerelease ? npm_prerelease : git_prerelease ))
local new_version="${base_version}-${preid}.$((max_prerelease + 1))"
echo " ✨ New version: $new_version" >&2
echo "$new_version"
}
# Calculate stable version for main branch releases
# Strategy: Get latest from npm, strip prerelease suffix, check if exists, bump if needed
calculate_stable_version() {
local package="$1"
echo " Calculating stable version for $package..." >&2
# Get the latest version from npm (could be stable or prerelease due to past bugs)
local latest_version
latest_version=$(npm view "$package" dist-tags.latest --registry=https://registry.npmjs.org 2>/dev/null || echo "")
if [ -z "$latest_version" ]; then
# Package not yet published, start at 0.0.1
echo " 📦 Package not yet published, using 0.0.1" >&2
echo "0.0.1"
return
fi
echo " 📦 Latest version on npm: $latest_version" >&2
# Strip prerelease suffix to get base version
local base_version="${latest_version%%-*}"
echo " 📐 Base version (stripped prerelease): $base_version" >&2
# If the latest was a prerelease, check if the base version exists as stable
if [[ "$latest_version" == *"-"* ]]; then
echo " ℹ️ Latest is a prerelease, checking if base version $base_version exists..." >&2
# Check if base version exists on npm
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
local base_exists_on_npm=false
if echo "$npm_versions" | jq -e --arg v "$base_version" 'index($v)' > /dev/null 2>&1; then
base_exists_on_npm=true
echo " 🔍 Base version $base_version exists on npm" >&2
fi
# Check if base version exists as git tag
local base_exists_as_tag=false
if git rev-parse "${package}@${base_version}" >/dev/null 2>&1; then
base_exists_as_tag=true
echo " 🏷️ Base version $base_version exists as git tag" >&2
fi
if [ "$base_exists_on_npm" = false ] && [ "$base_exists_as_tag" = false ]; then
# Base version doesn't exist, use it (graduate from prerelease)
echo " ✨ New version: $base_version (graduating from prerelease)" >&2
echo "$base_version"
return
fi
fi
# Base version exists or latest is stable, need to bump patch
local major minor patch
IFS='.' read -r major minor patch <<< "$base_version"
# Validate version components before arithmetic (defense-in-depth)
major=$(validate_numeric "$major" "major") || return 1
minor=$(validate_numeric "$minor" "minor") || return 1
patch=$(validate_numeric "$patch" "patch") || return 1
# Find the highest stable version on npm
local npm_versions
npm_versions=$(npm view "$package" versions --json --registry=https://registry.npmjs.org 2>/dev/null || echo "[]")
local highest_stable_patch=$patch
if [ "$npm_versions" != "[]" ]; then
local highest_npm_stable
highest_npm_stable=$(echo "$npm_versions" | jq -r --arg maj "$major" --arg min "$minor" '
.[] |
select(test("^" + $maj + "\\." + $min + "\\.[0-9]+$")) |
split(".")[2] |
tonumber
' 2>/dev/null | sort -n | tail -1)
if [ -n "$highest_npm_stable" ]; then
highest_stable_patch=$highest_npm_stable
echo " 🔍 Highest stable patch on npm: ${major}.${minor}.${highest_stable_patch}" >&2
fi
fi
# Also check git tags for stable versions
local git_tags
git_tags=$(git tag -l "${package}@${major}.${minor}.*" 2>/dev/null | grep -v '-' || echo "")
if [ -n "$git_tags" ]; then
local highest_git_patch
highest_git_patch=$(echo "$git_tags" | sed "s|${package}@${major}\.${minor}\.||" | sort -n | tail -1)
if [[ "$highest_git_patch" =~ ^[0-9]+$ ]] && [ "$highest_git_patch" -gt "$highest_stable_patch" ]; then
highest_stable_patch=$highest_git_patch
echo " 🏷️ Highest stable patch in git: ${major}.${minor}.${highest_stable_patch}" >&2
fi
fi
local new_version="${major}.${minor}.$((highest_stable_patch + 1))"
echo " ✨ New version: $new_version" >&2
echo "$new_version"
}
IFS=',' read -ra PROJECTS <<< "$INPUT_PROJECTS"
IFS=',' read -ra PACKAGES <<< "$INPUT_PACKAGES"
FAILED_PACKAGES=()
SUCCESS_PACKAGES=()
declare -A SUCCESS_VERSIONS # Track versions for each successful package
for i in "${!PROJECTS[@]}"; do
project="${PROJECTS[$i]}"
package="${PACKAGES[$i]}"
if [ -z "$project" ] || [ -z "$package" ]; then continue; fi
echo "=========================================="
echo "Processing: $project → $package"
echo "=========================================="
PACKAGE_NAME=$(echo "$package" | sed 's/@[^/]*\///')
PROJECT_ROOT=$(bunx nx show project "$project" --json 2>/dev/null | jq -r '.root' || echo "")
if [ -z "$PROJECT_ROOT" ] || [ ! -f "$PROJECT_ROOT/package.json" ]; then
echo "❌ Could not find package.json for $project"
FAILED_PACKAGES+=("$package")
continue
fi
echo ""
echo "Step 1: Versioning package..."
if [[ "$INPUT_VERSION_STRATEGY" == "prerelease" ]]; then
# Prerelease versioning (next branch)
NEW_VERSION=$(calculate_prerelease_version "$package" "$INPUT_PREID")
if [ -z "$NEW_VERSION" ]; then
echo "❌ Failed to calculate version"
FAILED_PACKAGES+=("$package")
continue
fi
else
# Stable versioning (main branch) - use smart calculation instead of nx release version
NEW_VERSION=$(calculate_stable_version "$package")
if [ -z "$NEW_VERSION" ]; then
echo "❌ Failed to calculate version"
FAILED_PACKAGES+=("$package")
continue
fi
fi
echo " Setting version to: $NEW_VERSION"
jq --arg version "$NEW_VERSION" '.version = $version' "$PROJECT_ROOT/package.json" > "$PROJECT_ROOT/package.json.tmp"
mv "$PROJECT_ROOT/package.json.tmp" "$PROJECT_ROOT/package.json"
# Run sync-version target if it exists (updates version.ts and similar files)
if bunx nx show project "$project" --json 2>/dev/null | jq -e '.targets["sync-version"]' > /dev/null 2>&1; then