-
Notifications
You must be signed in to change notification settings - Fork 45
251 lines (215 loc) · 8.6 KB
/
release-on-merge.yml
File metadata and controls
251 lines (215 loc) · 8.6 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
name: Release Cycle
on:
pull_request:
types: [closed]
branches:
- master
permissions:
contents: write
pull-requests: write
jobs:
# ---------------------------------------------------------------------------
# Create GitHub release and tag; optionally create next minor branch
# ---------------------------------------------------------------------------
release:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'r')
runs-on: ubuntu-latest
outputs:
new_branch: ${{ steps.create_next_branch.outputs.new_branch }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Get version from branch
id: get_version
run: |
VERSION=$(echo "${{ github.event.pull_request.head.ref }}" | sed -e 's/^r//')
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "tag_name=v$VERSION" >> $GITHUB_OUTPUT
- name: Create Release and Tag
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh release create ${{ steps.get_version.outputs.tag_name }} \
--generate-notes \
--title "${{ steps.get_version.outputs.tag_name }}"
- name: Create next release branch if major/minor
id: create_next_branch
if: endsWith(steps.get_version.outputs.version, '.0')
run: |
VERSION="${{ steps.get_version.outputs.version }}"
MAJOR=$(echo "$VERSION" | cut -d. -f1)
MINOR=$(echo "$VERSION" | cut -d. -f2)
NEW_MINOR=$((MINOR + 1))
NEW_BRANCH="r$MAJOR.$NEW_MINOR.0"
git checkout -b "$NEW_BRANCH" "${{ github.sha }}"
git push -u origin "$NEW_BRANCH"
echo "new_branch=$NEW_BRANCH" >> $GITHUB_OUTPUT
# ---------------------------------------------------------------------------
# Publish to Test PyPI
# ---------------------------------------------------------------------------
pypi-test:
needs: release
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
python-version: "3.11"
- name: Build
run: uv build
- name: Publish to Test PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
repository-url: https://test.pypi.org/legacy/
# ---------------------------------------------------------------------------
# Publish to PyPI
# ---------------------------------------------------------------------------
pypi:
needs: pypi-test
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.sha }}
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
python-version: "3.11"
- name: Build
run: uv build
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
# ---------------------------------------------------------------------------
# Prepare the next release branch (version bump + lock + PR)
# ---------------------------------------------------------------------------
prepare-next:
needs: release
if: needs.release.outputs.new_branch != ''
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ needs.release.outputs.new_branch }}
fetch-depth: 0
- name: Get version from branch
id: get_version
run: |
VERSION=$(echo "${{ needs.release.outputs.new_branch }}" | sed -e 's/^r//')
echo "version=$VERSION" >> $GITHUB_OUTPUT
- name: Update version in pyproject.toml
run: |
sed -i "s/^version = \".*\"/version = \"${{ steps.get_version.outputs.version }}\"/" pyproject.toml
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
python-version: "3.11"
- name: Update lock file
run: uv lock --python 3.11
- name: Commit and push changes
id: autocommit
uses: stefanzweifel/git-auto-commit-action@v5
with:
commit_message: "Update version to ${{ steps.get_version.outputs.version }}"
branch: ${{ needs.release.outputs.new_branch }}
file_pattern: "pyproject.toml uv.lock"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Pull Request
if: steps.autocommit.outputs.changes_detected == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh pr create \
--base master \
--head ${{ needs.release.outputs.new_branch }} \
--title "Release ${{ needs.release.outputs.new_branch }}" \
--body "This PR contains the changes for release ${{ needs.release.outputs.new_branch }}." \
--draft
# ---------------------------------------------------------------------------
# Propagate hotfix to the next open release branch (patch releases only)
# ---------------------------------------------------------------------------
propagate-hotfix:
if: github.event.pull_request.merged == true && startsWith(github.event.pull_request.head.ref, 'r')
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ github.event.repository.default_branch }}
fetch-depth: 0
- name: Check if hotfix propagation is needed
id: check
run: |
MERGED_BRANCH="${{ github.event.pull_request.head.ref }}"
if [[ ! $MERGED_BRANCH =~ ^r([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
echo "Branch $MERGED_BRANCH is not a valid release branch. Skipping."
echo "should_propagate=false" >> $GITHUB_OUTPUT
exit 0
fi
MAJOR=${BASH_REMATCH[1]}
MINOR=${BASH_REMATCH[2]}
PATCH=${BASH_REMATCH[3]}
if [[ $PATCH -eq 0 ]]; then
echo "Not a patch release (Z=0). No forward-merge needed."
echo "should_propagate=false" >> $GITHUB_OUTPUT
exit 0
fi
NEXT_MINOR=$((MINOR + 1))
TARGET_VERSION="$MAJOR.$NEXT_MINOR.0"
TARGET_BRANCH="r$TARGET_VERSION"
if ! git ls-remote --exit-code --heads origin "$TARGET_BRANCH"; then
echo "Target branch $TARGET_BRANCH does not exist. No forward-merge needed."
echo "should_propagate=false" >> $GITHUB_OUTPUT
exit 0
fi
echo "should_propagate=true" >> $GITHUB_OUTPUT
echo "target_branch=$TARGET_BRANCH" >> $GITHUB_OUTPUT
echo "target_version=$TARGET_VERSION" >> $GITHUB_OUTPUT
echo "merged_version=$MAJOR.$MINOR.$PATCH" >> $GITHUB_OUTPUT
- name: Install uv
if: steps.check.outputs.should_propagate == 'true'
uses: astral-sh/setup-uv@v5
with:
enable-cache: false
python-version: "3.11"
- name: Propagate hotfix
if: steps.check.outputs.should_propagate == 'true'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
TARGET_BRANCH="${{ steps.check.outputs.target_branch }}"
TARGET_VERSION="${{ steps.check.outputs.target_version }}"
MERGED_VERSION="${{ steps.check.outputs.merged_version }}"
TEMP_BRANCH="hotfix-propagate-v$MERGED_VERSION-to-$TARGET_BRANCH"
DEFAULT_BRANCH="${{ github.event.repository.default_branch }}"
echo "Propagating hotfix v$MERGED_VERSION to $TARGET_BRANCH"
git checkout -b "$TEMP_BRANCH" "origin/$DEFAULT_BRANCH"
# Update version in pyproject.toml
sed -i "s/^version = \".*\"/version = \"$TARGET_VERSION\"/" pyproject.toml
# Update lock file
uv lock --python 3.11
# Commit and push
git config --global user.name "github-actions[bot]"
git config --global user.email "41898282+github-actions[bot]@users.noreply.github.com"
git add pyproject.toml uv.lock
git commit -m "Align version to $TARGET_VERSION for hotfix propagation"
git push -u origin "$TEMP_BRANCH"
# Create PR
gh pr create \
--base "$TARGET_BRANCH" \
--head "$TEMP_BRANCH" \
--title "Merge hotfix changes from $DEFAULT_BRANCH into $TARGET_BRANCH" \
--body "This PR propagates hotfix changes from v$MERGED_VERSION into the next release branch ($TARGET_BRANCH)."