Publish to npm #2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Publish to npm | |
| on: | |
| workflow_run: | |
| workflows: ["Release Please"] | |
| types: | |
| - completed | |
| permissions: | |
| contents: read | |
| id-token: write | |
| jobs: | |
| publish: | |
| name: Publish | |
| runs-on: ubuntu-latest | |
| if: ${{ github.event.workflow_run.conclusion == 'success' }} | |
| steps: | |
| - name: Check for new release | |
| id: check | |
| uses: actions/github-script@v7 | |
| with: | |
| script: | | |
| const { data: releases } = await github.rest.repos.listReleases({ | |
| owner: context.repo.owner, | |
| repo: context.repo.repo, | |
| per_page: 1, | |
| }); | |
| const release = releases[0]; | |
| if (!release || release.draft) { | |
| core.setOutput('should_publish', 'false'); | |
| return; | |
| } | |
| // Only publish if the release was created in the last 10 minutes | |
| const created = new Date(release.created_at); | |
| const now = new Date(); | |
| const diffMinutes = (now - created) / 60000; | |
| if (diffMinutes > 10) { | |
| core.setOutput('should_publish', 'false'); | |
| return; | |
| } | |
| core.setOutput('should_publish', 'true'); | |
| core.setOutput('tag_name', release.tag_name); | |
| - name: Checkout | |
| if: steps.check.outputs.should_publish == 'true' | |
| uses: actions/checkout@v4 | |
| with: | |
| ref: ${{ steps.check.outputs.tag_name }} | |
| - name: Setup Node | |
| if: steps.check.outputs.should_publish == 'true' | |
| uses: actions/setup-node@v4 | |
| with: | |
| node-version: "24" | |
| registry-url: "https://registry.npmjs.org" | |
| - name: Setup Bun | |
| if: steps.check.outputs.should_publish == 'true' | |
| uses: oven-sh/setup-bun@v2 | |
| with: | |
| bun-version: latest | |
| - name: Install dependencies | |
| if: steps.check.outputs.should_publish == 'true' | |
| run: bun install --frozen-lockfile | |
| - name: Build | |
| if: steps.check.outputs.should_publish == 'true' | |
| run: bun run build | |
| - name: Test | |
| if: steps.check.outputs.should_publish == 'true' | |
| run: bun run test | |
| - name: Publish to npm | |
| if: steps.check.outputs.should_publish == 'true' | |
| run: npm publish | |
| env: | |
| NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |