Skip to content

Commit 14bfee2

Browse files
Add trusted npm publishing workflow
1 parent ed1d7d7 commit 14bfee2

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

‎.github/workflows/publish.yml‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
name: Publish
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
release_tag:
7+
description: Release tag to publish (for example, v5.0.0-beta.6)
8+
required: true
9+
type: string
10+
11+
concurrency:
12+
group: npm-publish-${{ inputs.release_tag }}
13+
cancel-in-progress: false
14+
15+
jobs:
16+
publish:
17+
runs-on: ubuntu-latest
18+
permissions:
19+
contents: read
20+
id-token: write
21+
steps:
22+
- name: Check out release tag
23+
uses: actions/checkout@v4
24+
with:
25+
ref: ${{ inputs.release_tag }}
26+
fetch-depth: 0
27+
28+
- name: Set up Node.js
29+
uses: actions/setup-node@v4
30+
with:
31+
node-version: "24.x"
32+
registry-url: https://registry.npmjs.org
33+
34+
- name: Set up release tools
35+
run: npm install --global npm@11.6.2 yarn@1.22.22
36+
37+
- name: Validate release
38+
id: release
39+
env:
40+
RELEASE_TAG: ${{ inputs.release_tag }}
41+
run: |
42+
set -euo pipefail
43+
44+
version="$(node -p "require('./package.json').version")"
45+
if [[ "${RELEASE_TAG}" != "v${version}" ]]; then
46+
echo "::error::Tag ${RELEASE_TAG} does not match package version ${version}."
47+
exit 1
48+
fi
49+
50+
tag_commit="$(git rev-parse "refs/tags/${RELEASE_TAG}^{commit}")"
51+
if [[ "$(git rev-parse HEAD)" != "${tag_commit}" ]]; then
52+
echo "::error::${RELEASE_TAG} is not the checked-out release tag."
53+
exit 1
54+
fi
55+
if ! git merge-base --is-ancestor "${tag_commit}" origin/main; then
56+
echo "::error::${RELEASE_TAG} is not on the main branch."
57+
exit 1
58+
fi
59+
60+
case "${version}" in
61+
*-alpha*)
62+
echo "::error::Alpha releases must not be published."
63+
exit 1
64+
;;
65+
*-*)
66+
npm_tag="next"
67+
;;
68+
*)
69+
npm_tag="latest"
70+
;;
71+
esac
72+
73+
echo "npm_tag=${npm_tag}" >> "${GITHUB_OUTPUT}"
74+
75+
- name: Install dependencies
76+
run: yarn install --frozen-lockfile --ignore-scripts
77+
78+
- name: Set up project
79+
run: yarn setup
80+
81+
- name: Test
82+
run: yarn test
83+
84+
- name: Publish
85+
env:
86+
NPM_TAG: ${{ steps.release.outputs.npm_tag }}
87+
run: npm publish --provenance --tag "${NPM_TAG}"

0 commit comments

Comments
 (0)