-
Notifications
You must be signed in to change notification settings - Fork 0
265 lines (229 loc) · 8.92 KB
/
release.yml
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
name: Release
on:
workflow_dispatch:
pull_request:
types: [closed]
push:
branches:
- main
permissions:
contents: write
pull-requests: write
packages: write
jobs:
build:
# Skip build on PR close if it wasn't merged
if: github.event_name != 'pull_request' || github.event.pull_request.merged == true
runs-on: ubuntu-latest
outputs:
build_success: ${{ steps.set_output.outputs.success }}
steps:
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: "3.8"
cache: "pip"
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r pr-diff-bot/requirements.txt
- name: Build Docker image
run: docker build -t pr-diff-analyzer .
- name: Cache Docker layers
uses: actions/cache@v3
with:
path: /tmp/.buildx-cache
key: ${{ runner.os }}-buildx-${{ github.sha }}
restore-keys: |
${{ runner.os }}-buildx-
- name: Set build output
id: set_output
run: echo "success=true" >> $GITHUB_OUTPUT
check-release:
needs: build
# Only run on push to main that's not a release commit
if: github.event_name == 'push' && !contains(github.event.head_commit.message, 'chore(release):')
runs-on: ubuntu-latest
outputs:
new_version: ${{ steps.version.outputs.NEW_VERSION }}
should_release: ${{ steps.check.outputs.should_release }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Check if release is needed
id: check
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
echo "should_release=true" >> $GITHUB_OUTPUT
elif [[ "${{ github.event_name }}" == "push" ]]; then
echo "should_release=true" >> $GITHUB_OUTPUT
else
echo "should_release=false" >> $GITHUB_OUTPUT
fi
- name: Set up Python
if: steps.check.outputs.should_release == 'true'
uses: actions/setup-python@v5
with:
python-version: "3.8"
- name: Install dependencies
if: steps.check.outputs.should_release == 'true'
run: |
python -m pip install --upgrade pip
pip install python-semantic-release
- name: Get Next Version
if: steps.check.outputs.should_release == 'true'
id: version
run: |
# Get current version from __init__.py
CURRENT_VERSION=$(grep -oP '__version__ = "\K[^"]+' pr-diff-bot/__init__.py)
echo "Current version: $CURRENT_VERSION"
# Get commit messages since last tag
COMMITS=$(git log --format=%B $(git describe --tags --abbrev=0 2>/dev/null || git rev-list --max-parents=0 HEAD)..HEAD)
echo "Commits since last tag:"
echo "$COMMITS"
# Check for fix commits
if echo "$COMMITS" | grep -q "^fix:"; then
# Calculate patch version
IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
NEW_VERSION="$major.$minor.$((patch + 1))"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
elif echo "$COMMITS" | grep -q "^feat:"; then
# Calculate minor version
IFS='.' read -r major minor patch <<< "$CURRENT_VERSION"
NEW_VERSION="$major.$((minor + 1)).0"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
fi
create-release-pr:
needs: [build, check-release]
if: needs.check-release.outputs.should_release == 'true' && needs.check-release.outputs.new_version != ''
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN }}
- name: Configure Git
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
- name: Create Release Branch and Update Version
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
BRANCH="release-${{ needs.check-release.outputs.new_version }}"
git checkout -b $BRANCH
# Update version in __init__.py
sed -i "s/__version__ = .*/__version__ = \"${{ needs.check-release.outputs.new_version }}\"/" pr-diff-bot/__init__.py
git add .
git commit -m "chore(release): ${{ needs.check-release.outputs.new_version }}"
git push -f origin $BRANCH
- name: Get changes since last tag
id: changes
run: |
PREVIOUS_TAG=$(git describe --tags --abbrev=0 2>/dev/null || echo "")
if [ -z "$PREVIOUS_TAG" ]; then
# First release - get all commits
CHANGES=$(git log --format="* %s" --reverse)
else
# Get commits since last tag
CHANGES=$(git log --format="* %s" --reverse ${PREVIOUS_TAG}..HEAD)
fi
echo "CHANGES<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Create Pull Request
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
gh pr create \
--title "chore(release): ${{ needs.check-release.outputs.new_version }}" \
--body "Automated release PR
This PR was automatically created by the release workflow to update the version to ${{ needs.check-release.outputs.new_version }}.
Changes included in this release:
${{ steps.changes.outputs.CHANGES }}" \
--base main \
--head "release-${{ needs.check-release.outputs.new_version }}"
tag-and-release:
# Only run when a release PR is merged (not just closed)
if: |
github.event_name == 'pull_request' &&
github.event.action == 'closed' &&
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release-')
runs-on: ubuntu-latest
outputs:
version: ${{ steps.get_version.outputs.VERSION }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN }}
- name: Configure Git
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
- name: Get version from branch name
id: get_version
run: |
VERSION=$(echo "${{ github.event.pull_request.head.ref }}" | sed 's/release-//')
echo "VERSION=$VERSION" >> $GITHUB_OUTPUT
- name: Create and push tag
run: |
git tag -a "v${{ steps.get_version.outputs.VERSION }}" -m "Release v${{ steps.get_version.outputs.VERSION }}"
git push origin "v${{ steps.get_version.outputs.VERSION }}"
- name: Create GitHub Release
env:
GITHUB_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
# Get changes since last release
PREVIOUS_TAG=$(git describe --tags --abbrev=0 "v${{ steps.get_version.outputs.VERSION }}^" 2>/dev/null || echo "")
if [ -z "$PREVIOUS_TAG" ]; then
# First release - get all commits
CHANGES=$(git log --format="* %s" --reverse)
else
# Get commits between tags
CHANGES=$(git log --format="* %s" --reverse ${PREVIOUS_TAG}..v${{ steps.get_version.outputs.VERSION }})
fi
# Create GitHub release
gh release create "v${{ steps.get_version.outputs.VERSION }}" \
--title "Release v${{ steps.get_version.outputs.VERSION }}" \
--notes "## Changes in this release:
${CHANGES}"
publish:
needs: [build, tag-and-release]
if: |
needs.build.outputs.build_success == 'true' &&
needs.tag-and-release.outputs.version != ''
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: "v${{ needs.tag-and-release.outputs.version }}"
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Get lowercase repository name
id: repo
run: |
REPO="${{ github.repository }}"
echo "name=${REPO,,}" >> $GITHUB_OUTPUT
- name: Build and push
uses: docker/build-push-action@v5
with:
push: true
cache-from: type=local,src=/tmp/.buildx-cache
cache-to: type=local,dest=/tmp/.buildx-cache-new,mode=max
tags: |
ghcr.io/${{ steps.repo.outputs.name }}:latest
ghcr.io/${{ steps.repo.outputs.name }}:${{ needs.tag-and-release.outputs.version }}
- name: Move cache
run: |
rm -rf /tmp/.buildx-cache
mv /tmp/.buildx-cache-new /tmp/.buildx-cache