-
Notifications
You must be signed in to change notification settings - Fork 0
249 lines (220 loc) · 7.84 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
name: Release
on:
workflow_dispatch:
pull_request:
types: [closed]
push:
branches:
- main
- "feature/*"
- "fix/*"
permissions:
contents: write
pull-requests: write
packages: write
jobs:
check-release:
# Skip if commit is a release commit
if: ${{ !contains(github.event.head_commit.message, 'chore(release):') }}
runs-on: ubuntu-latest
outputs:
should_release: ${{ steps.check.outputs.should_release }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- 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 python-semantic-release==7.34.6
- name: Check if release is needed
id: check
run: |
if [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then
# Check if there are changes that warrant a new version
NEW_VERSION=$(semantic-release version --noop)
if [ -z "$NEW_VERSION" ]; then
echo "No version bump needed"
echo "should_release=false" >> $GITHUB_OUTPUT
else
echo "Version bump needed: $NEW_VERSION"
echo "should_release=true" >> $GITHUB_OUTPUT
fi
elif [[ "${{ github.event_name }}" == "push" ]]; then
# Check if there are changes that warrant a new version
NEW_VERSION=$(semantic-release version --noop)
if [ -z "$NEW_VERSION" ]; then
echo "No version bump needed"
echo "should_release=false" >> $GITHUB_OUTPUT
else
echo "Version bump needed: $NEW_VERSION"
echo "should_release=true" >> $GITHUB_OUTPUT
fi
else
echo "should_release=false" >> $GITHUB_OUTPUT
fi
create-release-pr:
needs: check-release
if: needs.check-release.outputs.should_release == 'true'
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
token: ${{ secrets.PAT_TOKEN }}
- 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 python-semantic-release==7.34.6
- name: Configure Git
run: |
git config --global user.name "github-actions"
git config --global user.email "[email protected]"
- name: Create Release PR
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
# Get next version
NEW_VERSION=$(semantic-release version --noop)
if [ -z "$NEW_VERSION" ]; then
echo "No version bump needed"
exit 0
fi
# Determine branch type and target
if [[ "${{ github.ref }}" == refs/heads/feature/* ]]; then
BASE_BRANCH="feature/$(echo ${{ github.ref_name }} | cut -d/ -f2-)"
PRERELEASE_TYPE="alpha"
elif [[ "${{ github.ref }}" == refs/heads/fix/* ]]; then
BASE_BRANCH="fix/$(echo ${{ github.ref_name }} | cut -d/ -f2-)"
PRERELEASE_TYPE="beta"
else
BASE_BRANCH="main"
PRERELEASE_TYPE=""
fi
# Create release branch
BRANCH="release-$NEW_VERSION"
git checkout -b $BRANCH
# Update version and create changelog
if [ -n "$PRERELEASE_TYPE" ]; then
semantic-release version --prerelease $PRERELEASE_TYPE --no-commit
else
semantic-release version --no-commit
fi
# Commit changes
git add .
git commit -m "chore(release): $NEW_VERSION"
git push -f origin $BRANCH
# Create PR
if [ -n "$PRERELEASE_TYPE" ]; then
gh pr create \
--title "chore(prerelease): $NEW_VERSION" \
--body "Automated prerelease PR to update version to $NEW_VERSION" \
--base $BASE_BRANCH \
--head $BRANCH
else
gh pr create \
--title "chore(release): $NEW_VERSION" \
--body "Automated release PR to update version to $NEW_VERSION" \
--base main \
--head $BRANCH
fi
release:
# Only run when a release PR is merged
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: 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 python-semantic-release==7.34.6
- 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: Check and Create Release
env:
GH_TOKEN: ${{ secrets.PAT_TOKEN }}
run: |
# Check if release already exists
if gh release view "v${{ steps.get_version.outputs.VERSION }}" &>/dev/null; then
echo "Release v${{ steps.get_version.outputs.VERSION }} already exists. Skipping release creation."
exit 0
fi
# Create and push tag
git tag -f "v${{ steps.get_version.outputs.VERSION }}" -m "Release v${{ steps.get_version.outputs.VERSION }}"
git push --force origin "v${{ steps.get_version.outputs.VERSION }}"
# Determine if this is a prerelease
if [[ "${{ github.ref }}" == refs/heads/feature/* ]]; then
IS_PRERELEASE="--prerelease"
TARGET_BRANCH="${{ github.ref_name }}"
elif [[ "${{ github.ref }}" == refs/heads/fix/* ]]; then
IS_PRERELEASE="--prerelease"
TARGET_BRANCH="${{ github.ref_name }}"
else
IS_PRERELEASE=""
TARGET_BRANCH="main"
fi
# Create GitHub release
gh release create "v${{ steps.get_version.outputs.VERSION }}" \
--title "Release v${{ steps.get_version.outputs.VERSION }}" \
--notes "See CHANGELOG.md for details" \
--target $TARGET_BRANCH \
$IS_PRERELEASE
publish:
needs: release
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- 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
platforms: linux/amd64,linux/arm64
tags: |
ghcr.io/${{ steps.repo.outputs.name }}:latest
ghcr.io/${{ steps.repo.outputs.name }}:${{ needs.release.outputs.version }}