From 910b45c9af24e1427500031b73d6f7b8e657ba88 Mon Sep 17 00:00:00 2001 From: ktechmidas <9920871+ktechmidas@users.noreply.github.com> Date: Fri, 20 Nov 2026 23:34:28 +0300 Subject: [PATCH] ci: publish to npm with trusted publishing The release job published with the org NPM_TOKEN secret, which npm rejects (E404 on the PUT), so v7.0.6 never reached npm. Publish through npm trusted publishing instead, the way dashcore-lib does: the job gets an OIDC token and npm exchanges it for a publish token. No secret is involved. - The job needs id-token: write, npm 11.5.1 or later and no stored token. - Only GitHub's own actions run in it, pinned to commits. - package.json's repository now names dashpay/dashcore-node. npm checks it against the provenance attestation that trusted publishing adds. - A manual run of the workflow rehearses the publish with --dry-run and fails if npm does not accept the workflow as a trusted publisher. Co-Authored-By: Claude Fable 5.2 --- .github/workflows/test_and_release.yml | 87 +++++++++++++++++--------- package.json | 5 +- 2 files changed, 63 insertions(+), 29 deletions(-) diff --git a/.github/workflows/test_and_release.yml b/.github/workflows/test_and_release.yml index 012c612f3..4e5db854c 100644 --- a/.github/workflows/test_and_release.yml +++ b/.github/workflows/test_and_release.yml @@ -46,42 +46,73 @@ jobs: name: Release NPM package runs-on: ubuntu-24.04 needs: test - if: ${{ github.event_name == 'release' }} + # A release publishes the package. A manual run only rehearses the publish: + # it packs the package and checks that npm accepts this workflow as a + # trusted publisher, and publishes nothing. + if: ${{ github.event_name == 'release' || github.event_name == 'workflow_dispatch' }} + # npm trusted publishing: npm exchanges the job's OIDC token for a publish + # token, so no NPM_TOKEN secret is needed + permissions: + contents: read + id-token: write + # Every step here can request an npm publish token, so only GitHub's own + # actions run, pinned to commits, and the checks are plain shell steps: - - uses: actions/checkout@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 - - uses: actions/setup-node@v4 + # Trusted publishing needs npm 11.5.1 or later. No registry-url: with it, + # setup-node writes a token entry to .npmrc and npm tries token auth + # instead of the OIDC exchange. + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: - node-version: '20' + node-version: '24.14.1' - name: Check package version matches tag - uses: geritol/match-tag-to-package-version@0.1.0 + if: ${{ github.event_name == 'release' }} env: - TAG_PREFIX: refs/tags/v - - - name: Enable NPM cache - uses: actions/cache@v4 - with: - path: '~/.npm' - key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} - restore-keys: | - ${{ runner.os }}-node- - - - name: Install NPM dependencies - run: npm ci + RELEASE_TAG: ${{ github.event.release.tag_name }} + run: | + VERSION=$(node -p "require('./package.json').version") + if [ "$RELEASE_TAG" != "v$VERSION" ]; then + echo "::error::Release tag $RELEASE_TAG does not match package version v$VERSION" + exit 1 + fi + # X.Y-dev for dev releases, latest otherwise - name: Set release tag - uses: actions/github-script@v7 id: tag - with: - result-encoding: string - script: | - const tag = context.payload.release.tag_name; - const [, major, minor] = tag.match(/^v([0-9]+)\.([0-9]+)/); - return (tag.includes('dev') ? `${major}.${minor}-dev` : 'latest'); + if: ${{ github.event_name == 'release' }} + env: + RELEASE_TAG: ${{ github.event.release.tag_name }} + run: | + if [[ "$RELEASE_TAG" == *dev* ]]; then + [[ "$RELEASE_TAG" =~ ^v([0-9]+)\.([0-9]+) ]] + echo "result=${BASH_REMATCH[1]}.${BASH_REMATCH[2]}-dev" >> "$GITHUB_OUTPUT" + else + echo "result=latest" >> "$GITHUB_OUTPUT" + fi + # Packs the checkout as earlier releases did. The package has no build + # step, and nothing is installed in this job, so no package script runs + # while the OIDC token is available. Trusted publishing adds a provenance + # attestation, which requires package.json's repository.url to name this + # repository. - name: Publish NPM package - uses: JS-DevTools/npm-publish@v3 - with: - token: ${{ secrets.NPM_TOKEN }} - tag: ${{ steps.tag.outputs.result }} + if: ${{ github.event_name == 'release' }} + env: + NPM_TAG: ${{ steps.tag.outputs.result }} + run: npm publish --ignore-scripts --access public --tag "$NPM_TAG" + + # npm does the OIDC exchange on a dry run too, and without a token a dry + # run only warns, so the result is read from npm's verbose log. The text + # matched is the one printed by the npm that ships with the Node version + # pinned above. + - name: Rehearse the publish + if: ${{ github.event_name == 'workflow_dispatch' }} + run: | + LOG="$RUNNER_TEMP/npm-publish-dry-run.log" + npm publish --dry-run --ignore-scripts --access public --loglevel verbose 2>&1 | tee "$LOG" + if ! grep -q 'oidc Successfully retrieved and set token' "$LOG"; then + echo "::error::npm did not accept this workflow as a trusted publisher of this package. See the oidc lines above." + exit 1 + fi diff --git a/package.json b/package.json index 1dec0b08e..985cd885b 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,10 @@ "author": "BitPay ", "version": "7.0.6", "main": "./index.js", - "repository": "git://github.com/dashevo/dashcore-node.git", + "repository": { + "type": "git", + "url": "git+https://github.com/dashpay/dashcore-node.git" + }, "homepage": "https://github.com/dashevo/dashcore-node", "bugs": { "url": "https://github.com/dashevo/dashcore-node/issues"