Skip to content

Commit e191567

Browse files
authored
ci: isolate npm publishing credentials from package builds (#45)
1 parent ff6464d commit e191567

1 file changed

Lines changed: 52 additions & 8 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 52 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,25 @@ on:
44
release:
55
types: [published]
66

7+
permissions: {}
8+
79
jobs:
8-
publish:
10+
build:
911
runs-on: ubuntu-latest
12+
timeout-minutes: 20
13+
permissions:
14+
contents: read
1015

1116
steps:
1217
- uses: actions/checkout@v4
18+
with:
19+
persist-credentials: false
1320

1421
- name: Setup Node.js
15-
uses: actions/setup-node@v4
22+
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
1623
with:
17-
node-version: '18'
24+
node-version: '24'
1825
cache: 'npm'
19-
registry-url: 'https://registry.npmjs.org'
2026

2127
- name: Validate version
2228
run: |
@@ -55,7 +61,45 @@ jobs:
5561
- name: Build widget
5662
run: npm run build:widget
5763

58-
- name: Publish to npm
59-
run: npm publish
60-
env:
61-
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
64+
- name: Pack npm artifact
65+
run: |
66+
mkdir -p "$RUNNER_TEMP/npm-package"
67+
npm pack --ignore-scripts --pack-destination "$RUNNER_TEMP/npm-package"
68+
69+
- name: Upload npm artifact
70+
uses: actions/upload-artifact@043fb46d1a93c77aae656e7c1c64a875d1fc6a0a # v7.0.1
71+
with:
72+
name: npm-package
73+
path: ${{ runner.temp }}/npm-package/*.tgz
74+
if-no-files-found: error
75+
retention-days: 1
76+
77+
publish:
78+
needs: build
79+
runs-on: ubuntu-latest
80+
timeout-minutes: 5
81+
permissions:
82+
id-token: write
83+
steps:
84+
# Keep package code and caches out of the job with publishing authority.
85+
- name: Set up Node.js
86+
uses: actions/setup-node@48b55a011bda9f5d6aeb4c2d9c7362e8dae4041e # v6.4.0
87+
with:
88+
node-version: '24'
89+
registry-url: 'https://registry.npmjs.org'
90+
package-manager-cache: false
91+
92+
- name: Download npm artifact
93+
uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8.0.1
94+
with:
95+
name: npm-package
96+
path: .release
97+
98+
- name: Publish npm artifact
99+
run: |
100+
tarballs=(.release/*.tgz)
101+
if [[ ${#tarballs[@]} -ne 1 || ! -f "${tarballs[0]}" ]]; then
102+
echo "::error::Expected exactly one npm package tarball."
103+
exit 1
104+
fi
105+
npm publish "${tarballs[0]}" --access public --ignore-scripts

0 commit comments

Comments
 (0)