Repository navigation
108 lines (99 loc) · 4.19 KB
/
Copy pathrelease.yml
File metadata and controls
108 lines (99 loc) · 4.19 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
name: Release
# The version in package.json is the switch: merging a pull request that bumps it publishes that
# version. Pushes that leave the version alone (already on npm) do nothing.
#
# On a new version this tests and builds, publishes to npm with provenance (a version with a
# pre-release suffix such as 1.2.0-beta.1 goes to the `next` dist-tag), tags the commit vX.Y.Z and
# creates a GitHub release from the version's section in CHANGELOG.md.
#
# Authentication is npm Trusted Publishing (OIDC) only; no token or secret is used. It is set up on
# npmjs.com under the package's Settings → Trusted Publisher: GitHub Actions, user Inchill,
# repository react-whiteboard, workflow release.yml, no environment, with npm publish and
# npm dist-tag allowed. A new connection expires if it has not published within 48 hours, so set it
# up shortly before a release, or create it again if it has expired.
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: write # push the tag and create the release
id-token: write # Trusted Publishing and provenance
# Never run two releases at once, and never cancel one halfway through publishing.
concurrency:
group: release
cancel-in-progress: false
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: pnpm/action-setup@v4
- uses: actions/setup-node@v4
with:
node-version: 24
cache: pnpm
registry-url: https://registry.npmjs.org
- name: Is this version new?
id: version
run: |
name="$(node -p "require('./package.json').name")"
version="$(node -p "require('./package.json').version")"
echo "version=$version" >> "$GITHUB_OUTPUT"
if npm view "$name@$version" version > /dev/null 2>&1; then
echo "$name@$version is already on npm; nothing to release."
echo "new=false" >> "$GITHUB_OUTPUT"
else
echo "Releasing $name@$version."
echo "new=true" >> "$GITHUB_OUTPUT"
fi
# Trusted Publishing needs npm CLI 11.5.1 or later.
- if: steps.version.outputs.new == 'true'
run: npm install -g npm@11
- if: steps.version.outputs.new == 'true'
run: pnpm install --frozen-lockfile
- if: steps.version.outputs.new == 'true'
run: |
pnpm lint
pnpm typecheck
pnpm test
- name: Release notes from CHANGELOG.md
if: steps.version.outputs.new == 'true'
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
# The lines under "## <version>" up to the next "## " heading.
awk -v v="$VERSION" '
index($0, "## " v " ") == 1 || $0 == "## " v { found = 1; next }
found && /^## / { exit }
found { print }
' CHANGELOG.md > release-notes.md
if ! grep -q '[^[:space:]]' release-notes.md; then
echo "CHANGELOG.md has no section for $VERSION" >&2
exit 1
fi
- name: Publish to npm
if: steps.version.outputs.new == 'true'
# A publish waiting on something (an approval, a one-time password) fails here instead of
# holding the runner for hours.
timeout-minutes: 20
env:
VERSION: ${{ steps.version.outputs.version }}
run: |
case "$VERSION" in
*-*) dist_tag=next ;;
*) dist_tag=latest ;;
esac
npm --version
# prepublishOnly builds the library. Verbose output shows how npm authenticates (the
# Trusted Publishing token exchange) and each registry request; stdin is closed so npm
# cannot wait for input.
npm publish --provenance --access public --tag "$dist_tag" --loglevel verbose < /dev/null
- name: Tag and create the GitHub release
if: steps.version.outputs.new == 'true'
env:
VERSION: ${{ steps.version.outputs.version }}
GH_TOKEN: ${{ github.token }}
run: |
prerelease=""
case "$VERSION" in *-*) prerelease="--prerelease" ;; esac
gh release create "v$VERSION" --target "$GITHUB_SHA" --title "v$VERSION" --notes-file release-notes.md $prerelease