-
Notifications
You must be signed in to change notification settings - Fork 20
227 lines (198 loc) · 8.73 KB
/
Copy pathrelease-on-main.yml
File metadata and controls
227 lines (198 loc) · 8.73 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
name: Release on main
on:
push:
branches: [main]
workflow_dispatch:
permissions:
contents: write
pull-requests: read
concurrency:
group: release-main
cancel-in-progress: true
jobs:
release:
runs-on: ubuntu-latest
if: ${{ !contains(github.event.head_commit.message, '[skip release]') }}
env:
RELEASE_DRY_RUN: ${{ vars.RELEASE_DRY_RUN || 'true' }}
steps:
- name: Checkout
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Setup Node
uses: actions/setup-node@v4
with:
node-version: "22"
- name: Resolve dry-run mode
id: dryrun
run: |
case "${RELEASE_DRY_RUN,,}" in
1|true|yes|on) echo "enabled=true" >> "$GITHUB_OUTPUT" ;;
*) echo "enabled=false" >> "$GITHUB_OUTPUT" ;;
esac
- name: Determine last release tag
id: last_tag
run: |
git fetch --tags --force
LAST_TAG=$(git tag --list 'v*' --sort=-v:refname | head -n1)
if [ -z "$LAST_TAG" ]; then
LAST_TAG="v0.0.0"
fi
CURRENT_VERSION=$(node -p "require('./package.json').version")
echo "last_tag=$LAST_TAG" >> "$GITHUB_OUTPUT"
echo "current_version=$CURRENT_VERSION" >> "$GITHUB_OUTPUT"
- name: Gather merged PRs since last tag
id: prs
env:
GH_TOKEN: ${{ github.token }}
LAST_TAG: ${{ steps.last_tag.outputs.last_tag }}
run: |
set -euo pipefail
OWNER_REPO="${GITHUB_REPOSITORY}"
OWNER="${OWNER_REPO%/*}"
REPO="${OWNER_REPO#*/}"
if [ "$LAST_TAG" = "v0.0.0" ]; then
# No prior release — look back 30 days instead of all time
START_DATE=$(date -u -d '30 days ago' '+%Y-%m-%dT%H:%M:%SZ')
else
START_DATE=$(git log -1 --format=%cI "$LAST_TAG")
fi
gh api \
--paginate \
--slurp \
"/repos/$OWNER/$REPO/pulls?state=closed&base=main&sort=updated&direction=desc&per_page=100" \
> /tmp/all_prs.json
jq --arg start "$START_DATE" 'flatten | map(select(.merged_at != null and .merged_at > $start)) | sort_by(.merged_at)' /tmp/all_prs.json > /tmp/merged_prs.json
COUNT=$(jq 'length' /tmp/merged_prs.json)
echo "count=$COUNT" >> "$GITHUB_OUTPUT"
if [ "$COUNT" -eq 0 ]; then
echo "has_changes=false" >> "$GITHUB_OUTPUT"
echo '[]' > /tmp/pr_context.json
exit 0
fi
jq '[.[] | {number, title, body: (.body // "" | .[:200]), labels: [.labels[].name], user: .user.login, merged_at, html_url}]' /tmp/merged_prs.json > /tmp/pr_context.json
# Cap at 50 most recent PRs for the Claude prompt to keep payload reasonable
jq '[ sort_by(.merged_at) | reverse | .[:50] | reverse | .[] ]' /tmp/pr_context.json > /tmp/pr_context_capped.json
echo "has_changes=true" >> "$GITHUB_OUTPUT"
- name: Decide release bump from PR labels
id: decide
if: steps.prs.outputs.has_changes == 'true'
run: |
set -euo pipefail
DECISION=$(jq -r '
if any(.[]; (.labels // []) | index("release:minor")) then "minor"
elif any(.[]; (.labels // []) | index("release:patch")) then "patch"
elif all(.[]; ((.labels // []) | index("release:none"))) then "none"
else "patch"
end
' /tmp/pr_context.json)
REASON=$(jq -r --arg decision "$DECISION" '
if $decision == "minor" then
"At least one merged PR requested a minor release via label."
elif $decision == "patch" then
"No minor label found; defaulting to patch for shipped changes."
else
"All merged PRs were explicitly marked release:none."
end
' /tmp/pr_context.json)
echo "decision=$DECISION" >> "$GITHUB_OUTPUT"
echo "reason=$REASON" >> "$GITHUB_OUTPUT"
- name: No-op summary
if: steps.prs.outputs.has_changes != 'true' || steps.decide.outputs.decision == 'none'
run: |
echo "## Release decision" >> "$GITHUB_STEP_SUMMARY"
if [ "${{ steps.prs.outputs.has_changes }}" != "true" ]; then
echo "No merged PRs since last tag; skipping release." >> "$GITHUB_STEP_SUMMARY"
else
echo "Decision: none" >> "$GITHUB_STEP_SUMMARY"
echo "Reason: ${{ steps.decide.outputs.reason }}" >> "$GITHUB_STEP_SUMMARY"
fi
- name: Bump version
id: bump
if: steps.decide.outputs.decision == 'patch' || steps.decide.outputs.decision == 'minor'
env:
BUMP: ${{ steps.decide.outputs.decision }}
run: |
set -euo pipefail
CURRENT="${{ steps.last_tag.outputs.current_version }}"
NEXT=$(node -e "const v=process.argv[1].split('.').map(Number); if(process.argv[2]==='minor'){v[1]++; v[2]=0;} else {v[2]++;} process.stdout.write(v.join('.'))" -- "${CURRENT}" "${BUMP}")
node -e "const fs=require('fs'); const p='package.json'; const j=JSON.parse(fs.readFileSync(p,'utf8')); j.version=process.argv[1]; fs.writeFileSync(p, JSON.stringify(j,null,2)+'\\n');" -- "${NEXT}"
if [ -f package-lock.json ]; then
npm install --package-lock-only --ignore-scripts
fi
echo "current=$CURRENT" >> "$GITHUB_OUTPUT"
echo "next=$NEXT" >> "$GITHUB_OUTPUT"
echo "tag=v${NEXT}" >> "$GITHUB_OUTPUT"
- name: Check tag does not already exist
id: tag_check
if: steps.bump.outputs.tag != '' && steps.dryrun.outputs.enabled != 'true'
env:
TAG: ${{ steps.bump.outputs.tag }}
run: |
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "Tag already exists: $TAG. Skipping to keep idempotent."
echo "exists=true" >> "$GITHUB_OUTPUT"
else
echo "exists=false" >> "$GITHUB_OUTPUT"
fi
- name: Commit and tag
if: steps.bump.outputs.tag != '' && steps.dryrun.outputs.enabled != 'true' && steps.tag_check.outputs.exists != 'true'
env:
TAG: ${{ steps.bump.outputs.tag }}
run: |
set -euo pipefail
git config user.name "github-actions[bot]"
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add package.json package-lock.json || true
git commit -m "release: ${TAG} [skip release]"
git tag "$TAG"
git push --atomic origin main "$TAG"
- name: Build changelog markdown
id: changelog
if: steps.bump.outputs.tag != ''
env:
TAG: ${{ steps.bump.outputs.tag }}
DECISION: ${{ steps.decide.outputs.decision }}
REASON: ${{ steps.decide.outputs.reason }}
run: |
set -euo pipefail
{
echo "## ${TAG}"
echo
echo "Release type: ${DECISION}"
echo
echo "Reason: ${REASON}"
echo
echo "### Merged PRs"
jq -r '.[] | "- #\(.number) \(.title) (@\(.user)) — \(.html_url)"' /tmp/pr_context.json
} > /tmp/release-notes.md
echo "notes_path=/tmp/release-notes.md" >> "$GITHUB_OUTPUT"
- name: Create GitHub Release
if: steps.bump.outputs.tag != '' && steps.dryrun.outputs.enabled != 'true'
env:
GH_TOKEN: ${{ github.token }}
TAG: ${{ steps.bump.outputs.tag }}
NOTES: ${{ steps.changelog.outputs.notes_path }}
run: |
set -euo pipefail
if gh release view "$TAG" >/dev/null 2>&1; then
echo "Release already exists for $TAG; skipping."
exit 0
fi
gh release create "$TAG" --title "$TAG" --notes-file "$NOTES"
- name: Release summary
if: steps.bump.outputs.tag != ''
run: |
echo "## Release created" >> "$GITHUB_STEP_SUMMARY"
echo "Tag: ${{ steps.bump.outputs.tag }}" >> "$GITHUB_STEP_SUMMARY"
echo "Decision: ${{ steps.decide.outputs.decision }}" >> "$GITHUB_STEP_SUMMARY"
echo "Reason: ${{ steps.decide.outputs.reason }}" >> "$GITHUB_STEP_SUMMARY"
- name: Dry-run release summary
if: steps.bump.outputs.tag != '' && steps.dryrun.outputs.enabled == 'true'
run: |
echo "## Dry run: no release published" >> "$GITHUB_STEP_SUMMARY"
echo "Decision: ${{ steps.decide.outputs.decision }}" >> "$GITHUB_STEP_SUMMARY"
echo "Reason: ${{ steps.decide.outputs.reason }}" >> "$GITHUB_STEP_SUMMARY"
echo "Current version: ${{ steps.bump.outputs.current }}" >> "$GITHUB_STEP_SUMMARY"
echo "Would publish tag: ${{ steps.bump.outputs.tag }}" >> "$GITHUB_STEP_SUMMARY"