Repository navigation
182 lines (166 loc) · 7.35 KB
/
Copy pathrelease.yml
File metadata and controls
182 lines (166 loc) · 7.35 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
name: Release
# Publishes to npm automatically once `Build` has passed on master.
#
# The version and changelog come from release-it + @release-it/conventional-changelog
# (see the "release-it" block in package.json), so they are derived from the
# conventional commit messages since the previous tag.
#
# Authentication is npm Trusted Publishing (OIDC): npm mints a short-lived
# credential from this workflow's GitHub identity, so there is no NPM_TOKEN secret
# to rotate and the published tarball carries a provenance attestation.
#
# That binding is configured npm-side, on the package's Settings -> Trusted
# publisher: organization/repository `react-native-progress-view/progress-view`,
# workflow `release.yml`. Renaming this file or moving the publish to another
# workflow breaks publishing until that entry is updated to match. npm also
# rejects the provenance unless `repository.url` in package.json names this
# repository.
#
# `npm.skipChecks` in the package.json release-it config belongs to the same
# switch: release-it otherwise gates publishing on `npm whoami` and
# `npm access list collaborators`, which need a long-lived token. Under OIDC
# there is none -- `npm publish` mints the credential itself -- so those checks
# fail with ENEEDAUTH and abort the release before it starts.
on:
workflow_run:
workflows: ["Build"]
types:
- completed
branches:
- master
# Manual escape hatch, e.g. to retry a release that failed partway through.
# Note this path does not check Build, so only run it on a commit you know is green.
workflow_dispatch:
inputs:
increment:
description: >-
Version bump. "auto" derives it from conventional commits since the last
tag, and releases nothing if none of them is a feat/fix/perf/breaking change.
type: choice
default: auto
options:
- auto
- patch
- minor
- major
concurrency:
group: release
cancel-in-progress: false
jobs:
release:
name: Publish to npm
runs-on: ubuntu-latest
# Never react to the `chore: release` commit release-it pushes itself,
# and never publish off a red Build.
# `branches: master` filters the *triggering* run's head branch, and Build also
# runs on pull_request -- so a fork PR from a branch named "master" would match.
# Requiring event == 'push' keeps this to real pushes to our own master.
if: >-
github.event_name == 'workflow_dispatch' ||
(github.event.workflow_run.event == 'push' &&
github.event.workflow_run.conclusion == 'success' &&
!startsWith(github.event.workflow_run.head_commit.message, 'chore: release'))
permissions:
# release-it pushes the release commit and tag, and creates the GitHub release.
contents: write
# Lets npm exchange this run's OIDC token for publish rights (Trusted Publishing).
id-token: write
steps:
- uses: actions/checkout@v5
with:
ref: master
# Full history and tags, so conventional-changelog can see the commits
# since the previous release.
fetch-depth: 0
- name: Decide whether to release
id: guard
run: |
set -euo pipefail
# `Build` validated a specific commit. If master has moved on since then,
# publishing now would ship code CI never checked.
if [ "${{ github.event_name }}" = "workflow_run" ]; then
expected='${{ github.event.workflow_run.head_sha }}'
actual="$(git rev-parse HEAD)"
if [ "$expected" != "$actual" ]; then
echo "master moved past the commit Build validated ($expected -> $actual)."
echo "Skipping; the next green Build will pick this up."
echo "release=false" >> "$GITHUB_OUTPUT"
exit 0
fi
fi
# A manual run is an explicit request to release. With increment "auto"
# and no releasable commit, release-it itself exits without releasing.
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
echo "release=true" >> "$GITHUB_OUTPUT"
exit 0
fi
last_tag="$(git describe --tags --abbrev=0 2>/dev/null || true)"
if [ -n "$last_tag" ]; then
range="$last_tag..HEAD"
else
range="HEAD"
fi
# What the `conventionalcommits` preset will actually bump on:
# - `!` after any type -> major
# - a BREAKING CHANGE: / BREAKING-CHANGE: footer -> major
# - feat: -> minor
# - fix: / perf: -> patch
# Anything else (chore/docs/test/refactor/ci) bumps nothing, so release-it
# has no work to do. Keep these in step with the preset.
subject_re='^[a-zA-Z]+(\([^)]*\))?!: |^(feat|fix|perf)(\([^)]*\))?: '
footer_re='^BREAKING[ -]CHANGE:'
# Walk commits one at a time, checking the subject and the body against
# their own patterns. Matching both at once against the whole message
# would let a body line that merely starts with "fix: " count as a fix.
# Via a file rather than a pipe, so a failing `git log` fails the step
# instead of silently counting zero.
shas="$(mktemp)"
git rev-list "$range" > "$shas"
releasable=0
while IFS= read -r sha; do
[ -n "$sha" ] || continue
if git log -1 --format='%s' "$sha" | grep -qE "$subject_re" \
|| git log -1 --format='%b' "$sha" | grep -qE "$footer_re"; then
releasable=$((releasable + 1))
fi
done < "$shas"
echo "Releasable commits since ${last_tag:-the start of history}: $releasable"
if [ "$releasable" -eq 0 ]; then
echo "release=false" >> "$GITHUB_OUTPUT"
else
echo "release=true" >> "$GITHUB_OUTPUT"
fi
- uses: actions/setup-node@v5
if: steps.guard.outputs.release == 'true'
with:
node-version: 22
registry-url: https://registry.npmjs.org
# Node 22 ships npm 10, which has no OIDC support and would fall back to
# looking for a token. Trusted Publishing needs npm >= 11.5.1.
- name: Upgrade npm for Trusted Publishing
if: steps.guard.outputs.release == 'true'
run: |
npm install -g npm@^11.5.1
npm --version
- name: Install dependencies
if: steps.guard.outputs.release == 'true'
run: yarn install --immutable
- name: Configure git identity
if: steps.guard.outputs.release == 'true'
run: |
git config user.name 'github-actions[bot]'
git config user.email 'github-actions[bot]@users.noreply.github.com'
- name: Release
if: steps.guard.outputs.release == 'true'
run: |
if [ -n "$INCREMENT" ] && [ "$INCREMENT" != "auto" ]; then
yarn release "$INCREMENT" --ci
else
yarn release --ci
fi
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INCREMENT: ${{ inputs.increment }}
# No npm credential here on purpose: `npm publish` picks up the OIDC
# token from the id-token permission above. Setting NODE_AUTH_TOKEN
# would take precedence and put us back on a 2FA-gated token.