-
Notifications
You must be signed in to change notification settings - Fork 0
493 lines (454 loc) Β· 19.2 KB
/
Copy pathrelease.yml
File metadata and controls
493 lines (454 loc) Β· 19.2 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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# Versioning PRs (changesets) still run from CI. Automated `npm publish` is disabled
# until the team adopts this repo; publish packages manually in the meantime.
# To re-enable: set ENABLE_CICD_NPM_PUBLISH to 'true' on the release job and restore
# `publish: bun run release:publish` on the changesets/action step.
name: Release
on:
push:
branches:
- master
- develop
# Avoid running release on docs-only changes.
# Releases should be driven by Changesets (new .changeset files) and the
# resulting version-bump commits (packages/*/package.json + CHANGELOG.md).
paths:
- ".changeset/**"
- "packages/**"
- "package.json"
- "bun.lock"
- "turbo.json"
- "tsconfig*.json"
- ".github/workflows/release.yml"
workflow_dispatch:
inputs:
release_intent:
description: "What should this manual run do? (The 'publish' option is rejected while ENABLE_CICD_NPM_PUBLISH is false.)"
required: true
type: choice
options:
- version-pr
- publish
default: version-pr
force_publish:
description: "Allow manual publish even when version-bump detection is false (use for reruns/recovery)"
required: false
type: boolean
default: false
version_type:
description: 'Version bump type (major/minor/patch)'
required: false
type: choice
options:
- auto
- major
- minor
- patch
publish_auth:
description: "Publish auth mode for manual publish runs"
required: false
type: choice
options:
- auto
- token
- oidc
default: auto
release_channel:
description: "Publish channel (auto maps branch: master->stable, develop->next)"
required: false
type: choice
options:
- auto
- stable
- next
- beta
default: auto
concurrency: ${{ github.workflow }}-${{ github.ref }}
jobs:
release:
name: Release (version PRs only; npm publish off)
runs-on: ubuntu-latest
env:
PUBLISH_NPM_TOKEN: ""
PUBLISH_NODE_AUTH_TOKEN: ""
# Flip to 'true' and restore `publish: bun run release:publish` on changesets/action to re-enable CI publish.
ENABLE_CICD_NPM_PUBLISH: "true"
permissions:
contents: write
pull-requests: write
id-token: write
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Setup Node.js
uses: actions/setup-node@v4
with:
node-version: 20.x
- name: Setup Bun
uses: oven-sh/setup-bun@v2
with:
bun-version: 1.3.13
- name: Cache Bun downloads
uses: actions/cache@v4
with:
path: |
~/.bun/install/cache
key: ${{ runner.os }}-bun-${{ hashFiles('bun.lock') }}
restore-keys: |
${{ runner.os }}-bun-
- name: Configure Git
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
- name: Install dependencies
run: bun install --frozen-lockfile
- name: Detect release intent (changesets or version bumps)
id: detect
shell: bash
run: |
set -euo pipefail
# Are there any Changesets present in the repo at this commit?
# Ignore the .changeset/README.md fixture file which is part of the
# changesets bootstrap and never represents a real changeset entry.
if find .changeset -maxdepth 1 -type f -name '*.md' ! -name 'README.md' -print -quit | grep -q .; then
echo "has_changesets=true" >> "$GITHUB_OUTPUT"
else
echo "has_changesets=false" >> "$GITHUB_OUTPUT"
fi
# Did this push include actual package version bumps (i.e. merged release PR)?
before="${{ github.event.before }}"
after="${{ github.sha }}"
changed_manifests="$(git diff --name-only "$before" "$after" -- 'packages/**/package.json' 'tools/**/package.json' || true)"
has_version_bumps=false
for manifest in $changed_manifests; do
if ! node -e 'const fs=require("fs"); const p=process.argv[1]; try { const pkg=JSON.parse(fs.readFileSync(p, "utf8")); process.exit(pkg.private ? 1 : 0); } catch { process.exit(1); }' "$manifest"; then
continue
fi
manifest_diff="$(git diff -U0 "$before" "$after" -- "$manifest" || true)"
# Require both removed and added "version" lines to avoid treating
# metadata-only package.json edits as publish intents.
if echo "$manifest_diff" | grep -Eq '^-\\s*"version"\\s*:' && echo "$manifest_diff" | grep -Eq '^\\+\\s*"version"\\s*:'; then
has_version_bumps=true
break
fi
done
echo "has_version_bumps=${has_version_bumps}" >> "$GITHUB_OUTPUT"
- name: Validate manual release intent prerequisites
if: github.event_name == 'workflow_dispatch'
shell: bash
run: |
set -euo pipefail
intent="${{ inputs.release_intent }}"
has_changesets="${{ steps.detect.outputs.has_changesets }}"
has_version_bumps="${{ steps.detect.outputs.has_version_bumps }}"
force_publish="${{ inputs.force_publish }}"
if [ "$intent" = "version-pr" ] && [ "$has_changesets" != "true" ]; then
echo "Manual intent 'version-pr' requires at least one .changeset/*.md file in this commit."
exit 1
fi
if [ "$intent" = "publish" ] && [ "$has_version_bumps" != "true" ] && [ "$force_publish" != "true" ]; then
echo "Manual intent 'publish' requires package.json/CHANGELOG.md version bump changes."
echo "If you are rerunning/recovering a failed publish from master, re-run with force_publish=true."
exit 1
fi
if [ "$intent" = "publish" ] && [ "${ENABLE_CICD_NPM_PUBLISH:-false}" != "true" ]; then
echo "Automated npm publish from GitHub Actions is disabled (ENABLE_CICD_NPM_PUBLISH != true)."
echo "Publish manually, or set ENABLE_CICD_NPM_PUBLISH=true and restore the changesets/action publish input in .github/workflows/release.yml."
exit 1
fi
- name: Resolve release channel
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.release_intent == 'publish') || (github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_version_bumps == 'true'))
id: release_channel
shell: bash
run: |
set -euo pipefail
ref_name="${{ github.ref_name }}"
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
channel="${{ inputs.release_channel }}"
if [ "${{ inputs.release_intent }}" != "publish" ]; then
channel="auto"
fi
else
channel="auto"
fi
if [ "${channel}" = "auto" ]; then
if [ "${ref_name}" = "develop" ]; then
channel="next"
elif [ "${ref_name}" = "master" ]; then
channel="stable"
else
echo "Auto release channel is only supported on master or develop; got ${ref_name}" >&2
exit 1
fi
fi
case "${channel}" in
stable|next|beta) ;;
*)
echo "Unsupported release channel: ${channel}" >&2
exit 1
;;
esac
echo "channel=${channel}" >> "$GITHUB_OUTPUT"
if [ "${channel}" = "stable" ]; then
echo "npm_tag=latest" >> "$GITHUB_OUTPUT"
else
echo "npm_tag=${channel}" >> "$GITHUB_OUTPUT"
fi
echo "Resolved release channel: ${channel}"
- name: Enforce branch to channel policy
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.release_intent == 'publish') || (github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_version_bumps == 'true'))
shell: bash
run: |
set -euo pipefail
ref_name="${{ github.ref_name }}"
channel="${{ steps.release_channel.outputs.channel }}"
if [ "${ref_name}" = "develop" ] && [ "${channel}" != "next" ]; then
echo "develop branch publish must use release channel 'next'." >&2
exit 1
fi
if [ "${ref_name}" = "master" ] && [ "${channel}" != "stable" ]; then
echo "master branch publish must use release channel 'stable'." >&2
exit 1
fi
echo "Branch/channel policy validated: ${ref_name} -> ${channel}"
- name: Stop early (no changesets / no version bumps)
if: github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_changesets != 'true' && steps.detect.outputs.has_version_bumps != 'true'
run: |
echo "No .changeset files and no package version/changelog updates in this push. Skipping release."
exit 0
- name: Build packages (publish runs only)
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.release_intent == 'publish') || (github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_version_bumps == 'true'))
run: bun run build
- name: Run tests (publish runs only)
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.release_intent == 'publish') || (github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_version_bumps == 'true'))
run: bun run test
- name: Resolve npm publish auth mode
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && ((github.event_name == 'workflow_dispatch' && inputs.release_intent == 'publish') || (github.event_name != 'workflow_dispatch' && steps.detect.outputs.has_version_bumps == 'true'))
id: npm_auth
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
set -euo pipefail
REGISTRY="$(npm config get registry)"
echo "npm registry: ${REGISTRY}"
if [ "${REGISTRY}" != "https://registry.npmjs.org/" ]; then
echo "Unexpected npm registry configured: ${REGISTRY}" >&2
exit 1
fi
publish_auth_input="auto"
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
publish_auth_input="${{ inputs.publish_auth }}"
fi
if [ "${publish_auth_input}" = "token" ]; then
if [ -z "${NPM_TOKEN:-}" ]; then
echo "publish_auth=token requires NPM_TOKEN secret." >&2
exit 1
fi
echo "mode=token" >> "$GITHUB_OUTPUT"
elif [ "${publish_auth_input}" = "oidc" ]; then
echo "mode=oidc" >> "$GITHUB_OUTPUT"
else
if [ -n "${NPM_TOKEN:-}" ]; then
echo "mode=token" >> "$GITHUB_OUTPUT"
else
echo "mode=oidc" >> "$GITHUB_OUTPUT"
fi
fi
- name: Configure token auth for npm publish
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && steps.npm_auth.outputs.mode == 'token'
env:
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
run: |
set -euo pipefail
echo "Using token auth for npm publish."
echo "PUBLISH_NPM_TOKEN=${NPM_TOKEN}" >> "$GITHUB_ENV"
echo "PUBLISH_NODE_AUTH_TOKEN=${NPM_TOKEN}" >> "$GITHUB_ENV"
- name: Configure OIDC auth for npm publish
if: env.ENABLE_CICD_NPM_PUBLISH == 'true' && steps.npm_auth.outputs.mode == 'oidc'
run: |
set -euo pipefail
echo "Using OIDC trusted publishing for npm publish."
echo "PUBLISH_NPM_TOKEN=" >> "$GITHUB_ENV"
echo "PUBLISH_NODE_AUTH_TOKEN=" >> "$GITHUB_ENV"
- name: Create release pull request (npm publish disabled in CI)
if: github.event_name == 'workflow_dispatch' || steps.detect.outputs.has_changesets == 'true' || steps.detect.outputs.has_version_bumps == 'true'
id: changesets
uses: changesets/action@v1
with:
# Omit `publish` so the action never runs `bun run release:publish` from CI.
# Re-enable publishing by setting ENABLE_CICD_NPM_PUBLISH=true and adding:
publish: bun run release:publish
createGithubReleases: false
version: bun run version
title: 'chore(release): version packages [skip-heavy-ci]'
commit: 'chore(release): version packages'
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
NPM_TOKEN: ${{ env.PUBLISH_NPM_TOKEN }}
NODE_AUTH_TOKEN: ${{ env.PUBLISH_NODE_AUTH_TOKEN }}
NPM_CONFIG_REGISTRY: https://registry.npmjs.org/
RELEASE_CHANNEL: ${{ steps.release_channel.outputs.channel }}
- name: Create GitHub Release
if: steps.changesets.outputs.published == 'true'
uses: actions/create-release@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
# changesets/action outputs `publishedPackages` as a JSON string; parse it before indexing.
tag_name: v${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}
release_name: Release v${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}
body: |
## Changes
${{ steps.changesets.outputs.publishedPackages }}
See CHANGELOG.md files in each package for detailed changes.
draft: false
prerelease: false
- name: Notify Slack on Successful Publish
if: steps.changesets.outputs.published == 'true'
continue-on-error: true
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
"text": "π¦ PIE Elements Package Published to NPM",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "π¦ NPM Package Published"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Version:*\nv${{ fromJson(steps.changesets.outputs.publishedPackages)[0].version }}"
},
{
"type": "mrkdwn",
"text": "*Package:*\n${{ fromJson(steps.changesets.outputs.publishedPackages)[0].name }}"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n${{ github.ref_name }}"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n<${{ github.event.head_commit.url }}|${{ github.sha }}>"
},
{
"type": "mrkdwn",
"text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Run>"
}
]
},
{
"type": "context",
"elements": [
{
"type": "mrkdwn",
"text": "See the workflow summary for the full published package list."
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL || secrets.SLACK_PR_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Notify Slack on Release PR Created/Updated
if: steps.changesets.outputs.hasChangesets == 'true' && steps.changesets.outputs.published != 'true'
continue-on-error: true
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
"text": "π Release PR updated",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "π Release PR Created/Updated"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n${{ github.ref_name }}"
},
{
"type": "mrkdwn",
"text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Run>"
},
{
"type": "mrkdwn",
"text": "*Release PR:*\n${{ steps.changesets.outputs.pullRequestUrl || 'Created/updated by changesets action' }}"
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL || secrets.SLACK_PR_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
- name: Notify Slack on Release Failure
if: failure()
continue-on-error: true
uses: slackapi/slack-github-action@v1.27.0
with:
payload: |
{
"text": "β PIE Elements Release Failed",
"blocks": [
{
"type": "header",
"text": {
"type": "plain_text",
"text": "β NPM Release Failed"
}
},
{
"type": "section",
"fields": [
{
"type": "mrkdwn",
"text": "*Repository:*\n${{ github.repository }}"
},
{
"type": "mrkdwn",
"text": "*Branch:*\n${{ github.ref_name }}"
},
{
"type": "mrkdwn",
"text": "*Commit:*\n<${{ github.event.head_commit.url }}|${{ github.sha }}>"
},
{
"type": "mrkdwn",
"text": "*Author:*\n${{ github.event.head_commit.author.name }}"
},
{
"type": "mrkdwn",
"text": "*Workflow:*\n<${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}|View Run>"
}
]
}
]
}
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL || secrets.SLACK_PR_WEBHOOK_URL }}
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK