-
-
Notifications
You must be signed in to change notification settings - Fork 0
223 lines (190 loc) · 7.08 KB
/
Copy pathversion-bump.yml
File metadata and controls
223 lines (190 loc) · 7.08 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
name: Version Bump
on:
push:
branches:
- main
paths-ignore:
- '**.md'
- '.github/**'
- 'docs/**'
- 'examples/**'
- 'CHANGELOG.md'
- 'LICENSE'
workflow_dispatch:
inputs:
bump_type:
description: 'Version bump type'
required: true
type: choice
options:
- patch
- minor
- major
permissions:
contents: write
pull-requests: write
jobs:
detect-and-bump:
runs-on: ubuntu-latest
if: github.event_name == 'push' && !github.event.head_commit.message.startswith('chore: bump version')
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install gitpython
- name: Detect version bump type
id: detect
run: |
python << 'EOF'
import re
import subprocess
from datetime import datetime
# Get commits since last tag
try:
result = subprocess.run(
['git', 'describe', '--tags', '--abbrev=0'],
capture_output=True,
text=True,
check=True
)
last_tag = result.stdout.strip()
commits = subprocess.run(
['git', 'log', f'{last_tag}..HEAD', '--pretty=format:%s'],
capture_output=True,
text=True,
check=True
).stdout
except subprocess.CalledProcessError:
# No tags yet, get all commits
commits = subprocess.run(
['git', 'log', '--pretty=format:%s'],
capture_output=True,
text=True,
check=True
).stdout
bump_type = "patch" # default
# Check for breaking changes
if re.search(r'BREAKING CHANGE|!:', commits, re.IGNORECASE | re.MULTILINE):
bump_type = "minor"
# Check for features
elif re.search(r'^feat(\(.+\))?:', commits, re.IGNORECASE | re.MULTILINE):
bump_type = "minor"
# Fixes default to patch
elif re.search(r'^fix(\(.+\))?:', commits, re.IGNORECASE | re.MULTILINE):
bump_type = "patch"
import os
output_file = os.environ.get("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"bump_type={bump_type}\n")
print(f"bump_type={bump_type}")
EOF
- name: Get current version
id: current_version
run: |
python << 'EOF'
import re
from pathlib import Path
init_file = Path("ocpi/__init__.py")
content = init_file.read_text()
match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
current_version = match.group(1) if match else "2026.1.0"
import os
output_file = os.environ.get("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"current_version={current_version}\n")
print(f"current_version={current_version}")
EOF
- name: Bump version
id: bump
run: |
python scripts/bump_version.py ${{ steps.detect.outputs.bump_type }}
# Get new version
python << 'EOF'
import re
from pathlib import Path
init_file = Path("ocpi/__init__.py")
content = init_file.read_text()
match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
new_version = match.group(1) if match else ""
import os
output_file = os.environ.get("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"new_version={new_version}\n")
print(f"new_version={new_version}")
EOF
- name: Check if version changed
id: check
run: |
if [ "${{ steps.current_version.outputs.current_version }}" != "${{ steps.bump.outputs.new_version }}" ]; then
echo "changed=true" >> $GITHUB_OUTPUT
else
echo "changed=false" >> $GITHUB_OUTPUT
fi
- name: Create Pull Request
if: steps.check.outputs.changed == 'true'
uses: peter-evans/create-pull-request@v6
with:
token: ${{ secrets.GITHUB_TOKEN }}
commit-message: "chore: bump version to ${{ steps.bump.outputs.new_version }}"
title: "chore: bump version to ${{ steps.bump.outputs.new_version }}"
body: |
## Version Bump
This PR automatically bumps the version from `${{ steps.current_version.outputs.current_version }}` to `${{ steps.bump.outputs.new_version }}`.
**Bump type:** ${{ steps.detect.outputs.bump_type }}
**Triggered by:** Recent commits to main branch
- [ ] Review version change
- [ ] Merge to create tag and release
branch: version-bump-${{ steps.bump.outputs.new_version }}
delete-branch: true
manual-bump:
runs-on: ubuntu-latest
if: github.event_name == 'workflow_dispatch'
steps:
- uses: actions/checkout@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: '3.11'
- name: Bump version
id: bump
run: |
python scripts/bump_version.py ${{ github.event.inputs.bump_type }}
# Get new version
python << 'EOF'
import re
from pathlib import Path
init_file = Path("ocpi/__init__.py")
content = init_file.read_text()
match = re.search(r'__version__\s*=\s*["\']([^"\']+)["\']', content)
new_version = match.group(1) if match else ""
import os
output_file = os.environ.get("GITHUB_OUTPUT")
if output_file:
with open(output_file, "a") as f:
f.write(f"new_version={new_version}\n")
print(f"new_version={new_version}")
EOF
- name: Commit and push
run: |
git config --local user.email "action@github.com"
git config --local user.name "GitHub Action"
git add ocpi/__init__.py
git commit -m "chore: bump version to ${{ steps.bump.outputs.new_version }}"
git push
- name: Create tag
run: |
git tag -a "v${{ steps.bump.outputs.new_version }}" -m "Release ${{ steps.bump.outputs.new_version }}"
git push origin "v${{ steps.bump.outputs.new_version }}"