Skip to content

Commit 0650bb9

Browse files
authored
Merge pull request #46 from athombv/chore/oidc-npm-publish
chore: use OIDC trusted publisher for npm publish
2 parents 98a7e21 + 527a13e commit 0650bb9

1 file changed

Lines changed: 41 additions & 44 deletions

File tree

‎.github/workflows/publish-package-npm.yml‎

Lines changed: 41 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,47 @@
11
name: Publish to NPM
22

3-
# Version: 2.0.3
4-
# Modified: No
5-
# Requirements:
6-
# - Ensure you've run `npm version major|minor|patch` on the `master` branch before merging to `production`.
3+
# Publishes the package to npmjs.org using OIDC (Trusted Publisher).
74
#
8-
# This GitHub Workflow:
9-
# 1. [Optional] If `npm run build` exists. If so, it runs `npm ci` and `npm run build`.
10-
# 2. Publishes the package to the NPM registry.
5+
# Requirements:
6+
# - The `@athombv/data-types` package must have a Trusted Publisher
7+
# configured on npmjs.org pointing at this repository and workflow file.
8+
# - Bump the version on `master` before merging to `production`.
119
#
1210
# Secrets:
13-
# - HOMEY_GITHUB_ACTIONS_BOT_PERSONAL_ACCESS_TOKEN
14-
# - NPM_AUTH_TOKEN: required for publishing to npm
1511
# - SLACK_WEBHOOK_URL: required for posting a message in #software
12+
#
13+
# Steps:
14+
# 1. Verifies a `files` array is present in package.json.
15+
# 2. Installs dependencies and runs `npm run build` if it exists.
16+
# 3. On `workflow_dispatch`: runs `npm publish --dry-run`.
17+
# 4. On push to `production`: publishes to npmjs.org.
18+
# 5. On push to `testing`: publishes with `beta` tag.
1619

17-
# Note: when publishing a scoped npm package (e.g. @athombv/node-my-package),
18-
# add "publishConfig": { "access": "public" } to your package.json.
1920
on:
21+
workflow_dispatch:
2022
push:
2123
branches:
2224
- production
25+
- testing
26+
27+
permissions:
28+
id-token: write
29+
contents: read
2330

2431
jobs:
2532
publish:
2633
name: Publish
2734
runs-on: ubuntu-latest
2835
steps:
2936
- name: Checkout git repository
30-
uses: actions/checkout@v3
37+
uses: actions/checkout@v4
3138

32-
# Setup
33-
- name: Set up node 16 environment
34-
uses: actions/setup-node@v3
39+
- name: Set up node
40+
uses: actions/setup-node@v4
3541
with:
36-
node-version: 16
37-
registry-url: 'https://npm.pkg.github.com'
42+
# Node 24 bundles npm >= 11.5.1, required for OIDC `npm publish`.
43+
node-version: '24'
44+
registry-url: 'https://registry.npmjs.org'
3845

3946
# Sets package.json name & version to environment.
4047
- name: Get Package Info
@@ -45,50 +52,40 @@ jobs:
4552
VERSION="$(node -p "require('./package.json').version")"
4653
echo package_version=${VERSION} >> $GITHUB_ENV
4754
48-
# Ensure `packake.json .files` exists.
49-
- name: Verify
55+
- name: Verify files field in package.json
5056
run: |
51-
if ! cat package.json | jq -e .files; then
52-
echo "Missing 'files' array in package.json."
57+
if ! jq -e '(.files | type == "array") and (.files | length > 0)' package.json > /dev/null; then
58+
echo "package.json must declare a non-empty 'files' array."
5359
exit 1
5460
fi
5561
62+
- name: Install dependencies
63+
run: npm ci --ignore-scripts --audit=false
5664

57-
# Run `npm ci && npm run build` if it exists.
5865
- name: Build
5966
run: |
60-
if jq --exit-status '.scripts | has("build")' package.json; then
61-
echo "'npm run build' does exist. Building..."
62-
npm ci --ignore-scripts --audit=false
67+
if jq --exit-status '(.scripts // {}) | has("build")' package.json > /dev/null; then
68+
echo "Running npm run build..."
6369
npm run build
6470
else
65-
echo "'npm run build' does not exist. Skipping build..."
71+
echo "No build script; skipping."
6672
fi
67-
env:
68-
NODE_AUTH_TOKEN: ${{ secrets.HOMEY_GITHUB_ACTIONS_BOT_PERSONAL_ACCESS_TOKEN }}
6973
70-
# Configure NPM to use the NPM Registry
71-
- name: Configure NPM
72-
run: |
73-
npm config set registry https://registry.npmjs.org
74-
npm config set @athombv:registry https://registry.npmjs.org
75-
npm config set //registry.npmjs.org/:_authToken=${{ secrets.NPM_AUTH_TOKEN }} -q
74+
- name: Publish dry run
75+
if: github.event_name == 'workflow_dispatch'
76+
run: npm publish --dry-run --provenance
7677

77-
# Publish when this action is running on branch production.
7878
- name: Publish
79-
if: github.ref == 'refs/heads/production'
80-
run: |
81-
npm publish
79+
if: github.event_name == 'push' && github.ref == 'refs/heads/production'
80+
run: npm publish --provenance
8281

83-
# Publish to beta when this action is running on branch testing.
8482
- name: Publish (beta)
85-
if: github.ref == 'refs/heads/testing'
86-
run: |
87-
npm publish --tag beta
83+
if: github.event_name == 'push' && github.ref == 'refs/heads/testing'
84+
run: npm publish --provenance --tag beta
8885

89-
# Post a Slack notification on success/failure
86+
# Post a Slack notification on success/failure
9087
- name: Slack notify
91-
if: always()
88+
if: always() && github.event_name == 'push'
9289
uses: innocarpe/actions-slack@v1
9390
with:
9491
status: ${{ job.status }}

0 commit comments

Comments
 (0)