From bca3bd7b8134ccadf4109d001e539c56558fe84b Mon Sep 17 00:00:00 2001 From: Yuqing Bai Date: Thu, 5 Nov 2026 09:47:19 +0800 Subject: [PATCH] fix: scope CI tokens and validate releases before npm publish --- .github/workflows/integration-test-macos.yml | 9 +++++++-- .github/workflows/integration-test.yml | 15 +++++++++++---- .github/workflows/lint.yml | 11 ++++++++--- .github/workflows/publish.yml | 18 +++++++++++++++--- .github/workflows/unit-test.yml | 11 ++++++++--- docs/dev-guide.md | 4 ++-- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/.github/workflows/integration-test-macos.yml b/.github/workflows/integration-test-macos.yml index 6779dda..eb20776 100644 --- a/.github/workflows/integration-test-macos.yml +++ b/.github/workflows/integration-test-macos.yml @@ -3,12 +3,17 @@ name: Integration Test on MacOS on: workflow_dispatch: +permissions: + contents: read + jobs: test: runs-on: macos-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: node-version: 18 cache: 'npm' diff --git a/.github/workflows/integration-test.yml b/.github/workflows/integration-test.yml index 70f75bd..8081934 100644 --- a/.github/workflows/integration-test.yml +++ b/.github/workflows/integration-test.yml @@ -5,12 +5,17 @@ on: branches: [ main ] workflow_dispatch: +permissions: + contents: read + jobs: test-ubuntu: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: node-version: 18 cache: 'npm' @@ -25,8 +30,10 @@ jobs: if: ${{ always() }} needs: test-ubuntu steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: node-version: 18 cache: 'npm' diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e63047e..5acbeab 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -6,13 +6,18 @@ on: pull_request: branches: [ main ] +permissions: + contents: read + jobs: lint: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - - uses: actions/cache@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index 38b06b3..987b649 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -4,15 +4,27 @@ on: release: types: [published] +permissions: + contents: read + jobs: publish-npm: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + ref: ${{ github.sha }} + fetch-depth: 0 + - name: Verify release commit is on main + run: git merge-base --is-ancestor HEAD origin/main + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 with: + node-version: 24 registry-url: 'https://registry.npmjs.org' - run: npm ci - - run: npm publish --access public + - run: npm run lint + - run: npm test + - run: npm publish --access public --ignore-scripts env: NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml index 03d22c5..3d4501d 100644 --- a/.github/workflows/unit-test.yml +++ b/.github/workflows/unit-test.yml @@ -6,13 +6,18 @@ on: pull_request: branches: [ main ] +permissions: + contents: read + jobs: test: runs-on: ubuntu-latest steps: - - uses: actions/checkout@v4 - - uses: actions/setup-node@v4 - - uses: actions/cache@v4 + - uses: actions/checkout@11d5960a326750d5838078e36cf38b85af677262 # v4 + with: + persist-credentials: false + - uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + - uses: actions/cache@0057852bfaa89a56745cba8c7296529d2fc39830 # v4 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }} diff --git a/docs/dev-guide.md b/docs/dev-guide.md index f8790fa..655d69d 100644 --- a/docs/dev-guide.md +++ b/docs/dev-guide.md @@ -1,5 +1,5 @@ # How to release 1. Update the version in `package.json` and run `npm run version`. Then merge the PR. -2. Publish a new GitHub release. -3. The npm pacakge will be published automatically. +2. Publish a new GitHub release pointing to a commit already merged into `main`. +3. The workflow checks that the release commit is on `main`, then runs lint and unit tests before publishing the npm package. `npm test` builds the package; publish lifecycle scripts are disabled so the build does not run with `NPM_TOKEN`.