release.yml 11 KB

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