1
0

release.yml 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. name: Release
  2. # Manually triggered ("Run workflow"). On trigger it:
  3. # 1. reads the version from package.json,
  4. # 2. promotes `## [Unreleased]` content into `## [<version>]` in
  5. # CHANGELOG.md (and commits + pushes that change back to main), so
  6. # the published release notes are never sparse just because the
  7. # maintainer didn't pre-stage the [<version>] block by hand,
  8. # 3. builds a self-contained bundle for every platform (one runner — there's no
  9. # native compilation, so cross-packaging is fine),
  10. # 4. creates the GitHub Release (tag v<version>) with all archives, using the
  11. # release notes from CHANGELOG.md,
  12. # 5. publishes the npm thin-installer (shim + per-platform packages).
  13. #
  14. # Before triggering: bump package.json. CHANGELOG.md entries can live under
  15. # `## [Unreleased]` — step 2 takes care of moving them. Set the NPM_TOKEN secret.
  16. on:
  17. workflow_dispatch: {}
  18. permissions:
  19. contents: write # create the GitHub Release + tag, push the CHANGELOG promote
  20. jobs:
  21. release:
  22. runs-on: ubuntu-latest
  23. steps:
  24. - uses: actions/checkout@v6
  25. with:
  26. # Default checkout is detached at a SHA; we need an actual branch
  27. # so the CHANGELOG-promote commit knows where to push.
  28. ref: ${{ github.ref }}
  29. # Authenticate as the maintainer (admin), not as github-actions[bot].
  30. # The "Require PR approval for main branch" ruleset only lets the
  31. # Admin repo role bypass — and GitHub blocks adding the GitHub
  32. # Actions integration to bypass_actors on user-owned (non-org)
  33. # repos with "Actor GitHub Actions integration must be part of
  34. # the ruleset source or owner organization." So the auto-promote
  35. # and auto-sync `git push origin HEAD:main` steps below both fail
  36. # under the default GITHUB_TOKEN. Using a fine-grained PAT owned
  37. # by the admin makes the push go through cleanly. Set the
  38. # RELEASE_PAT secret with: contents:write on this repo, no other
  39. # scopes. Rotate per your token policy; the workflow only runs
  40. # on manual dispatch so the blast radius is small.
  41. token: ${{ secrets.RELEASE_PAT }}
  42. - uses: actions/setup-node@v6
  43. with:
  44. node-version: 22
  45. registry-url: https://registry.npmjs.org
  46. - name: Sync package-lock.json if version drifted
  47. # When the maintainer bumps the version on package.json only — for
  48. # example via a GitHub web-UI edit — `npm ci` would refuse to run
  49. # with `EUSAGE: npm ci can only install packages when your
  50. # package.json and package-lock.json … are in sync`. This step
  51. # rewrites just the lock-file's version fields (top-level + the
  52. # `packages.""` entry) to match package.json, then auto-commits
  53. # and pushes the result so on-disk truth on `main` stays
  54. # consistent. Idempotent: if the lock file already matches, no
  55. # commit is made.
  56. run: |
  57. set -euo pipefail
  58. PKG_V=$(node -p "require('./package.json').version")
  59. LOCK_V=$(node -p "require('./package-lock.json').version")
  60. if [ "$PKG_V" = "$LOCK_V" ]; then
  61. echo "package-lock.json already at $PKG_V — nothing to sync."
  62. exit 0
  63. fi
  64. echo "Lock-file version drift: lock=$LOCK_V, package=$PKG_V. Syncing."
  65. # `--package-lock-only` rewrites only the lock file, doesn't
  66. # touch node_modules or actually install anything. Cheap.
  67. npm install --package-lock-only --ignore-scripts
  68. # Sanity: lockfile should now report the package version.
  69. NEW_LOCK_V=$(node -p "require('./package-lock.json').version")
  70. if [ "$NEW_LOCK_V" != "$PKG_V" ]; then
  71. echo "::error::lock-file still at $NEW_LOCK_V after sync attempt; expected $PKG_V"; exit 1
  72. fi
  73. if git diff --quiet -- package-lock.json; then
  74. echo "lock file unchanged after sync? bailing"; exit 1
  75. fi
  76. git config user.name "github-actions[bot]"
  77. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  78. git add package-lock.json
  79. git commit -m "release: sync package-lock.json to ${PKG_V}" -m "[skip ci] Auto-generated by Release workflow."
  80. git push origin "HEAD:${GITHUB_REF#refs/heads/}"
  81. - run: npm ci
  82. - name: Ensure zip/unzip
  83. run: sudo apt-get update -qq && sudo apt-get install -y -qq zip unzip
  84. - name: Resolve version
  85. id: ver
  86. run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
  87. - name: Promote [Unreleased] → [<version>] in CHANGELOG.md
  88. # Idempotent: a no-op if [Unreleased] is empty OR if the previous
  89. # run already moved everything. Auto-commit + push the change back
  90. # so the version block on main is the source of truth going
  91. # forward (and so subsequent extract-release-notes.mjs calls
  92. # surface the full content even if this run is re-triggered).
  93. run: |
  94. set -euo pipefail
  95. V="${{ steps.ver.outputs.version }}"
  96. before=$(git rev-parse HEAD)
  97. node scripts/prepare-release.mjs "$V"
  98. if git diff --quiet -- CHANGELOG.md; then
  99. echo "CHANGELOG.md unchanged — nothing to commit."
  100. else
  101. git config user.name "github-actions[bot]"
  102. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  103. git add CHANGELOG.md
  104. git commit -m "docs(changelog): promote [Unreleased] into [${V}]" -m "[skip ci] Auto-generated by Release workflow."
  105. # Push to the branch the workflow was triggered on (main).
  106. git push origin "HEAD:${GITHUB_REF#refs/heads/}"
  107. fi
  108. - name: Build all platform bundles
  109. run: |
  110. for t in darwin-arm64 darwin-x64 linux-x64 linux-arm64 win32-x64 win32-arm64; do
  111. bash scripts/build-bundle.sh "$t"
  112. done
  113. ls -lh release
  114. - name: Generate SHA256SUMS
  115. # Published as a release asset; the npm launcher verifies downloaded
  116. # bundles against it (basenames only, so its path.basename match works).
  117. run: |
  118. ( cd release && sha256sum codegraph-* > SHA256SUMS )
  119. cat release/SHA256SUMS
  120. - name: Release notes from CHANGELOG.md
  121. # The [<version>] block was guaranteed-populated by the
  122. # "Promote" step above, so the [Unreleased] fallback should
  123. # never be needed in practice. Kept for defense-in-depth.
  124. run: |
  125. V="${{ steps.ver.outputs.version }}"
  126. node scripts/extract-release-notes.mjs "$V" > notes.md 2>/dev/null \
  127. || node scripts/extract-release-notes.mjs Unreleased > notes.md 2>/dev/null || true
  128. if [ ! -s notes.md ]; then
  129. echo "::error::No release notes in CHANGELOG.md for [$V] or [Unreleased]."
  130. exit 1
  131. fi
  132. echo "----- release notes -----"; cat notes.md
  133. - name: Create GitHub Release
  134. env:
  135. GH_TOKEN: ${{ github.token }}
  136. run: |
  137. TAG="v${{ steps.ver.outputs.version }}"
  138. # Idempotent: create the release once, otherwise (re-run) refresh assets.
  139. if gh release view "$TAG" >/dev/null 2>&1; then
  140. gh release upload "$TAG" release/codegraph-* release/SHA256SUMS --clobber
  141. else
  142. gh release create "$TAG" release/codegraph-* release/SHA256SUMS --title "$TAG" --notes-file notes.md
  143. fi
  144. - name: Publish to npm
  145. env:
  146. NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
  147. run: |
  148. V="${{ steps.ver.outputs.version }}"
  149. bash scripts/pack-npm.sh "$V"
  150. # Platform packages first, then the main shim (which depends on them).
  151. # Skip any already on the registry so a re-run only fills in gaps.
  152. for dir in release/npm/codegraph-* release/npm/main; do
  153. name=$(node -p "require('./$dir/package.json').name")
  154. if npm view "$name@$V" version >/dev/null 2>&1; then
  155. echo "skip $name@$V (already published)"
  156. else
  157. echo "publishing $name@$V"
  158. ( cd "$dir" && npm publish --access public )
  159. fi
  160. done
  161. - name: Verify every package is actually on the registry
  162. run: |
  163. V="${{ steps.ver.outputs.version }}"
  164. # npm publish can print success without persisting; confirm against the
  165. # registry (with retries for propagation) so green means really shipped.
  166. for dir in release/npm/codegraph-* release/npm/main; do
  167. name=$(node -p "require('./$dir/package.json').name")
  168. ok=
  169. for i in 1 2 3 4 5 6; do
  170. if npm view "$name@$V" version >/dev/null 2>&1; then ok=1; break; fi
  171. echo "waiting for $name@$V to appear ($i)…"; sleep 10
  172. done
  173. [ -n "$ok" ] || { echo "::error::$name@$V never appeared on the registry"; exit 1; }
  174. echo "verified $name@$V"
  175. done
  176. - name: Sync packages to npmmirror
  177. # npmmirror/cnpm mirror lazily and frequently never pull the per-platform
  178. # optionalDependencies on their own, so `npm i` there fails with
  179. # "no prebuilt bundle" (issue #303). Nudge a sync now so mirror users get
  180. # the bundle without waiting. Best-effort — the launcher also self-heals
  181. # from GitHub Releases — so a mirror hiccup never fails the release.
  182. continue-on-error: true
  183. run: |
  184. for dir in release/npm/codegraph-* release/npm/main; do
  185. name=$(node -p "require('./$dir/package.json').name")
  186. enc=$(node -p "encodeURIComponent(require('./$dir/package.json').name)")
  187. echo "sync $name"
  188. curl -s -X PUT "https://registry.npmmirror.com/-/package/$enc/syncs" || true
  189. echo
  190. done