|
| 1 | +# Publishes @agentruntimecontrolprotocol/arcp to npm after the `test` workflow |
| 2 | +# succeeds on main, but only when package.json `version` differs from the |
| 3 | +# version currently published on npm. |
| 4 | +# |
| 5 | +# Required repo configuration: |
| 6 | +# - Secret: NPM_TOKEN (npm automation token with publish rights to the |
| 7 | +# @agentruntimecontrolprotocol scope) |
| 8 | +# - Settings > Actions > General > Workflow permissions: "Read and write" |
| 9 | +# is NOT required; this workflow only needs id-token:write (set below) |
| 10 | +# for npm provenance. |
| 11 | + |
| 12 | +name: publish |
| 13 | + |
| 14 | +on: |
| 15 | + workflow_run: |
| 16 | + workflows: ["test"] |
| 17 | + types: [completed] |
| 18 | + branches: [main] |
| 19 | + workflow_dispatch: |
| 20 | + |
| 21 | +concurrency: |
| 22 | + group: publish-${{ github.ref }} |
| 23 | + cancel-in-progress: false |
| 24 | + |
| 25 | +jobs: |
| 26 | + publish: |
| 27 | + name: publish to npm |
| 28 | + runs-on: ubuntu-latest |
| 29 | + # Only run if the test workflow succeeded (or this was manually dispatched). |
| 30 | + if: > |
| 31 | + github.event_name == 'workflow_dispatch' || |
| 32 | + (github.event.workflow_run.conclusion == 'success' && |
| 33 | + github.event.workflow_run.head_branch == 'main') |
| 34 | + permissions: |
| 35 | + contents: read |
| 36 | + id-token: write # required for npm provenance |
| 37 | + steps: |
| 38 | + - name: Checkout |
| 39 | + uses: actions/checkout@v4 |
| 40 | + with: |
| 41 | + # For workflow_run, check out the exact commit that passed CI. |
| 42 | + ref: ${{ github.event.workflow_run.head_sha || github.sha }} |
| 43 | + fetch-depth: 1 |
| 44 | + |
| 45 | + - name: Setup pnpm |
| 46 | + # pnpm/action-setup v4.0.0 |
| 47 | + uses: pnpm/action-setup@a7487c7e89a18df4991f7f222e4898a00d66ddda # v4.0.0 |
| 48 | + with: |
| 49 | + version: 9.15.0 |
| 50 | + run_install: false |
| 51 | + |
| 52 | + - name: Setup Node.js |
| 53 | + uses: actions/setup-node@v4 |
| 54 | + with: |
| 55 | + node-version: "22" |
| 56 | + cache: "pnpm" |
| 57 | + registry-url: "https://registry.npmjs.org" |
| 58 | + |
| 59 | + - name: Read local version |
| 60 | + id: local |
| 61 | + run: echo "version=$(node -p "require('./package.json').version")" >> "$GITHUB_OUTPUT" |
| 62 | + |
| 63 | + - name: Read published version |
| 64 | + id: published |
| 65 | + run: | |
| 66 | + name=$(node -p "require('./package.json').name") |
| 67 | + # `npm view` exits non-zero if the package has never been published. |
| 68 | + published=$(npm view "$name" version 2>/dev/null || echo "") |
| 69 | + echo "version=$published" >> "$GITHUB_OUTPUT" |
| 70 | +
|
| 71 | + - name: Decide whether to publish |
| 72 | + id: decide |
| 73 | + run: | |
| 74 | + if [ "${{ steps.local.outputs.version }}" = "${{ steps.published.outputs.version }}" ]; then |
| 75 | + echo "publish=false" >> "$GITHUB_OUTPUT" |
| 76 | + echo "Local version (${{ steps.local.outputs.version }}) matches npm; skipping publish." |
| 77 | + else |
| 78 | + echo "publish=true" >> "$GITHUB_OUTPUT" |
| 79 | + echo "Publishing ${{ steps.local.outputs.version }} (npm has '${{ steps.published.outputs.version }}')." |
| 80 | + fi |
| 81 | +
|
| 82 | + - name: Install dependencies |
| 83 | + if: steps.decide.outputs.publish == 'true' |
| 84 | + run: pnpm install --frozen-lockfile |
| 85 | + |
| 86 | + - name: Build |
| 87 | + if: steps.decide.outputs.publish == 'true' |
| 88 | + run: pnpm run build |
| 89 | + |
| 90 | + - name: Publish |
| 91 | + if: steps.decide.outputs.publish == 'true' |
| 92 | + env: |
| 93 | + NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} |
| 94 | + run: npm publish --access public --provenance |
0 commit comments