Repository navigation
189 lines (172 loc) · 7.08 KB
/
Copy pathrelease.yml
File metadata and controls
189 lines (172 loc) · 7.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
name: Release
# Publishing is irreversible, so the trigger is a deliberate act: draft a
# GitHub Release, publish it, and this ships the same commit to both registries.
#
# Use the workflow_dispatch entry point to rehearse — it defaults to a dry run,
# which exercises packaging and the already-published guard without uploading.
# A rehearsal cannot prove npmjs auth works, because the OIDC credential is
# only minted by a real publish; see RELEASING.md.
on:
release:
types: [published]
workflow_dispatch:
inputs:
dry_run:
description: Rehearse without publishing anything
type: boolean
default: true
# No ambient permissions; each job asks for exactly what it needs.
permissions: {}
jobs:
verify:
name: Verify
runs-on: ubuntu-latest
permissions:
contents: read
outputs:
version: ${{ steps.resolve.outputs.version }}
npm-tag: ${{ steps.resolve.outputs.npm-tag }}
dry-run: ${{ steps.resolve.outputs.dry-run }}
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
cache: npm
- run: npm ci
- run: npm test
# 0.6.0 shipped a `bin` that exited 0 having printed nothing, and nothing
# here caught it: `npm test` and `npm run generate:example` both invoke the
# generator by path, which is the one way that never crosses the symlink npm
# installs a bin as. CI runs this across every package manager and platform;
# the release gate runs it once more against the exact tree being published.
- name: Smoke-test the packed artifact
run: .github/scripts/smoke-packed-artifact.sh npm
- name: Resolve version and publish tag
id: resolve
env:
RELEASE_TAG: ${{ github.event.release.tag_name }}
IS_PRERELEASE: ${{ github.event.release.prerelease }}
DRY_RUN: ${{ inputs.dry_run }}
run: |
set -euo pipefail
version="$(node -p "require('./package.json').version")"
echo "version=$version" >> "$GITHUB_OUTPUT"
# A release tag that disagrees with package.json means the registry
# and the release notes would describe different things.
if [ "${GITHUB_EVENT_NAME}" = "release" ]; then
if [ "${RELEASE_TAG#v}" != "$version" ]; then
echo "::error::Release tag '${RELEASE_TAG}' does not match package.json version '${version}'."
exit 1
fi
if [ "$IS_PRERELEASE" = "true" ]; then
echo "npm-tag=next" >> "$GITHUB_OUTPUT"
else
echo "npm-tag=latest" >> "$GITHUB_OUTPUT"
fi
echo "dry-run=false" >> "$GITHUB_OUTPUT"
else
echo "npm-tag=latest" >> "$GITHUB_OUTPUT"
echo "dry-run=${DRY_RUN}" >> "$GITHUB_OUTPUT"
fi
- name: Summarise
run: |
{
echo "| | |"
echo "| --- | --- |"
echo "| Version | \`${{ steps.resolve.outputs.version }}\` |"
echo "| npm dist-tag | \`${{ steps.resolve.outputs.npm-tag }}\` |"
echo "| Dry run | \`${{ steps.resolve.outputs.dry-run }}\` |"
} >> "$GITHUB_STEP_SUMMARY"
npmjs:
name: Publish to npmjs.com
needs: verify
runs-on: ubuntu-latest
environment: release
permissions:
contents: read
# Trusted publishing: npm exchanges this workflow's OIDC token for a
# short-lived registry credential, so there is no NPM_TOKEN secret to
# leak or rotate. The trust is configured on npmjs.com against the
# repository *and this filename* — renaming this file breaks publishing
# until the trusted publisher is updated to match.
id-token: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '24'
registry-url: https://registry.npmjs.org
# Trusted publishing needs npm >= 11.5.1. Node 24 ships a new enough npm
# today, but pin it rather than inherit whatever the runner image has.
- name: Use an npm that speaks OIDC
run: |
npm install -g npm@latest
npm --version
# One registry can succeed while the other fails, so a re-run must not
# trip over its own earlier success.
- name: Skip if this version is already published
id: exists
run: .github/scripts/version-published.sh
env:
VERSION: ${{ needs.verify.outputs.version }}
# No NODE_AUTH_TOKEN, and no --provenance: trusted publishing supplies
# the credential from the OIDC token above and attaches provenance by
# default. --access public is what makes the first publish of a scoped
# package public rather than restricted; publishConfig says the same.
- name: Publish
if: steps.exists.outputs.published != 'true'
run: |
npm publish \
--access public \
--tag '${{ needs.verify.outputs.npm-tag }}' \
${{ needs.verify.outputs.dry-run == 'true' && '--dry-run' || '' }}
github-packages:
name: Publish to GitHub Packages
needs: verify
runs-on: ubuntu-latest
permissions:
contents: read
packages: write
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '22'
registry-url: https://npm.pkg.github.com
# GitHub Packages accepts only scoped names, and the scope must be the
# repository owner — lowercased, because npm rejects uppercase in a
# package name and the owner here is `OpenPredicate`. npmjs.com carries
# the @open-predicate scope instead, so rewrite the name here; the
# tarball is otherwise identical.
- name: Scope the package name to the repository owner
env:
OWNER: ${{ github.repository_owner }}
REPO: ${{ github.event.repository.name }}
run: |
set -euo pipefail
scope="$(printf '%s' "$OWNER" | tr '[:upper:]' '[:lower:]')"
npm pkg set name="@${scope}/${REPO}"
node -p "require('./package.json').name"
# GitHub Packages has no OIDC trusted-publishing equivalent, so this one
# stays token-authenticated. GITHUB_TOKEN is minted per run and expires
# with it, so there is still no long-lived secret in the repository.
- name: Check authentication
run: npm whoami --registry=https://npm.pkg.github.com
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Skip if this version is already published
id: exists
run: .github/scripts/version-published.sh
env:
VERSION: ${{ needs.verify.outputs.version }}
REGISTRY: https://npm.pkg.github.com
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Publish
if: steps.exists.outputs.published != 'true'
run: |
npm publish \
--tag '${{ needs.verify.outputs.npm-tag }}' \
${{ needs.verify.outputs.dry-run == 'true' && '--dry-run' || '' }}
env:
NODE_AUTH_TOKEN: ${{ secrets.GITHUB_TOKEN }}