-
Notifications
You must be signed in to change notification settings - Fork 0
203 lines (181 loc) · 6.47 KB
/
Copy pathrelease.yml
File metadata and controls
203 lines (181 loc) · 6.47 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
name: Release
on:
workflow_dispatch:
inputs:
tag:
description: Release tag, for example v0.8-beta.1 or v1.0.0
required: true
type: string
package_version:
description: npm package version, for example 0.8.0-beta.1 or 1.0.0
required: true
type: string
prerelease:
description: Mark the GitHub release as a prerelease
required: true
default: true
type: boolean
pull_request:
types: [closed]
branches: [main]
permissions:
contents: write
pull-requests: write
concurrency:
group: release
cancel-in-progress: false
jobs:
prepare-release:
name: Prepare release
runs-on: ubuntu-latest
if: ${{ github.event_name == 'workflow_dispatch' }}
steps:
- name: Validate release token
env:
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
if [[ -z "$RELEASE_TOKEN" ]]; then
echo "RELEASE_TOKEN secret is required so release PRs can trigger required checks."
exit 1
fi
- name: Validate inputs
env:
RELEASE_TAG: ${{ inputs.tag }}
PACKAGE_VERSION: ${{ inputs.package_version }}
run: |
if [[ ! "$RELEASE_TAG" =~ ^v[0-9]+\.[0-9]+(-beta\.[0-9]+|\.[0-9]+)$ ]]; then
echo "Invalid release tag: $RELEASE_TAG"
exit 1
fi
if [[ ! "$PACKAGE_VERSION" =~ ^[0-9]+\.[0-9]+\.[0-9]+(-[0-9A-Za-z.-]+)?$ ]]; then
echo "Invalid package version: $PACKAGE_VERSION"
exit 1
fi
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Setup pnpm
uses: pnpm/action-setup@v6
with:
version: 11.1.0
- name: Setup Node
uses: actions/setup-node@v6
with:
node-version: 22
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Check release target
env:
RELEASE_TAG: ${{ inputs.tag }}
run: |
if git ls-remote --exit-code --tags origin "refs/tags/$RELEASE_TAG" >/dev/null 2>&1; then
echo "Release tag already exists: $RELEASE_TAG"
exit 1
fi
if git ls-remote --exit-code --heads origin "release/$RELEASE_TAG" >/dev/null 2>&1; then
echo "Release branch already exists: release/$RELEASE_TAG"
exit 1
fi
- name: Update package version
env:
PACKAGE_VERSION: ${{ inputs.package_version }}
run: |
node -e "const fs = require('node:fs'); const path = 'package.json'; const pkg = JSON.parse(fs.readFileSync(path, 'utf8')); pkg.version = process.env.PACKAGE_VERSION; fs.writeFileSync(path, JSON.stringify(pkg, null, 2) + '\n');"
pnpm install --lockfile-only
- name: Verify
run: pnpm verify
- name: Commit release metadata
env:
RELEASE_TAG: ${{ inputs.tag }}
run: |
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git checkout -b "release/$RELEASE_TAG"
git add package.json pnpm-lock.yaml
git commit -m "chore(release): prepare $RELEASE_TAG"
- name: Push release branch
env:
RELEASE_TAG: ${{ inputs.tag }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
git remote set-url origin "https://x-access-token:${RELEASE_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git push --set-upstream origin "release/$RELEASE_TAG"
- name: Open release PR
env:
GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
RELEASE_TAG: ${{ inputs.tag }}
PACKAGE_VERSION: ${{ inputs.package_version }}
PRERELEASE: ${{ inputs.prerelease }}
run: |
cat > release-pr-body.md <<EOF
# Release $RELEASE_TAG
Summary:
- Prepare release $RELEASE_TAG.
- Update package version to $PACKAGE_VERSION.
Release metadata:
- Tag: $RELEASE_TAG.
- Package version: $PACKAGE_VERSION.
- Prerelease: $PRERELEASE.
Verification:
- pnpm verify.
EOF
pr_url=$(gh pr create \
--base main \
--head "release/$RELEASE_TAG" \
--title "chore(release): prepare $RELEASE_TAG" \
--body-file release-pr-body.md)
gh pr merge "$pr_url" --auto --squash --delete-branch
create-release:
name: Create release
runs-on: ubuntu-latest
if: >-
${{
github.event_name == 'pull_request' &&
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release/v')
}}
steps:
- name: Validate release token
env:
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
if [[ -z "$RELEASE_TOKEN" ]]; then
echo "RELEASE_TOKEN secret is required so tag pushes can trigger npm publishing."
exit 1
fi
- name: Checkout main
uses: actions/checkout@v6
with:
ref: main
fetch-depth: 0
- name: Create and push release tag
env:
HEAD_REF: ${{ github.event.pull_request.head.ref }}
RELEASE_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
release_tag="${HEAD_REF#release/}"
if [[ ! "$release_tag" =~ ^v[0-9]+\.[0-9]+(-beta\.[0-9]+|\.[0-9]+)$ ]]; then
echo "Invalid release branch: $HEAD_REF"
exit 1
fi
if git ls-remote --exit-code --tags origin "refs/tags/$release_tag" >/dev/null 2>&1; then
echo "Release tag already exists: $release_tag"
exit 1
fi
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git tag -a "$release_tag" -m "$release_tag"
git remote set-url origin "https://x-access-token:${RELEASE_TOKEN}@github.com/${GITHUB_REPOSITORY}.git"
git push origin "$release_tag"
echo "RELEASE_TAG=$release_tag" >> "$GITHUB_ENV"
- name: Create GitHub release
env:
GH_TOKEN: ${{ secrets.RELEASE_TOKEN }}
run: |
args=(--generate-notes)
if [[ "$RELEASE_TAG" == *-* ]]; then
args+=(--prerelease)
fi
gh release create "$RELEASE_TAG" --title "$RELEASE_TAG" "${args[@]}"