|
| 1 | +name: Release |
| 2 | + |
| 3 | +# Builds the package and publishes it to npm. Triggered by pushing a version |
| 4 | +# tag (e.g. v0.2.1); also runnable manually for dry runs (build-only — the |
| 5 | +# publish job is gated on an actual tag push). |
| 6 | +on: |
| 7 | + push: |
| 8 | + tags: ["v*"] |
| 9 | + workflow_dispatch: |
| 10 | + |
| 11 | +# Re-pushing a tag (or re-dispatching) cancels an in-flight run for the same |
| 12 | +# ref so a stuck build can't pile up behind a newer attempt. |
| 13 | +concurrency: |
| 14 | + group: release-${{ github.ref }} |
| 15 | + cancel-in-progress: true |
| 16 | + |
| 17 | +jobs: |
| 18 | + build: |
| 19 | + name: Build package |
| 20 | + runs-on: ubuntu-latest |
| 21 | + timeout-minutes: 10 |
| 22 | + permissions: |
| 23 | + contents: read |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + |
| 27 | + - uses: pnpm/action-setup@v4 |
| 28 | + - uses: actions/setup-node@v4 |
| 29 | + with: |
| 30 | + node-version: 22 |
| 31 | + cache: pnpm |
| 32 | + |
| 33 | + - run: pnpm install --frozen-lockfile |
| 34 | + |
| 35 | + # Fail fast if the tag doesn't match package.json's version, so the |
| 36 | + # GitHub Release name can never disagree with the version published to npm. |
| 37 | + - name: Check tag matches package version |
| 38 | + if: startsWith(github.ref, 'refs/tags/') |
| 39 | + run: | |
| 40 | + pkg=$(node -p "require('./package.json').version") |
| 41 | + tag=${GITHUB_REF_NAME#v} |
| 42 | + if [ "$pkg" != "$tag" ]; then |
| 43 | + echo "::error::Tag v${tag} does not match package.json version ${pkg}. Bump package.json and re-tag." |
| 44 | + exit 1 |
| 45 | + fi |
| 46 | + echo "Tag and package version agree: ${pkg}" |
| 47 | +
|
| 48 | + # Runs the postbuild node-builtin guard (scripts/assert-no-node-builtins.mjs) |
| 49 | + # automatically, and builds both the main entry and the ./node subpath. |
| 50 | + - name: Build |
| 51 | + run: pnpm build |
| 52 | + |
| 53 | + # Bundle everything `pnpm publish` needs (package.json, README, LICENSE, |
| 54 | + # dist/ — which includes dist/node/) so the publish job below can run |
| 55 | + # without checking out the repo. |
| 56 | + - name: Assemble publishable package |
| 57 | + run: | |
| 58 | + mkdir -p publish |
| 59 | + cp -r dist publish/dist |
| 60 | + cp package.json README.md LICENSE publish/ |
| 61 | +
|
| 62 | + - name: Upload artifact |
| 63 | + uses: actions/upload-artifact@v4 |
| 64 | + with: |
| 65 | + name: package |
| 66 | + path: publish/ |
| 67 | + if-no-files-found: error |
| 68 | + |
| 69 | + release: |
| 70 | + name: Publish to npm and GitHub |
| 71 | + needs: build |
| 72 | + runs-on: ubuntu-latest |
| 73 | + # Only publish for real tag pushes; workflow_dispatch runs just build. |
| 74 | + if: startsWith(github.ref, 'refs/tags/') |
| 75 | + permissions: |
| 76 | + contents: write |
| 77 | + # Required for npm provenance attestation. |
| 78 | + id-token: write |
| 79 | + steps: |
| 80 | + - name: Download artifact |
| 81 | + uses: actions/download-artifact@v4 |
| 82 | + with: |
| 83 | + name: package |
| 84 | + path: package |
| 85 | + |
| 86 | + - name: List artifact contents |
| 87 | + run: ls -lR package |
| 88 | + |
| 89 | + - uses: pnpm/action-setup@v4 |
| 90 | + - uses: actions/setup-node@v4 |
| 91 | + with: |
| 92 | + node-version: 22 |
| 93 | + registry-url: "https://registry.npmjs.org" |
| 94 | + |
| 95 | + - name: Publish to npm |
| 96 | + working-directory: package |
| 97 | + env: |
| 98 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 99 | + run: pnpm publish --access public --no-git-checks --provenance |
| 100 | + |
| 101 | + - name: Publish GitHub Release |
| 102 | + env: |
| 103 | + GH_TOKEN: ${{ github.token }} |
| 104 | + # This job doesn't check out the repo, so gh can't infer it from a local |
| 105 | + # git remote — pass --repo explicitly on every gh call. GitHub release |
| 106 | + # assets are identified by filename within a release, and dist/node/ |
| 107 | + # (the ./node subpath build) has the exact same filenames as dist/ |
| 108 | + # (index.js, index.cjs, ...) — uploading both by basename would |
| 109 | + # collide, so flatten into a staging dir with the node/ half prefixed. |
| 110 | + run: | |
| 111 | + set -euo pipefail |
| 112 | + tag="${GITHUB_REF_NAME}" |
| 113 | + repo="${GITHUB_REPOSITORY}" |
| 114 | +
|
| 115 | + mkdir -p release-assets |
| 116 | + find package/dist -maxdepth 1 -type f -exec cp {} release-assets/ \; |
| 117 | + for f in package/dist/node/*; do |
| 118 | + cp "$f" "release-assets/node-$(basename "$f")" |
| 119 | + done |
| 120 | + assets=(release-assets/*) |
| 121 | +
|
| 122 | + if gh release view "$tag" --repo "$repo" >/dev/null 2>&1; then |
| 123 | + echo "Release $tag exists; uploading assets (clobbering)." |
| 124 | + gh release upload "$tag" "${assets[@]}" --repo "$repo" --clobber |
| 125 | + else |
| 126 | + echo "Creating release $tag." |
| 127 | + gh release create "$tag" \ |
| 128 | + --repo "$repo" \ |
| 129 | + --title "$tag" \ |
| 130 | + --generate-notes \ |
| 131 | + "${assets[@]}" |
| 132 | + fi |
0 commit comments