Skip to content

Commit 0b25742

Browse files
josephfuscoCopilot
andauthored
ci: Migrate npm publishing to OIDC and consolidate into reusable workflow (#2264)
* ci: add reusable npm publish workflow for OIDC trusted publishing npm only allows one trusted publisher (workflow filename) per package. This reusable workflow is called by both release-packages.yml and nightly-releases.yml so a single filename can be registered on npmjs.com. Accepts inputs for pre-build commands, publish commands, and optional tag pushing. Inherits permissions from the caller workflow. * ci: split release workflow into changesets and publish jobs Separates version management from publishing so the OIDC token is obtained right before changeset publish, avoiding token expiration during the build step. The publish job calls the reusable npm-publish workflow. * ci: migrate nightly releases to OIDC via reusable workflow Replace NPM_TOKEN auth with OIDC trusted publishing by calling the shared npm-publish workflow. Upgrades Node.js 18 to 22, removes .npmrc creation step, and adds workflow_dispatch trigger for manual testing. Adds optional package input to workflow_dispatch for publishing a single package (e.g., @faustwp/cli) to debug OIDC incrementally. * Update .github/workflows/release-packages.yml Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> * ci: upgrade npm to >=11.5.1 for OIDC trusted publishing --------- Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 5d9b5db commit 0b25742

3 files changed

Lines changed: 96 additions & 52 deletions

File tree

.github/workflows/nightly-releases.yml

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ name: Nightly Releases
1212
# * * * * *
1313

1414
on:
15+
workflow_dispatch:
16+
inputs:
17+
package:
18+
description: 'Specific package to publish (e.g., @faustwp/cli). Leave empty for all.'
19+
required: false
20+
default: ''
1521
schedule:
1622
# Monday-Friday at 8:35pm UTC / 3:35pm CST
1723

@@ -20,35 +26,16 @@ on:
2026
# middle of the hour, there is less chance of delay.
2127
- cron: '35 20 * * 1-5'
2228

29+
permissions:
30+
contents: read # For checking out the repo
31+
id-token: write # For npm OIDC authentication
32+
2333
jobs:
2434
release_nightly_canary:
2535
if: github.repository_owner == 'wpengine'
2636
name: Release Nightly Canary
27-
runs-on: ubuntu-22.04
28-
steps:
29-
- name: Checkout repo
30-
uses: actions/checkout@v4
31-
with:
32-
fetch-depth: 0
33-
34-
- name: Setup Node.js 18.x
35-
uses: actions/setup-node@v4
36-
with:
37-
node-version: 18.x
38-
39-
- name: Install Dependencies
40-
run: npm ci
41-
42-
- name: Create .npmrc
43-
run: |
44-
cat << EOF > "$HOME/.npmrc"
45-
//registry.npmjs.org/:_authToken=$NPM_TOKEN
46-
EOF
47-
env:
48-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
49-
50-
# https://github.com/atlassian/changesets/blob/master/docs/snapshot-releases.md
51-
- name: Release to NPM
52-
run: |
53-
npm run version:nightly
54-
npm run release:nightly
37+
# https://github.com/atlassian/changesets/blob/master/docs/snapshot-releases.md
38+
uses: ./.github/workflows/npm-publish.yml
39+
with:
40+
pre-build-command: npm run version:nightly
41+
publish-command: ${{ inputs.package && format('npm publish -w {0} --tag canary', inputs.package) || 'changeset publish --tag canary' }}

.github/workflows/npm-publish.yml

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
name: Publish to npm
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
pre-build-command:
7+
description: 'Command to run before building (e.g., snapshot versioning)'
8+
type: string
9+
required: false
10+
default: ''
11+
publish-command:
12+
description: 'Command to run for publishing'
13+
type: string
14+
required: true
15+
push-tags:
16+
description: 'Whether to push git tags after publishing'
17+
type: boolean
18+
required: false
19+
default: false
20+
21+
# No permissions block — inherits from caller workflow.
22+
# Release caller grants contents: write + id-token: write (enables git push and OIDC).
23+
# Nightly caller grants contents: read + id-token: write (enables checkout and OIDC).
24+
25+
jobs:
26+
publish:
27+
runs-on: ubuntu-22.04
28+
steps:
29+
- name: Checkout Repo
30+
uses: actions/checkout@v4
31+
with:
32+
fetch-depth: 0
33+
34+
- name: Setup Node.js 22.x
35+
uses: actions/setup-node@v4
36+
with:
37+
node-version: 22.x
38+
registry-url: 'https://registry.npmjs.org'
39+
40+
- name: Install Dependencies
41+
run: npm ci
42+
43+
- name: Upgrade npm for OIDC support
44+
run: npm install -g npm@^11.5.1
45+
46+
- name: Verify OIDC token availability
47+
run: |
48+
if [ -z "${ACTIONS_ID_TOKEN_REQUEST_URL}" ]; then
49+
echo "::error::OIDC token not available. Ensure the caller workflow has 'id-token: write' permission."
50+
exit 1
51+
fi
52+
echo "OIDC token endpoint available"
53+
54+
- name: Pre-build
55+
if: inputs.pre-build-command != ''
56+
run: ${{ inputs.pre-build-command }}
57+
58+
- name: Build packages
59+
run: npm run build
60+
61+
- name: Publish to npm
62+
run: ${{ inputs.publish-command }}
63+
64+
- name: Push tags
65+
if: inputs.push-tags
66+
run: git push --follow-tags

.github/workflows/release-packages.yml

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ on:
66
- canary
77

88
permissions:
9-
contents: write # For creating releases/tags
9+
contents: write # For creating releases/tags and pushing tags
1010
pull-requests: write # For creating version PRs
1111
id-token: write # For npm OIDC authentication
1212

1313
jobs:
14-
release_packages:
15-
name: Release Packages
14+
changesets:
15+
name: Manage Changesets
1616
runs-on: ubuntu-22.04
17+
outputs:
18+
has-changesets: ${{ steps.changesets.outputs.hasChangesets }}
1719
steps:
1820
- name: Checkout Repo
1921
uses: actions/checkout@v4
@@ -25,34 +27,23 @@ jobs:
2527
uses: actions/setup-node@v4
2628
with:
2729
node-version: 22.x
28-
registry-url: 'https://registry.npmjs.org'
2930

3031
- name: Install Dependencies
3132
run: npm ci
3233

33-
- name: Create Release Pull Request or Publish to npm
34+
- name: Create Release Pull Request
3435
id: changesets
3536
uses: changesets/action@v1
3637
with:
37-
# This expects you to have a script called release which does a build for your packages and calls changeset publish
38-
publish: npm run release
3938
version: npm run version
4039
env:
4140
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
42-
- name: Save Plugin version
43-
run: |
44-
json=${{ toJSON(steps.changesets.outputs.publishedPackages) }}
45-
echo PLUGIN_VERSION=$(echo "$json" | jq '.[] | select(.name == "@faustwp/wordpress-plugin") | .version') >> $GITHUB_ENV
46-
- name: Deploy WordPress plugin
47-
# Checks the changesets publishedPackages output
48-
# If there is a published package named "@faustwp/wordpress-plugin"
49-
# Then deploy the WordPress plugin
50-
# https://github.com/changesets/action#outputs
51-
if: steps.changesets.outputs.published && contains(steps.changesets.outputs.publishedPackages, '"@faustwp/wordpress-plugin"')
52-
uses: ./.github/actions/release-plugin
53-
with:
54-
github_token: ${{ secrets.GITHUB_TOKEN }}
55-
env:
56-
PLUGIN_DIR: plugins/faustwp
57-
SLUG: faustwp
58-
VERSION: ${{ env.PLUGIN_VERSION }}
41+
42+
publish:
43+
name: Publish to npm
44+
needs: changesets
45+
if: needs.changesets.outputs.has-changesets == 'false'
46+
uses: ./.github/workflows/npm-publish.yml
47+
with:
48+
publish-command: npx changeset publish
49+
push-tags: true

0 commit comments

Comments
 (0)