|
1 | 1 | name: Release to npm |
2 | 2 |
|
3 | 3 | on: |
4 | | - pull_request: |
5 | | - types: |
6 | | - - closed |
7 | | - branches: |
8 | | - - main |
| 4 | + # Fire only after the "Tests" workflow completes |
| 5 | + workflow_run: |
| 6 | + workflows: ["Tests"] # must match your Tests workflow name exactly |
| 7 | + types: [completed] |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read # default for all jobs; least-privileged |
| 11 | + |
| 12 | +concurrency: |
| 13 | + group: release-${{ github.workflow }}-${{ github.run_id }} |
| 14 | + cancel-in-progress: false |
9 | 15 |
|
10 | 16 | jobs: |
11 | | - release: |
12 | | - if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release') |
| 17 | + publish: |
| 18 | + # Only proceed if: |
| 19 | + # - Tests concluded successfully |
| 20 | + # - Event was a push (not PR) to main |
| 21 | + # - The head repository is THIS repository (not a fork) |
| 22 | + if: > |
| 23 | + github.event.workflow_run.conclusion == 'success' && |
| 24 | + github.event.workflow_run.event == 'push' && |
| 25 | + github.event.workflow_run.head_branch == 'main' && |
| 26 | + github.event.workflow_run.head_repository.full_name == github.repository |
13 | 27 | runs-on: ubuntu-latest |
14 | 28 |
|
| 29 | + # Elevate only this job to get OIDC for npm provenance |
| 30 | + permissions: |
| 31 | + contents: read |
| 32 | + id-token: write |
| 33 | + |
| 34 | + environment: |
| 35 | + # Optional: protect with required reviewers (Settings → Environments → npm) |
| 36 | + name: npm |
| 37 | + |
15 | 38 | steps: |
16 | | - - name: Checkout Repository |
| 39 | + # 1) Sparse, read-only checkout of ONLY the files needed for publish. |
| 40 | + # No GitHub token persisted; avoids running arbitrary repo scripts. |
| 41 | + - name: Checkout (sparse, no creds) |
17 | 42 | uses: actions/checkout@v4 |
18 | 43 | with: |
19 | | - fetch-depth: 0 |
| 44 | + ref: ${{ github.event.workflow_run.head_sha }} |
| 45 | + fetch-depth: 1 |
| 46 | + persist-credentials: false |
| 47 | + sparse-checkout: | |
| 48 | + package.json |
| 49 | + README.md |
| 50 | + LICENSE |
20 | 51 |
|
21 | | - - name: Set up Node.js |
22 | | - uses: actions/setup-node@v4 |
| 52 | + # 2) Download the dist/ artifact produced by the Tests workflow build. |
| 53 | + - name: Download dist artifact |
| 54 | + uses: actions/download-artifact@v4 |
23 | 55 | with: |
24 | | - node-version: '20' |
25 | | - registry-url: 'https://registry.npmjs.org/' |
| 56 | + name: radui-ui-dist |
| 57 | + path: dist |
26 | 58 |
|
27 | | - - name: Install Dependencies |
28 | | - run: npm ci |
| 59 | + # 3) Hard-disable all npm lifecycle scripts to prevent code execution. |
| 60 | + - name: Disable npm scripts |
| 61 | + run: | |
| 62 | + npm config set ignore-scripts true |
| 63 | + echo "npm_config_ignore_scripts=true" >> $GITHUB_ENV |
29 | 64 |
|
30 | | - - name: Build Package |
31 | | - run: npm run build:rollup |
| 65 | + # 4) Read the version that Changesets bumped. |
| 66 | + - name: Read version |
| 67 | + id: pkg |
| 68 | + run: echo "version=$(node -p \"require('./package.json').version\")" >> $GITHUB_OUTPUT |
32 | 69 |
|
33 | | - - name: Get current version |
34 | | - id: package_version |
35 | | - run: echo "version=$(node -p "require('./package.json').version")" >> $GITHUB_OUTPUT |
| 70 | + # 5) Skip if this exact version is already on npm (idempotent reruns). |
| 71 | + - name: Check if version exists on npm |
| 72 | + id: exists |
| 73 | + run: | |
| 74 | + if npm view @radui/ui@${{ steps.pkg.outputs.version }} version >/dev/null 2>&1; then |
| 75 | + echo "exists=true" >> $GITHUB_OUTPUT |
| 76 | + else |
| 77 | + echo "exists=false" >> $GITHUB_OUTPUT |
| 78 | + fi |
36 | 79 |
|
| 80 | + # 6) Publish the already-tested artifact with provenance. |
| 81 | + # No install, no build, no scripts, minimal trust surface. |
37 | 82 | - name: Publish to npm |
38 | | - run: npm publish --access public |
| 83 | + if: steps.exists.outputs.exists == 'false' |
39 | 84 | env: |
40 | 85 | NODE_AUTH_TOKEN: ${{ secrets.NPM_AUTOMATION_TOKEN_FROM_KOTAPI }} |
| 86 | + run: npm publish --access public --provenance |
0 commit comments