|
| 1 | +name: Release to npm |
| 2 | + |
| 3 | +# Fires when release-please publishes a GitHub Release — that only happens when a |
| 4 | +# release pull request is merged, which is itself a deliberate act with the version |
| 5 | +# and changelog visible for review. So merging the release PR is the single action |
| 6 | +# that ships a version. |
| 7 | +# |
| 8 | +# Still dispatchable by hand, defaulting to a dry run, for re-publishing after a |
| 9 | +# failure or validating the tarball without shipping. |
| 10 | +on: |
| 11 | + release: |
| 12 | + types: [published] |
| 13 | + workflow_dispatch: |
| 14 | + inputs: |
| 15 | + dry_run: |
| 16 | + description: 'Pack and validate without publishing' |
| 17 | + type: boolean |
| 18 | + default: true |
| 19 | + |
| 20 | +jobs: |
| 21 | + release: |
| 22 | + runs-on: ubuntu-latest |
| 23 | + permissions: |
| 24 | + contents: read |
| 25 | + # Required for OIDC — this is what npm exchanges for a short-lived publish |
| 26 | + # credential, and what provenance is derived from. Only works on a public |
| 27 | + # repository. |
| 28 | + id-token: write |
| 29 | + |
| 30 | + steps: |
| 31 | + - uses: actions/checkout@v7 |
| 32 | + |
| 33 | + - uses: actions/setup-node@v7 |
| 34 | + with: |
| 35 | + # 24 rather than 22: trusted publishing needs npm >= 11.5.1, and Node 22 |
| 36 | + # still ships npm 10.x. |
| 37 | + node-version: 24 |
| 38 | + cache: npm |
| 39 | + registry-url: https://registry.npmjs.org |
| 40 | + |
| 41 | + - name: Ensure an npm new enough for trusted publishing |
| 42 | + run: | |
| 43 | + npm install -g npm@^11 |
| 44 | + npm --version |
| 45 | +
|
| 46 | + # Also compiles the addon, since the install script runs node-gyp. Ubuntu is |
| 47 | + # unaffected by the Visual Studio detection problem that forces ci.yml to pin |
| 48 | + # its own node-gyp on Windows. |
| 49 | + - run: npm ci |
| 50 | + |
| 51 | + - name: Test |
| 52 | + run: npm test |
| 53 | + |
| 54 | + - name: Refuse to publish a version that already exists |
| 55 | + run: | |
| 56 | + NAME=$(node -p "require('./package.json').name") |
| 57 | + VERSION=$(node -p "require('./package.json').version") |
| 58 | + echo "Preparing $NAME@$VERSION" |
| 59 | + if npm view "$NAME@$VERSION" version >/dev/null 2>&1; then |
| 60 | + echo "::error::$NAME@$VERSION is already published. Bump the version first." |
| 61 | + exit 1 |
| 62 | + fi |
| 63 | +
|
| 64 | + # This package builds from source on the user's machine, so the vendored Lua |
| 65 | + # and LuaFileSystem sources are not an optional extra — a tarball missing them |
| 66 | + # is unbuildable for everyone who installs it, and npm versions are immutable. |
| 67 | + # Cheapest possible check against the most expensive possible mistake. |
| 68 | + - name: Verify the tarball can actually build |
| 69 | + run: | |
| 70 | + npm pack --dry-run --json > pack.json |
| 71 | + node -e " |
| 72 | + // npm 11 and earlier emit an array of results; npm 12 emits an object |
| 73 | + // keyed by package name. Accept either, so an npm upgrade cannot turn |
| 74 | + // this guard into a crash — or, worse, into a silent pass. |
| 75 | + const raw = require('./pack.json'); |
| 76 | + const entry = Array.isArray(raw) ? raw[0] : Object.values(raw)[0]; |
| 77 | + if (!entry || !Array.isArray(entry.files)) { |
| 78 | + console.error('could not read the file list from npm pack --json'); |
| 79 | + process.exit(1); |
| 80 | + } |
| 81 | + const files = entry.files.map(f => f.path); |
| 82 | +
|
| 83 | + const needed = ['index.js', 'binding.gyp', 'src/luastate.cc', 'src/nodelua.cc', 'src/utils.cc', 'vendor/lfs/lfs.c', 'README.md', 'LICENSE.md']; |
| 84 | + const missing = needed.filter(n => !files.includes(n)); |
| 85 | + if (missing.length) { |
| 86 | + console.error('missing from tarball:', missing.join(', ')); |
| 87 | + process.exit(1); |
| 88 | + } |
| 89 | +
|
| 90 | + // Lua 5.1.5 is 29 translation units; a partial copy links with undefined |
| 91 | + // symbols rather than failing loudly at pack time. |
| 92 | + const lua = files.filter(f => /^vendor\/lua\/.+\.c\$/.test(f)); |
| 93 | + if (lua.length !== 29) { |
| 94 | + console.error('expected 29 vendored Lua sources, found ' + lua.length); |
| 95 | + process.exit(1); |
| 96 | + } |
| 97 | +
|
| 98 | + // Build output is platform-specific and must never ship; node_modules |
| 99 | + // would bloat the tarball and shadow the consumer's own tree. |
| 100 | + const leaked = files.filter(f => /^(build|node_modules|test|examples)\//.test(f)); |
| 101 | + if (leaked.length) { |
| 102 | + console.error('unexpected files in tarball:', leaked.join(', ')); |
| 103 | + process.exit(1); |
| 104 | + } |
| 105 | +
|
| 106 | + console.log(files.length + ' files, all expected'); |
| 107 | + " |
| 108 | +
|
| 109 | + - name: Pack (dry run) |
| 110 | + if: ${{ github.event_name == 'workflow_dispatch' && inputs.dry_run }} |
| 111 | + run: npm publish --dry-run |
| 112 | + |
| 113 | + # No NODE_AUTH_TOKEN. Publishing uses npm trusted publishing (OIDC): npm |
| 114 | + # verifies this workflow's identity against the trusted publisher configured on |
| 115 | + # the package, so there is no long-lived token to leak or rotate. |
| 116 | + # |
| 117 | + # Provenance is automatic under OIDC for a public package from a public repo, |
| 118 | + # so --provenance is not passed explicitly. |
| 119 | + # On a release event inputs.dry_run is undefined, so this must not rely on |
| 120 | + # negating it — an undefined input would otherwise read as "not a dry run" by |
| 121 | + # luck rather than intent. |
| 122 | + - name: Publish |
| 123 | + if: ${{ github.event_name == 'release' || !inputs.dry_run }} |
| 124 | + run: npm publish --access public |
| 125 | + |
| 126 | + # No tagging step: release-please already created the tag and the GitHub |
| 127 | + # Release that triggered this run. |
0 commit comments