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 }}