Skip to content

Commit 20141c1

Browse files
authored
Add trusted publishing (#791)
1 parent bfac6b0 commit 20141c1

2 files changed

Lines changed: 48 additions & 1 deletion

File tree

.github/actions/setup/action.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ runs:
77
with:
88
node-version: 22
99
cache: yarn
10+
registry-url: 'https://registry.npmjs.org'
1011

1112
- name: Install dependencies
1213
run: yarn --frozen-lockfile --prefer-offline --network-concurrency 1

.github/workflows/publish.yml

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ jobs:
1111
permissions:
1212
contents: write
1313
pull-requests: write
14+
id-token: write
1415
runs-on: ubuntu-latest
1516
environment: publish
1617
steps:
@@ -20,6 +21,51 @@ jobs:
2021
ref: ${{ github.ref }}
2122
- name: Set up environment
2223
uses: ./.github/actions/setup
24+
- name: Install npm 11 for OIDC trusted publishing
25+
run: npm install -g npm@11.12.1
26+
- name: Check for new packages
27+
id: check-packages
28+
run: |
29+
: > "$RUNNER_TEMP/new_packages.txt"
30+
while IFS=: read -r pkg dir; do
31+
npm_stderr_file="$RUNNER_TEMP/npm-view-${pkg//[^a-zA-Z0-9]/_}.stderr"
32+
if npm view "$pkg" version > /dev/null 2> "$npm_stderr_file"; then
33+
echo "Existing package: $pkg"
34+
rm -f "$npm_stderr_file"
35+
continue
36+
fi
37+
38+
npm_error="$(tr '\n' ' ' < "$npm_stderr_file")"
39+
rm -f "$npm_stderr_file"
40+
41+
if [[ "$npm_error" == *"E404"* || "$npm_error" == *"404"* || "$npm_error" == *"Not Found"* ]]; then
42+
echo "New package detected: $pkg ($dir)"
43+
echo "$dir" >> "$RUNNER_TEMP/new_packages.txt"
44+
else
45+
echo "::error::npm view failed for $pkg: ${npm_error:-Unknown error}"
46+
exit 1
47+
fi
48+
done < <(yarn workspaces --json info | node -e "
49+
const info = JSON.parse(JSON.parse(require('fs').readFileSync('/dev/stdin','utf8')).data);
50+
for (const [name, meta] of Object.entries(info)) {
51+
const pkgJson = require('./' + meta.location + '/package.json');
52+
if (!pkgJson.private) console.log(name + ':' + meta.location);
53+
}
54+
")
55+
56+
has_new_packages=false
57+
if [ -s "$RUNNER_TEMP/new_packages.txt" ]; then
58+
echo "::notice::New packages detected — will use NPM token for publish"
59+
has_new_packages=true
60+
else
61+
echo "All packages exist on npm — using OIDC trusted publishing"
62+
fi
63+
echo "has_new_packages=$has_new_packages" >> "$GITHUB_OUTPUT"
64+
- name: Enable NPM token publishing
65+
if: steps.check-packages.outputs.has_new_packages == 'true'
66+
run: echo "NODE_AUTH_TOKEN=${NPM_TOKEN}" >> "$GITHUB_ENV"
67+
env:
68+
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
2369
- name: Create Prepare Release PR or Publish
2470
id: changesets
2571
uses: changesets/action@e0145edc7d9d8679003495b11f87bd8ef63c0cba # v1.5.3
@@ -31,7 +77,7 @@ jobs:
3177
commitMode: github-api
3278
env:
3379
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
34-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
80+
NPM_CONFIG_PROVENANCE: true
3581
- name: Check changesets status
3682
if: steps.changesets.outputs.hasChangesets == 'true'
3783
run: |

0 commit comments

Comments
 (0)