-
Notifications
You must be signed in to change notification settings - Fork 2
187 lines (157 loc) · 7.19 KB
/
release.yml
File metadata and controls
187 lines (157 loc) · 7.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
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
name: Release
on:
workflow_dispatch:
inputs:
version:
description: "Version to release (e.g., 1.0.0)"
required: true
permissions:
contents: write
pull-requests: write
statuses: write
jobs:
release:
runs-on: ubuntu-latest
steps:
- name: Validate Version
run: |
if [[ ! "${{ inputs.version }}" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
echo "::error::Version must be in format X.Y.Z (e.g., 1.0.0)"
exit 1
fi
echo "VERSION=${{ inputs.version }}" >> $GITHUB_ENV
echo "MAJOR_VERSION=$(echo ${{ inputs.version }} | cut -d. -f1)" >> $GITHUB_ENV
- name: Checkout
uses: actions/checkout@v6
with:
fetch-depth: 0
- name: Get Previous Tag
id: prev_tag
run: |
# Get the most recent tag, or use empty string for first release
PREV_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
echo "tag=$PREV_TAG" >> $GITHUB_OUTPUT
echo "Previous tag: $PREV_TAG"
- name: Generate Changelog
id: changelog
uses: mikepenz/release-changelog-builder-action@348e88fab4c37338b1e803ceb2d4a7a5db6c0833 # v6
with:
configuration: "configuration.json"
mode: "COMMIT"
fromTag: ${{ steps.prev_tag.outputs.tag }}
toTag: "HEAD"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Changelog PR and Merge
id: changelog_pr
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
BRANCH_NAME="chore/changelog-v$VERSION"
git checkout -b "$BRANCH_NAME"
# Prepend changelog to CHANGELOG.md (create if not exists)
touch CHANGELOG.md
{
echo -e "## [$VERSION] - $(date +%Y-%m-%d)\n"
echo "${{ steps.changelog.outputs.changelog }}"
echo ""
cat CHANGELOG.md
} > CHANGELOG.md.tmp
mv CHANGELOG.md.tmp CHANGELOG.md
git add CHANGELOG.md
git commit -m "chore: update changelog for v$VERSION"
git push origin "$BRANCH_NAME"
HEAD_SHA=$(git rev-parse HEAD)
# Create PR and capture the URL
PR_URL=$(gh pr create \
--title "chore: update changelog for v$VERSION" \
--body "Automated changelog update for release v$VERSION" \
--base main \
--head "$BRANCH_NAME")
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
# PRs created by GITHUB_TOKEN don't trigger pull_request events in other
# workflows (GitHub security feature to prevent loops), so build-and-test.yml
# won't run. Since this PR only changes CHANGELOG.md, set the required
# status check directly to unblock auto-merge.
gh api \
-X POST \
"repos/${{ github.repository }}/statuses/$HEAD_SHA" \
-f state=success \
-f context="all-tests-passed" \
-f description="Changelog-only PR — no code changes"
# Enable auto-merge (requires auto-merge to be enabled in repo settings)
gh pr merge "$PR_URL" --auto --squash
- name: Wait for PR to be Merged
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
PR_URL="${{ steps.changelog_pr.outputs.pr_url }}"
echo "Waiting for PR to be merged: $PR_URL"
# Wait for up to 5 minutes for the PR to be merged
for i in {1..30}; do
STATE=$(gh pr view "$PR_URL" --json state --jq '.state')
echo "PR state: $STATE (attempt $i/30)"
if [ "$STATE" = "MERGED" ]; then
echo "PR has been merged!"
break
elif [ "$STATE" = "CLOSED" ]; then
echo "::error::PR was closed without merging"
exit 1
fi
sleep 10
done
# Final check
STATE=$(gh pr view "$PR_URL" --json state --jq '.state')
if [ "$STATE" != "MERGED" ]; then
echo "::error::PR was not merged within the timeout period. Current state: $STATE"
exit 1
fi
- name: Checkout Updated Main
uses: actions/checkout@v6
with:
ref: main
fetch-depth: 0
- name: Update References for Release
run: |
# Create a release branch from updated main
git checkout -b "release/v$VERSION"
# Replace @main with @v{major} in action.yml and sub-actions
find . -name "action.yml" -print0 | xargs -0 sed -i "s|@main|@v$MAJOR_VERSION|g"
# Verify if any changes were made
git diff
- name: Commit Release Changes
run: |
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Only commit if there are changes
if ! git diff --quiet; then
git add .
git commit -m "chore: prepare release v$VERSION"
else
echo "No changes to commit (references might already be correct or using relative paths)."
fi
- name: Tagging
run: |
# Tag specific version
git tag "v$VERSION"
git push origin "v$VERSION"
# Tag/Move major version
# Force update the major tag to point to this new release
git tag -f "v$MAJOR_VERSION"
git push -f origin "v$MAJOR_VERSION"
- name: Create Release
uses: softprops/action-gh-release@b4309332981a82ec1c5618f44dd2e27cc8bfbfda # v3
with:
tag_name: "v${{ inputs.version }}"
name: "v${{ inputs.version }}"
body: ${{ steps.changelog.outputs.changelog }}
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Cleanup
if: always()
run: |
# Delete the changelog branch if it still exists
git push origin --delete "chore/changelog-v$VERSION" || true