Skip to content

Commit eafcb5e

Browse files
dahliaclaude
andcommitted
Extract npm publish to reusable build.yaml workflow
This allows both main.yaml and publish-pr.yaml to use npm trusted publishing through OIDC, as npm only allows one trusted publisher workflow per package. By naming the reusable workflow build.yaml, legacy maintenance branches can continue to publish without changes. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent c2f65d8 commit eafcb5e

3 files changed

Lines changed: 66 additions & 17 deletions

File tree

.github/workflows/build.yaml

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
name: Publish to npm (reusable)
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
tag:
7+
description: 'npm dist-tag to use (e.g., "latest", "dev", "pr-123")'
8+
required: true
9+
type: string
10+
package_pattern:
11+
description: 'Glob pattern for package tarballs to publish'
12+
required: false
13+
type: string
14+
default: 'fedify-*.tgz'
15+
16+
jobs:
17+
npm-publish:
18+
runs-on: ubuntu-latest
19+
permissions:
20+
id-token: write
21+
contents: read
22+
steps:
23+
- uses: actions/download-artifact@v4
24+
with:
25+
name: npm-packages
26+
- run: ls -la
27+
- name: Setup Node.js
28+
uses: actions/setup-node@v4
29+
with:
30+
node-version: lts/*
31+
registry-url: https://registry.npmjs.org
32+
- run: sudo npm install -g npm@latest && npm --version
33+
- name: Publish packages
34+
run: |
35+
set -ex
36+
for pkg in ${{ inputs.package_pattern }}; do
37+
if [[ "${{ inputs.tag }}" = "latest" ]]; then
38+
npm publish --logs-dir=. --provenance --access public "$pkg" \
39+
|| grep "Cannot publish over previously published version" *.log
40+
else
41+
npm publish \
42+
--logs-dir=. \
43+
--provenance \
44+
--access public \
45+
--tag "${{ inputs.tag }}" \
46+
"$pkg" \
47+
|| grep "Cannot publish over previously published version" *.log
48+
fi
49+
rm -f *.log
50+
done

.github/workflows/main.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ jobs:
298298
publish-npm:
299299
if: github.event_name == 'push'
300300
needs: [publish]
301-
uses: ./.github/workflows/npm-publish.yaml
301+
uses: ./.github/workflows/build.yaml
302302
with:
303303
tag: ${{ github.ref_type == 'tag' && 'latest' || 'dev' }}
304304

.github/workflows/publish-pr.yaml

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ jobs:
9090
pnpm install
9191
pnpm pack --recursive --filter='!./examples/**'
9292
rm fedify-cli-*.tgz
93+
- uses: actions/upload-artifact@v4
94+
with:
95+
name: npm-packages
96+
path: fedify-*.tgz
9397
- name: Publish to JSR
9498
run: |
9599
set -ex
@@ -106,18 +110,6 @@ jobs:
106110
sleep 30
107111
((attempt++))
108112
done
109-
- name: Publish to npm
110-
run: |
111-
set -ex
112-
for pkg in fedify-*.tgz; do
113-
npm publish \
114-
--logs-dir=. \
115-
--provenance \
116-
--access public \
117-
--tag "pr-${{ inputs.pr_number }}" \
118-
"$pkg" \
119-
|| grep "Cannot publish over previously published version" *.log
120-
done
121113
- name: Generate packages table
122114
id: packages-table
123115
run: |
@@ -137,9 +129,16 @@ jobs:
137129
echo 'EOFLINKS'
138130
} >> $GITHUB_OUTPUT
139131
132+
publish-npm:
133+
if: inputs.publish_packages
134+
needs: [publish-packages]
135+
uses: ./.github/workflows/build.yaml
136+
with:
137+
tag: pr-${{ inputs.pr_number }}
138+
140139
publish-docs:
141140
if: inputs.publish_docs
142-
needs: [get-pr-info]
141+
needs: [get-pr-info, publish-packages]
143142
runs-on: ubuntu-latest
144143
permissions:
145144
contents: read
@@ -179,8 +178,8 @@ jobs:
179178
workingDirectory: ${{ github.workspace }}/docs/
180179

181180
comment-on-pr:
182-
needs: [get-pr-info, publish-packages, publish-docs]
183-
if: always() && needs.get-pr-info.result == 'success' && (needs.publish-packages.result == 'success' || needs.publish-docs.result == 'success')
181+
needs: [get-pr-info, publish-packages, publish-npm, publish-docs]
182+
if: always() && needs.get-pr-info.result == 'success' && (needs.publish-npm.result == 'success' || needs.publish-docs.result == 'success')
184183
runs-on: ubuntu-latest
185184
permissions:
186185
pull-requests: write
@@ -199,7 +198,7 @@ jobs:
199198
message: |
200199
Pre-release has been published for this pull request:
201200
202-
${{ needs.publish-packages.result == 'success' && format('## Packages
201+
${{ needs.publish-npm.result == 'success' && format('## Packages
203202
204203
{0}
205204

0 commit comments

Comments
 (0)