From 93e4b44fb9005e717b697b1dc4c19268f69c38c1 Mon Sep 17 00:00:00 2001 From: Savio Dias Date: Thu, 26 Nov 2026 11:06:07 +0530 Subject: [PATCH] ci: publish to npm through trusted publishing Add publish.yml, which publishes with OIDC and no token. The release and beta workflows dispatch it and wait for the run instead of publishing with NPM_TOKEN. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/beta-release.yml | 18 ++++----- .github/workflows/npm-publish.yml | 12 ++++-- .github/workflows/publish.yml | 63 ++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+), 13 deletions(-) create mode 100644 .github/workflows/publish.yml diff --git a/.github/workflows/beta-release.yml b/.github/workflows/beta-release.yml index 5a64813f..988c754e 100644 --- a/.github/workflows/beta-release.yml +++ b/.github/workflows/beta-release.yml @@ -5,7 +5,7 @@ on: permissions: contents: read - id-token: write + actions: write jobs: publish-beta: @@ -20,7 +20,6 @@ jobs: uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24.22.3 - registry-url: "https://registry.npmjs.org/" - name: "Install dependencies" run: npm ci @@ -63,12 +62,13 @@ jobs: fi echo "Beta version $BETA_VERSION is available for release" - - name: "Update package.json with beta version" - run: | - npm version ${{ steps.get_version.outputs.version }} --no-git-tag-version - - name: "Publish beta to NPM" - run: npm publish --tag beta --access public --provenance env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} - + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: ${{ steps.get_version.outputs.version }} + run: | + RUN_ID=$(gh api -X POST "repos/$GITHUB_REPOSITORY/actions/workflows/publish.yml/dispatches" \ + -f ref="$GITHUB_REF_NAME" -F return_run_details=true \ + -f "inputs[ref]=$GITHUB_SHA" -f "inputs[version]=$VERSION" -f "inputs[tag]=beta" \ + --jq .workflow_run_id) + gh run watch "$RUN_ID" --repo "$GITHUB_REPOSITORY" --exit-status diff --git a/.github/workflows/npm-publish.yml b/.github/workflows/npm-publish.yml index ccfd597d..b7655fe0 100644 --- a/.github/workflows/npm-publish.yml +++ b/.github/workflows/npm-publish.yml @@ -5,7 +5,6 @@ on: permissions: contents: write - id-token: write actions: write jobs: @@ -21,7 +20,6 @@ jobs: uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 with: node-version: 24.22.3 - registry-url: "https://registry.npmjs.org/" - name: "Install dependencies" run: npm ci @@ -101,9 +99,15 @@ jobs: run: git push origin ${{ steps.get_version.outputs.version }} - name: "Publish to NPM" - run: npm publish --access public --provenance env: - NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} + VERSION: ${{ steps.get_version.outputs.version }} + run: | + RUN_ID=$(gh api -X POST "repos/$GITHUB_REPOSITORY/actions/workflows/publish.yml/dispatches" \ + -f ref="$GITHUB_REF_NAME" -F return_run_details=true \ + -f "inputs[ref]=$VERSION" -f "inputs[version]=${VERSION#v}" -f "inputs[tag]=latest" \ + --jq .workflow_run_id) + gh run watch "$RUN_ID" --repo "$GITHUB_REPOSITORY" --exit-status - name: "Create GitHub Release" env: diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml new file mode 100644 index 00000000..5c1a6f1f --- /dev/null +++ b/.github/workflows/publish.yml @@ -0,0 +1,63 @@ +name: "Publish to NPM" + +on: + workflow_dispatch: + inputs: + ref: + description: "Git tag or commit" + required: true + type: string + version: + description: "Version to publish" + required: true + type: string + tag: + description: "npm dist-tag (latest | beta | tp-canary)" + required: true + type: string + +permissions: + contents: read + +jobs: + publish: + runs-on: ubuntu-latest + permissions: + contents: read + id-token: write + env: + PACKAGE_NAME: "@browserstack/mcp-server" + VERSION: ${{ inputs.version }} + TAG: ${{ inputs.tag }} + steps: + - name: "Checkout source code" + uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4.4.0 + with: + ref: ${{ inputs.ref }} + + - name: "Set up Node.js" + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4.4.0 + with: + node-version: 24.22.3 + + - name: "Install npm with trusted publishing support" + run: npm install -g npm@11.21.0 + + - name: "Install dependencies" + run: npm ci + + - name: "Create build" + run: npm run build + + - name: "Verify name and set version" + run: | + [ "$(node -p 'require("./package.json").name')" = "$PACKAGE_NAME" ] || { echo "::error::wrong package"; exit 1; } + BASE="$(node -p 'require("./package.json").version')" + case "$VERSION" in + "$BASE") ;; + "$BASE"-*) npm version "$VERSION" --no-git-tag-version ;; + *) echo "::error::version $VERSION does not match package.json $BASE"; exit 1 ;; + esac + + - name: "Publish" + run: npm publish --tag "$TAG" --access public