From 5920d6d4563ef65600aca6b6252873d7f2e6ff99 Mon Sep 17 00:00:00 2001 From: Volodymyr Vreshch Date: Mon, 23 Nov 2026 01:26:23 +0100 Subject: [PATCH] ci(release): check the npm publisher before any tag A publish that npm refuses leaves a tag and images with no record, and that version is lost. The check asks npm first and stops the run. Claude-Session: https://claude.ai/code/session_01XQkodgygL4W8UcLQDwC212 --- .github/scripts/npm-publisher-check.sh | 25 +++++++++++++++++++++++++ .github/workflows/release.yml | 5 +++++ 2 files changed, 30 insertions(+) create mode 100755 .github/scripts/npm-publisher-check.sh diff --git a/.github/scripts/npm-publisher-check.sh b/.github/scripts/npm-publisher-check.sh new file mode 100755 index 0000000..edaea4b --- /dev/null +++ b/.github/scripts/npm-publisher-check.sh @@ -0,0 +1,25 @@ +#!/usr/bin/env bash +# Asks npm whether this workflow is the trusted publisher of each public package: `npm-publisher-check.sh ...`. +set -euo pipefail + +id_token() { + curl -fsS -H "Authorization: bearer $ACTIONS_ID_TOKEN_REQUEST_TOKEN" \ + "$ACTIONS_ID_TOKEN_REQUEST_URL&audience=npm:registry.npmjs.org" | jq -r .value +} + +bad=0 +for dir in "$@"; do + [ -f "$dir/package.json" ] || continue + [ "$(jq -r '.private // false' "$dir/package.json")" = "true" ] && continue + name=$(jq -r .name "$dir/package.json") + url="https://registry.npmjs.org/-/npm/v1/oidc/token/exchange/package/$(jq -rn --arg n "$name" '$n|@uri')" + status=$(curl -sS -o /dev/null -w '%{http_code}' -X POST -H "Authorization: Bearer $(id_token)" "$url") + if [ "$status" = "201" ] || [ "$status" = "200" ]; then + echo "PASS $name: npm takes this workflow as its publisher" + else + echo "FAIL $name: npm answered $status: set the trusted publisher of $name to $GITHUB_REPOSITORY and $(basename "${GITHUB_WORKFLOW_REF%@*}")" + echo "::error title=npm publisher of $name::npm answered $status. No tag was made." + bad=1 + fi +done +exit "$bad" diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index cace216..7fa33eb 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -7,6 +7,7 @@ name: Release on: push: branches: [master] + workflow_dispatch: # runs the checks again; an unchanged extension is not released concurrency: group: release @@ -68,6 +69,10 @@ jobs: env: GHCR_TOKEN: ${{ secrets.GITHUB_TOKEN }} run: printf '%s' "$GHCR_TOKEN" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin + # Before any tag: a publish that npm refuses would leave a tag and images with no record. + - name: npm takes this workflow as the publisher of each public package + if: steps.hold.outputs.any == 'true' + run: .github/scripts/npm-publisher-check.sh extensions/*/ - if: steps.hold.outputs.any == 'true' env: GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}