|
| 1 | +# |
| 2 | +# Publish the gobl-worker package to NPM. Runs automatically after a |
| 3 | +# successful Release CLI run, and can be re-run manually (workflow_dispatch) |
| 4 | +# to retry a failed publish without re-running the full release. |
| 5 | +# |
| 6 | +# Split into two jobs so the privileged publish never executes repository |
| 7 | +# code (CodeQL "untrusted checkout in a privileged context"): |
| 8 | +# - build: unprivileged (contents: read, no id-token / secrets). Checks |
| 9 | +# out the released commit, builds the package, and packs a tarball. |
| 10 | +# - publish: privileged (id-token: write for NPM OIDC trusted publishing). |
| 11 | +# Only downloads the pre-built tarball and publishes it with |
| 12 | +# --ignore-scripts; it never checks out or runs repo code. |
| 13 | +# |
| 14 | +name: Publish NPM |
| 15 | + |
| 16 | +on: |
| 17 | + workflow_run: |
| 18 | + workflows: ["Release CLI"] |
| 19 | + types: [completed] |
| 20 | + workflow_dispatch: |
| 21 | + |
| 22 | +permissions: |
| 23 | + contents: read |
| 24 | + |
| 25 | +jobs: |
| 26 | + build: |
| 27 | + name: Build gobl-worker |
| 28 | + # Run on a successful upstream Release CLI (restricted to main, the only |
| 29 | + # branch Release CLI runs on), or whenever triggered manually. |
| 30 | + if: ${{ github.event_name == 'workflow_dispatch' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.head_branch == 'main') }} |
| 31 | + runs-on: ubuntu-latest |
| 32 | + permissions: |
| 33 | + contents: read # no secrets / id-token here, so building the code is safe |
| 34 | + outputs: |
| 35 | + tag: ${{ steps.npm-tag.outputs.tag }} |
| 36 | + steps: |
| 37 | + # No explicit `ref`: for workflow_run this is the default branch and for |
| 38 | + # workflow_dispatch the selected ref. Referencing |
| 39 | + # github.event.workflow_run.head_sha here would count as untrusted data |
| 40 | + # for CodeQL (cache-poisoning/poisonable-step); the released commit is |
| 41 | + # pinned via its tag in the next step instead. |
| 42 | + - name: Checkout |
| 43 | + uses: actions/checkout@v4 |
| 44 | + with: |
| 45 | + fetch-depth: "0" # need tags to resolve the released version |
| 46 | + persist-credentials: false |
| 47 | + |
| 48 | + - name: Resolve released version |
| 49 | + id: version |
| 50 | + run: | |
| 51 | + version="$(git describe --tags --abbrev=0)" |
| 52 | + echo "Building gobl-worker $version" |
| 53 | + # Build exactly the released commit, even if the branch has moved on. |
| 54 | + git checkout --detach "refs/tags/${version}" |
| 55 | + echo "version=$version" >> "$GITHUB_OUTPUT" |
| 56 | +
|
| 57 | + - name: Set up Go |
| 58 | + uses: actions/setup-go@v5 |
| 59 | + with: |
| 60 | + go-version-file: "go.mod" |
| 61 | + # workflow_run executes in the default branch's cache scope; with |
| 62 | + # caching on, the npm build steps below could poison caches restored |
| 63 | + # by privileged workflows (CodeQL cache-poisoning/poisonable-step). |
| 64 | + cache: false |
| 65 | + |
| 66 | + # wasm_exec.js is gitignored; copy it from the active Go install before |
| 67 | + # the npm build bundles it. |
| 68 | + - name: Copy Go WASM Exec |
| 69 | + run: cp "$(go env GOROOT)/lib/wasm/wasm_exec.js" ./wasm/worker/src |
| 70 | + |
| 71 | + - name: Install Node.js |
| 72 | + uses: actions/setup-node@v4 |
| 73 | + with: |
| 74 | + node-version: 22.x |
| 75 | + |
| 76 | + - name: Build and pack gobl-worker |
| 77 | + working-directory: ./wasm/worker |
| 78 | + run: | |
| 79 | + npm ci |
| 80 | + npm version --no-git-tag-version "${{ steps.version.outputs.version }}" |
| 81 | + npm run build |
| 82 | + npm pack |
| 83 | +
|
| 84 | + # npm refuses to publish a prerelease to the default `latest` dist-tag; |
| 85 | + # derive `rc`/`beta`/etc. from the version, else `latest`. |
| 86 | + - name: Determine npm dist-tag |
| 87 | + id: npm-tag |
| 88 | + run: | |
| 89 | + v="${{ steps.version.outputs.version }}" |
| 90 | + if [[ "$v" =~ -([a-z]+) ]]; then |
| 91 | + tag="${BASH_REMATCH[1]}" |
| 92 | + else |
| 93 | + tag="latest" |
| 94 | + fi |
| 95 | + echo "Using npm dist-tag: $tag" |
| 96 | + echo "tag=$tag" >> "$GITHUB_OUTPUT" |
| 97 | +
|
| 98 | + - name: Upload package tarball |
| 99 | + uses: actions/upload-artifact@v4 |
| 100 | + with: |
| 101 | + name: gobl-worker-pkg |
| 102 | + path: ./wasm/worker/*.tgz |
| 103 | + if-no-files-found: error |
| 104 | + |
| 105 | + publish: |
| 106 | + name: Publish to NPM |
| 107 | + needs: build |
| 108 | + runs-on: ubuntu-latest |
| 109 | + permissions: |
| 110 | + id-token: write # NPM OIDC trusted publishing |
| 111 | + contents: read |
| 112 | + steps: |
| 113 | + - name: Download package tarball |
| 114 | + uses: actions/download-artifact@v4 |
| 115 | + with: |
| 116 | + name: gobl-worker-pkg |
| 117 | + |
| 118 | + - name: Install Node.js |
| 119 | + uses: actions/setup-node@v4 |
| 120 | + with: |
| 121 | + node-version: 22.x |
| 122 | + # Intentionally no `registry-url`: setup-node would otherwise write |
| 123 | + # a `.npmrc` with an `_authToken` entry that blocks the OIDC |
| 124 | + # trusted-publisher exchange. |
| 125 | + |
| 126 | + # npx fetches npm 11 (required for OIDC trusted publishing) without |
| 127 | + # touching the global install. The prebuilt tarball is published with |
| 128 | + # scripts disabled, so no repository code runs in this privileged job. |
| 129 | + - name: Publish to NPM |
| 130 | + run: npx -y npm@11 publish ./*.tgz --tag "${{ needs.build.outputs.tag }}" --ignore-scripts |
0 commit comments