-
Notifications
You must be signed in to change notification settings - Fork 1
361 lines (300 loc) · 11.8 KB
/
auto-release.yml
File metadata and controls
361 lines (300 loc) · 11.8 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
name: Automated Release Pipeline
on:
push:
branches: [ main ]
paths-ignore:
- 'README.md'
- 'docs/**'
- '.gitignore'
- 'LICENSE'
# Grant necessary permissions for the workflow
permissions:
contents: write # Required to push commits and create tags
actions: write # Required to trigger other workflows
packages: write # Required for package publishing
pull-requests: write # Required for PR operations
issues: write # Required for issue operations
env:
PYTHON_VERSION: '3.11'
jobs:
analyze-and-version:
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.version_check.outputs.should_release }}
version_type: ${{ steps.version_check.outputs.version_type }}
new_version: ${{ steps.version_check.outputs.new_version }}
current_version: ${{ steps.version_check.outputs.current_version }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
# Ensure the token can push to protected branches
persist-credentials: true
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Configure Git
run: |
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
git config --local user.name "github-actions[bot]"
# Configure git to use the GitHub token for authentication
git config --local url."https://x-access-token:${{ secrets.GITHUB_TOKEN }}@github.com/".insteadOf "https://github.com/"
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Check Rust formatting
run: cargo fmt --all -- --check
- name: Run Rust linting
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Analyze commits and determine version bump
id: version_check
run: |
# Get current version
CURRENT_VERSION=$(python scripts/get_version.py)
echo "current_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
echo "Current version: $CURRENT_VERSION"
# Get commits since last tag
LAST_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$LAST_TAG" ]; then
COMMITS=$(git log --oneline)
else
COMMITS=$(git log ${LAST_TAG}..HEAD --oneline)
fi
echo "Analyzing commits since $LAST_TAG:"
echo "$COMMITS"
# Determine version bump type based on commit messages
VERSION_TYPE="none"
# Check for breaking changes (major version)
if echo "$COMMITS" | grep -qiE "(BREAKING CHANGE|breaking:|major:)"; then
VERSION_TYPE="major"
# Check for new features (minor version)
elif echo "$COMMITS" | grep -qiE "(feat:|feature:|minor:)"; then
VERSION_TYPE="minor"
# Check for bug fixes and other changes (patch version)
elif echo "$COMMITS" | grep -qiE "(fix:|patch:|chore:|docs:|style:|refactor:|perf:|test:)"; then
VERSION_TYPE="patch"
fi
# Skip release if no relevant changes
if [ "$VERSION_TYPE" = "none" ]; then
echo "No version-relevant changes detected. Skipping release."
echo "should_release=false" >> $GITHUB_OUTPUT
echo "version_type=none" >> $GITHUB_OUTPUT
echo "new_version=$CURRENT_VERSION" >> $GITHUB_OUTPUT
exit 0
fi
echo "Determined version bump type: $VERSION_TYPE"
echo "should_release=true" >> $GITHUB_OUTPUT
echo "version_type=$VERSION_TYPE" >> $GITHUB_OUTPUT
# Calculate new version
python scripts/bump_version.py $VERSION_TYPE
NEW_VERSION=$(python scripts/get_version.py)
echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "New version will be: $NEW_VERSION"
- name: Commit version changes
if: steps.version_check.outputs.should_release == 'true'
run: |
# Check if there are changes to commit
if git diff --staged --quiet; then
echo "No changes to commit"
else
echo "Committing version changes..."
git add -A
git commit -m "chore: bump version to ${{ steps.version_check.outputs.new_version }} [skip ci]"
# Push with retry logic
for i in {1..3}; do
if git push origin main; then
echo "[OK] Successfully pushed version bump commit"
break
else
echo "[ERROR] Push attempt $i failed, retrying in 5 seconds..."
sleep 5
fi
if [ $i -eq 3 ]; then
echo "[ERROR] Failed to push after 3 attempts"
exit 1
fi
done
fi
- name: Create and push tag
if: steps.version_check.outputs.should_release == 'true'
run: |
TAG_NAME="v${{ steps.version_check.outputs.new_version }}"
echo "Creating tag: $TAG_NAME"
# Check if tag already exists
if git tag -l | grep -q "^$TAG_NAME$"; then
echo "[WARN] Tag $TAG_NAME already exists, skipping tag creation"
else
git tag "$TAG_NAME"
echo "[OK] Created tag: $TAG_NAME"
# Push tag with retry logic
for i in {1..3}; do
if git push origin "$TAG_NAME"; then
echo "[OK] Successfully pushed tag: $TAG_NAME"
break
else
echo "[ERROR] Tag push attempt $i failed, retrying in 5 seconds..."
sleep 5
fi
if [ $i -eq 3 ]; then
echo "[ERROR] Failed to push tag after 3 attempts"
exit 1
fi
done
fi
build-and-release:
needs: analyze-and-version
if: needs.analyze-and-version.outputs.should_release == 'true'
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macos-latest]
steps:
- uses: actions/checkout@v4
with:
ref: v${{ needs.analyze-and-version.outputs.new_version }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Set up Rust
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
- name: Check Rust formatting
run: cargo fmt --all -- --check
- name: Run Rust linting
run: cargo clippy --all-targets --all-features -- -D warnings
- name: Install maturin
run: pip install maturin
- name: Build wheels
run: maturin build --release --out dist --find-interpreter
- name: Upload wheels
uses: actions/upload-artifact@v4
with:
name: wheels-${{ matrix.os }}
path: dist/*.whl
build-sdist:
needs: analyze-and-version
if: needs.analyze-and-version.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: v${{ needs.analyze-and-version.outputs.new_version }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: ${{ env.PYTHON_VERSION }}
- name: Install maturin
run: pip install maturin
- name: Build source distribution
run: maturin sdist --out dist
- name: Upload sdist
uses: actions/upload-artifact@v4
with:
name: sdist
path: dist/*.tar.gz
publish:
needs: [analyze-and-version, build-and-release, build-sdist]
if: needs.analyze-and-version.outputs.should_release == 'true'
runs-on: ubuntu-latest
environment: release
steps:
- name: Download all artifacts
uses: actions/download-artifact@v4
with:
path: dist-artifacts
- name: Flatten artifacts
run: |
mkdir -p dist
find dist-artifacts -name "*.whl" -exec cp {} dist/ \;
find dist-artifacts -name "*.tar.gz" -exec cp {} dist/ \;
ls -la dist/
- name: Check if version already exists on PyPI
id: check_version
run: |
VERSION=${{ needs.analyze-and-version.outputs.new_version }}
echo "version=$VERSION" >> $GITHUB_OUTPUT
if pip index versions demopy_gb_jj 2>/dev/null | grep -q "$VERSION"; then
echo "exists=true" >> $GITHUB_OUTPUT
echo "[WARN] Version $VERSION already exists on PyPI"
else
echo "exists=false" >> $GITHUB_OUTPUT
echo "[OK] Version $VERSION is new, proceeding with upload"
fi
- name: Publish to PyPI
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist/
password: ${{ secrets.PYPI_API_TOKEN || '' }}
skip-existing: true
verbose: true
create-release:
needs: [analyze-and-version, publish]
if: needs.analyze-and-version.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: v${{ needs.analyze-and-version.outputs.new_version }}
- name: Generate changelog
id: changelog
run: |
# Get the previous tag
CURRENT_TAG="v${{ needs.analyze-and-version.outputs.new_version }}"
PREVIOUS_TAG=$(git describe --tags --abbrev=0 $CURRENT_TAG^ 2>/dev/null || echo "")
echo "Generating changelog from $PREVIOUS_TAG to $CURRENT_TAG"
# Generate changelog content
CHANGELOG="## What's Changed\n\n"
if [ -z "$PREVIOUS_TAG" ]; then
COMMITS=$(git log --oneline --pretty=format:"* %s (%h)" $CURRENT_TAG)
else
COMMITS=$(git log --oneline --pretty=format:"* %s (%h)" ${PREVIOUS_TAG}..${CURRENT_TAG})
fi
# Categorize commits
FEATURES=$(echo "$COMMITS" | grep -iE "(feat:|feature:)" || true)
FIXES=$(echo "$COMMITS" | grep -iE "(fix:|patch:)" || true)
CHORES=$(echo "$COMMITS" | grep -iE "(chore:|docs:|style:|refactor:|perf:|test:)" || true)
BREAKING=$(echo "$COMMITS" | grep -iE "(BREAKING CHANGE|breaking:|major:)" || true)
if [ ! -z "$BREAKING" ]; then
CHANGELOG="${CHANGELOG}### Breaking Changes\n${BREAKING}\n\n"
fi
if [ ! -z "$FEATURES" ]; then
CHANGELOG="${CHANGELOG}### New Features\n${FEATURES}\n\n"
fi
if [ ! -z "$FIXES" ]; then
CHANGELOG="${CHANGELOG}### Bug Fixes\n${FIXES}\n\n"
fi
if [ ! -z "$CHORES" ]; then
CHANGELOG="${CHANGELOG}### Maintenance\n${CHORES}\n\n"
fi
# Add installation instructions
CHANGELOG="${CHANGELOG}### Installation\n\n"
CHANGELOG="${CHANGELOG}\`\`\`bash\n"
CHANGELOG="${CHANGELOG}pip install demopy_gb_jj==${{ needs.analyze-and-version.outputs.new_version }}\n"
CHANGELOG="${CHANGELOG}\`\`\`\n\n"
# Add usage example
CHANGELOG="${CHANGELOG}### Usage\n\n"
CHANGELOG="${CHANGELOG}\`\`\`python\n"
CHANGELOG="${CHANGELOG}import demopy\n"
CHANGELOG="${CHANGELOG}print(demopy.hello()) # Hello from demopy_gb_jj!\n"
CHANGELOG="${CHANGELOG}print(demopy.add(5, 7)) # 12\n"
CHANGELOG="${CHANGELOG}\`\`\`\n"
# Save changelog to file and output
echo -e "$CHANGELOG" > changelog.md
echo "changelog<<EOF" >> $GITHUB_OUTPUT
echo -e "$CHANGELOG" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create GitHub Release
uses: softprops/action-gh-release@v1
with:
tag_name: v${{ needs.analyze-and-version.outputs.new_version }}
name: Release v${{ needs.analyze-and-version.outputs.new_version }}
body: ${{ steps.changelog.outputs.changelog }}
draft: false
prerelease: false
generate_release_notes: true