release.yml 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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.
  16. #
  17. # npm auth is OIDC trusted publishing (no NPM_TOKEN): every published package
  18. # (@colbymchenry/codegraph + the per-platform bundles) has this repo +
  19. # release.yml configured as its trusted publisher on npmjs.com. Adding a new
  20. # platform package means configuring its trusted publisher there before the
  21. # first release that includes it.
  22. on:
  23. workflow_dispatch: {}
  24. permissions:
  25. contents: write # create the GitHub Release + tag, push the CHANGELOG promote
  26. id-token: write # OIDC token for npm --provenance and Sigstore signing
  27. attestations: write # store the GitHub artifact attestations for the bundles
  28. jobs:
  29. # Native extraction-kernel prebuilds (docs/design/rust-kernel-migration-plan.md).
  30. # As of 1.5.0 the kernel is the release's HEADLINE, not an optional extra, so
  31. # this matrix is REQUIRED: a failed kernel build blocks the release instead of
  32. # silently shipping wasm-only bundles under a Rust-engine banner. Per-file
  33. # wasm fallback still exists at runtime for erroring files and unsupported
  34. # platforms — but every published bundle must carry its .node. Note the
  35. # vendored-grammar-C languages (kotlin/lua/scala/dart) compile parser.c via
  36. # the cc crate, so each leg needs its platform C toolchain (runner images
  37. # ship one). (Runner images ship rustup; build-kernel.sh
  38. # adds each cross target itself.)
  39. kernel:
  40. strategy:
  41. fail-fast: false
  42. matrix:
  43. include:
  44. - runner: macos-14
  45. targets: aarch64-apple-darwin x86_64-apple-darwin
  46. - runner: ubuntu-22.04 # oldest glibc runner → widest compatibility
  47. targets: x86_64-unknown-linux-gnu
  48. - runner: ubuntu-22.04-arm
  49. targets: aarch64-unknown-linux-gnu
  50. - runner: windows-latest
  51. targets: x86_64-pc-windows-msvc aarch64-pc-windows-msvc
  52. runs-on: ${{ matrix.runner }}
  53. steps:
  54. - uses: actions/checkout@v6
  55. - name: Build kernel prebuilds
  56. shell: bash
  57. run: |
  58. for t in ${{ matrix.targets }}; do
  59. bash scripts/build-kernel.sh --target "$t"
  60. done
  61. ls -R codegraph-kernel/prebuilds
  62. - uses: actions/upload-artifact@v4
  63. with:
  64. name: kernel-${{ matrix.runner }}
  65. path: codegraph-kernel/prebuilds/
  66. if-no-files-found: error
  67. release:
  68. runs-on: ubuntu-latest
  69. needs: kernel
  70. steps:
  71. - uses: actions/checkout@v6
  72. with:
  73. # Default checkout is detached at a SHA; we need an actual branch
  74. # so the CHANGELOG-promote commit knows where to push.
  75. ref: ${{ github.ref }}
  76. # Authenticate as the maintainer (admin), not as github-actions[bot].
  77. # The "Require PR approval for main branch" ruleset only lets the
  78. # Admin repo role bypass — and GitHub blocks adding the GitHub
  79. # Actions integration to bypass_actors on user-owned (non-org)
  80. # repos with "Actor GitHub Actions integration must be part of
  81. # the ruleset source or owner organization." So the auto-promote
  82. # and auto-sync `git push origin HEAD:main` steps below both fail
  83. # under the default GITHUB_TOKEN. Using a fine-grained PAT owned
  84. # by the admin makes the push go through cleanly. Set the
  85. # RELEASE_PAT secret with: contents:write on this repo, no other
  86. # scopes. Rotate per your token policy; the workflow only runs
  87. # on manual dispatch so the blast radius is small.
  88. token: ${{ secrets.RELEASE_PAT }}
  89. - uses: actions/setup-node@v6
  90. with:
  91. node-version: 22
  92. # No registry-url here: it writes an .npmrc that requires a
  93. # NODE_AUTH_TOKEN env var to exist, and we publish via OIDC
  94. # trusted publishing instead of a token.
  95. - name: Upgrade npm for OIDC trusted publishing
  96. # Trusted publishing needs npm >= 11.5; Node 22 bundles npm 10.
  97. run: npm install -g npm@11 && npm --version
  98. - name: Sync package-lock.json if version drifted
  99. # When the maintainer bumps the version on package.json only — for
  100. # example via a GitHub web-UI edit — `npm ci` would refuse to run
  101. # with `EUSAGE: npm ci can only install packages when your
  102. # package.json and package-lock.json … are in sync`. This step
  103. # rewrites just the lock-file's version fields (top-level + the
  104. # `packages.""` entry) to match package.json, then auto-commits
  105. # and pushes the result so on-disk truth on `main` stays
  106. # consistent. Idempotent: if the lock file already matches, no
  107. # commit is made.
  108. run: |
  109. set -euo pipefail
  110. PKG_V=$(node -p "require('./package.json').version")
  111. LOCK_V=$(node -p "require('./package-lock.json').version")
  112. if [ "$PKG_V" = "$LOCK_V" ]; then
  113. echo "package-lock.json already at $PKG_V — nothing to sync."
  114. exit 0
  115. fi
  116. echo "Lock-file version drift: lock=$LOCK_V, package=$PKG_V. Syncing."
  117. # `--package-lock-only` rewrites only the lock file, doesn't
  118. # touch node_modules or actually install anything. Cheap.
  119. npm install --package-lock-only --ignore-scripts
  120. # Sanity: lockfile should now report the package version.
  121. NEW_LOCK_V=$(node -p "require('./package-lock.json').version")
  122. if [ "$NEW_LOCK_V" != "$PKG_V" ]; then
  123. echo "::error::lock-file still at $NEW_LOCK_V after sync attempt; expected $PKG_V"; exit 1
  124. fi
  125. if git diff --quiet -- package-lock.json; then
  126. echo "lock file unchanged after sync? bailing"; exit 1
  127. fi
  128. git config user.name "github-actions[bot]"
  129. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  130. git add package-lock.json
  131. git commit -m "release: sync package-lock.json to ${PKG_V}" -m "[skip ci] Auto-generated by Release workflow."
  132. git push origin "HEAD:${GITHUB_REF#refs/heads/}"
  133. - run: npm ci
  134. - name: Ensure zip/unzip
  135. run: sudo apt-get update -qq && sudo apt-get install -y -qq zip unzip
  136. - name: Resolve version
  137. id: ver
  138. run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT"
  139. - name: Promote [Unreleased] → [<version>] in CHANGELOG.md
  140. # Idempotent: a no-op if [Unreleased] is empty OR if the previous
  141. # run already moved everything. Auto-commit + push the change back
  142. # so the version block on main is the source of truth going
  143. # forward (and so subsequent extract-release-notes.mjs calls
  144. # surface the full content even if this run is re-triggered).
  145. run: |
  146. set -euo pipefail
  147. V="${{ steps.ver.outputs.version }}"
  148. before=$(git rev-parse HEAD)
  149. node scripts/prepare-release.mjs "$V"
  150. if git diff --quiet -- CHANGELOG.md; then
  151. echo "CHANGELOG.md unchanged — nothing to commit."
  152. else
  153. git config user.name "github-actions[bot]"
  154. git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
  155. git add CHANGELOG.md
  156. git commit -m "docs(changelog): promote [Unreleased] into [${V}]" -m "[skip ci] Auto-generated by Release workflow."
  157. # Push to the branch the workflow was triggered on (main).
  158. git push origin "HEAD:${GITHUB_REF#refs/heads/}"
  159. fi
  160. - name: Download kernel prebuilds
  161. # All legs are required (see the kernel job), so every target's
  162. # <target>/codegraph-kernel.node must be present in release/kernel/.
  163. uses: actions/download-artifact@v4
  164. with:
  165. pattern: kernel-*
  166. merge-multiple: true
  167. path: release/kernel/
  168. - name: Kernel contract + full walker-parity gate
  169. # Runs EVERY kernel suite (__tests__/kernel-*.test.ts — the wire
  170. # contract, the grammar-source parity table checks, and all 20
  171. # languages' walker byte-parity suites incl. torture/CRLF/defer pins)
  172. # against the freshly built linux-x64 .node. The glob keeps this gate
  173. # current as languages are added. CODEGRAPH_KERNEL_EXPECT=1 turns a
  174. # missing binary into a FAILURE, and a missing prebuild fails outright
  175. # — the matrix is required, so absence here means a wiring bug.
  176. run: |
  177. if [ -f release/kernel/linux-x64/codegraph-kernel.node ]; then
  178. mkdir -p codegraph-kernel/prebuilds/linux-x64
  179. cp release/kernel/linux-x64/codegraph-kernel.node codegraph-kernel/prebuilds/linux-x64/
  180. CODEGRAPH_KERNEL_EXPECT=1 npx vitest run __tests__/kernel-*.test.ts
  181. else
  182. echo "::error::linux-x64 kernel prebuild missing despite required matrix"
  183. exit 1
  184. fi
  185. - name: Build all platform bundles
  186. run: |
  187. for t in darwin-arm64 darwin-x64 linux-x64 linux-arm64 win32-x64 win32-arm64; do
  188. bash scripts/build-bundle.sh "$t"
  189. done
  190. ls -lh release
  191. - name: Generate SHA256SUMS
  192. # Published as a release asset; the npm launcher verifies downloaded
  193. # bundles against it (basenames only, so its path.basename match works).
  194. run: |
  195. ( cd release && sha256sum codegraph-* > SHA256SUMS )
  196. cat release/SHA256SUMS
  197. - name: Attest build provenance for release bundles
  198. # Signed, publicly-verifiable proof that each bundle (and SHA256SUMS)
  199. # was built by this workflow from this repo — SHA256SUMS alone only
  200. # proves integrity, not origin, since it ships next to the bundles.
  201. # Verify any downloaded artifact with:
  202. # gh attestation verify <file> -R colbymchenry/codegraph
  203. uses: actions/attest-build-provenance@v4
  204. with:
  205. subject-path: |
  206. release/codegraph-*
  207. release/SHA256SUMS
  208. - name: Release notes from CHANGELOG.md
  209. # The [<version>] block was guaranteed-populated by the
  210. # "Promote" step above, so the [Unreleased] fallback should
  211. # never be needed in practice. Kept for defense-in-depth.
  212. run: |
  213. V="${{ steps.ver.outputs.version }}"
  214. node scripts/extract-release-notes.mjs "$V" > notes.md 2>/dev/null \
  215. || node scripts/extract-release-notes.mjs Unreleased > notes.md 2>/dev/null || true
  216. if [ ! -s notes.md ]; then
  217. echo "::error::No release notes in CHANGELOG.md for [$V] or [Unreleased]."
  218. exit 1
  219. fi
  220. echo "----- release notes -----"; cat notes.md
  221. - name: Create GitHub Release
  222. env:
  223. GH_TOKEN: ${{ github.token }}
  224. run: |
  225. TAG="v${{ steps.ver.outputs.version }}"
  226. # Idempotent: create the release once, otherwise (re-run) refresh assets.
  227. if gh release view "$TAG" >/dev/null 2>&1; then
  228. gh release upload "$TAG" release/codegraph-* release/SHA256SUMS --clobber
  229. else
  230. gh release create "$TAG" release/codegraph-* release/SHA256SUMS --title "$TAG" --notes-file notes.md
  231. fi
  232. - name: Publish to npm
  233. # Auth is OIDC trusted publishing (id-token: write above) — npm mints
  234. # a short-lived credential from the workflow's identity; there is no
  235. # NPM_TOKEN. Provenance is generated automatically on this path; the
  236. # explicit --provenance keeps the intent visible and fails loudly if
  237. # OIDC is ever unavailable.
  238. run: |
  239. V="${{ steps.ver.outputs.version }}"
  240. bash scripts/pack-npm.sh "$V"
  241. # Platform packages first, then the main shim (which depends on them).
  242. # Skip any already on the registry so a re-run only fills in gaps.
  243. for dir in release/npm/codegraph-* release/npm/main; do
  244. name=$(node -p "require('./$dir/package.json').name")
  245. if npm view "$name@$V" version >/dev/null 2>&1; then
  246. echo "skip $name@$V (already published)"
  247. else
  248. echo "publishing $name@$V"
  249. # --provenance: publish with an npm provenance attestation
  250. # (needs the id-token: write permission above and the
  251. # repository field pack-npm.sh writes into each package.json).
  252. ( cd "$dir" && npm publish --access public --provenance )
  253. fi
  254. done
  255. - name: Verify every package is actually on the registry
  256. run: |
  257. V="${{ steps.ver.outputs.version }}"
  258. # npm publish can print success without persisting; confirm against the
  259. # registry (with retries for propagation) so green means really shipped.
  260. for dir in release/npm/codegraph-* release/npm/main; do
  261. name=$(node -p "require('./$dir/package.json').name")
  262. ok=
  263. for i in 1 2 3 4 5 6; do
  264. if npm view "$name@$V" version >/dev/null 2>&1; then ok=1; break; fi
  265. echo "waiting for $name@$V to appear ($i)…"; sleep 10
  266. done
  267. [ -n "$ok" ] || { echo "::error::$name@$V never appeared on the registry"; exit 1; }
  268. echo "verified $name@$V"
  269. done
  270. - name: Sync packages to npmmirror
  271. # npmmirror/cnpm mirror lazily and frequently never pull the per-platform
  272. # optionalDependencies on their own, so `npm i` there fails with
  273. # "no prebuilt bundle" (issue #303). Nudge a sync now so mirror users get
  274. # the bundle without waiting. Best-effort — the launcher also self-heals
  275. # from GitHub Releases — so a mirror hiccup never fails the release.
  276. continue-on-error: true
  277. run: |
  278. for dir in release/npm/codegraph-* release/npm/main; do
  279. name=$(node -p "require('./$dir/package.json').name")
  280. enc=$(node -p "encodeURIComponent(require('./$dir/package.json').name)")
  281. echo "sync $name"
  282. curl -s -X PUT "https://registry.npmmirror.com/-/package/$enc/syncs" || true
  283. echo
  284. done