-
Notifications
You must be signed in to change notification settings - Fork 6
334 lines (267 loc) · 11.2 KB
/
auto-release.yml
File metadata and controls
334 lines (267 loc) · 11.2 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
name: Auto Release from Issue
# Trigger when an issue is labeled with 'release'
on:
issues:
types: [labeled]
# Also trigger when PR is merged (to create tag after merge)
pull_request:
types: [closed]
branches: [main]
permissions:
contents: write
issues: write
pull-requests: write
env:
# Only these users can trigger releases (comma-separated GitHub usernames)
ALLOWED_USERS: "mateof"
jobs:
# ===========================================
# JOB 1: Create PR with version bump
# ===========================================
create-version-pr:
name: Create Version Bump PR
runs-on: ubuntu-latest
# Only run on issue events with 'release' label
if: |
github.event_name == 'issues' &&
contains(github.event.issue.labels.*.name, 'release')
steps:
- name: '🔐 Verify user authorization'
env:
ISSUE_AUTHOR: ${{ github.event.issue.user.login }}
run: |
echo "Issue author: $ISSUE_AUTHOR"
echo "Allowed users: $ALLOWED_USERS"
if [[ ! ",$ALLOWED_USERS," == *",$ISSUE_AUTHOR,"* ]]; then
echo "❌ User '$ISSUE_AUTHOR' is not authorized to create releases"
exit 1
fi
echo "✅ User '$ISSUE_AUTHOR' is authorized"
- name: '📋 Extract version from issue title'
id: version
env:
ISSUE_TITLE: ${{ github.event.issue.title }}
run: |
echo "Issue title: $ISSUE_TITLE"
# Extract version (supports: v3.5.0, 3.5.0, v3.5.0.0)
VERSION=$(echo "$ISSUE_TITLE" | grep -oE '[vV]?[0-9]+\.[0-9]+\.[0-9]+(\.[0-9]+)?' | head -1)
if [ -z "$VERSION" ]; then
echo "❌ No version found in issue title"
exit 1
fi
# Remove 'v' prefix
VERSION=${VERSION#v}
VERSION=${VERSION#V}
# 4-part version for csproj
PARTS=$(echo "$VERSION" | tr '.' '\n' | wc -l)
if [ "$PARTS" -eq 3 ]; then
VERSION_CSPROJ="${VERSION}.0"
else
VERSION_CSPROJ="$VERSION"
fi
VERSION_TAG="v${VERSION}"
BRANCH_NAME="release/${VERSION}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "version_csproj=$VERSION_CSPROJ" >> $GITHUB_OUTPUT
echo "version_tag=$VERSION_TAG" >> $GITHUB_OUTPUT
echo "branch_name=$BRANCH_NAME" >> $GITHUB_OUTPUT
echo "✅ Version: $VERSION | CSPROJ: $VERSION_CSPROJ | Tag: $VERSION_TAG"
- name: '📄 Checkout main branch'
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
- name: '🔍 Check if tag already exists'
run: |
TAG="${{ steps.version.outputs.version_tag }}"
if git rev-parse "$TAG" >/dev/null 2>&1; then
echo "❌ Tag '$TAG' already exists!"
exit 1
fi
echo "✅ Tag '$TAG' does not exist"
- name: '🌿 Create release branch'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ steps.version.outputs.branch_name }}"
# Delete existing release branch if it exists (from a previous failed run)
if git ls-remote --heads origin "$BRANCH" | grep -q "$BRANCH"; then
echo "ℹ️ Branch '$BRANCH' already exists, deleting it..."
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/$BRANCH" || true
fi
git checkout -b "$BRANCH"
- name: '📝 Update version in TelegramDownloader.csproj'
run: |
VERSION_CSPROJ="${{ steps.version.outputs.version_csproj }}"
sed -i "s|<Version>[^<]*</Version>|<Version>$VERSION_CSPROJ</Version>|g" "TelegramDownloader/TelegramDownloader.csproj"
echo "✅ TelegramDownloader version updated to $VERSION_CSPROJ"
- name: '📝 Update version in TFMAudioApp.csproj (if exists)'
run: |
VERSION="${{ steps.version.outputs.version }}"
VERSION_CSPROJ="${{ steps.version.outputs.version_csproj }}"
CSPROJ="TFMAudioApp/TFMAudioApp.csproj"
if [ -f "$CSPROJ" ]; then
if grep -q "ApplicationDisplayVersion" "$CSPROJ"; then
sed -i "s|<ApplicationDisplayVersion>[^<]*</ApplicationDisplayVersion>|<ApplicationDisplayVersion>$VERSION</ApplicationDisplayVersion>|g" "$CSPROJ"
fi
if grep -q "<Version>" "$CSPROJ"; then
sed -i "s|<Version>[^<]*</Version>|<Version>$VERSION_CSPROJ</Version>|g" "$CSPROJ"
fi
echo "✅ TFMAudioApp version updated"
fi
- name: '💾 Commit and push changes'
run: |
VERSION="${{ steps.version.outputs.version }}"
BRANCH="${{ steps.version.outputs.branch_name }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git add -A
if git diff --staged --quiet; then
echo "ℹ️ No changes to commit"
else
git commit -m "Bump version to $VERSION"
git push origin "$BRANCH"
echo "✅ Changes pushed to $BRANCH"
fi
- name: '🔀 Create Pull Request'
id: create_pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION="${{ steps.version.outputs.version }}"
VERSION_TAG="${{ steps.version.outputs.version_tag }}"
BRANCH="${{ steps.version.outputs.branch_name }}"
ISSUE_NUMBER="${{ github.event.issue.number }}"
ISSUE_BODY="${{ github.event.issue.body }}"
PR_URL=$(gh pr create \
--base main \
--head "$BRANCH" \
--title "Release $VERSION_TAG" \
--body "## 🚀 Release $VERSION_TAG
This PR bumps the version to **$VERSION** and will trigger a release when merged.
### Changes
- Updated \`TelegramDownloader.csproj\` version to \`${{ steps.version.outputs.version_csproj }}\`
- Updated \`TFMAudioApp.csproj\` version (if applicable)
### Release Notes
$ISSUE_BODY
---
Closes #$ISSUE_NUMBER
> ⚠️ **After merging**, the release workflow will automatically:
> 1. Create tag \`$VERSION_TAG\`
> 2. Create GitHub Release
> 3. Build and upload binaries
> 4. Sync \`develop\` branch with \`main\`")
echo "pr_url=$PR_URL" >> $GITHUB_OUTPUT
echo "✅ PR created: $PR_URL"
- name: '💬 Comment on issue'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION_TAG="${{ steps.version.outputs.version_tag }}"
PR_URL="${{ steps.create_pr.outputs.pr_url }}"
gh issue comment ${{ github.event.issue.number }} --body "## 📋 Release PR Created
A pull request has been created to bump the version to **$VERSION_TAG**:
👉 $PR_URL
**Next steps:**
1. Review and approve the PR
2. Merge the PR to \`main\`
3. The release will be created automatically after merge
---
*This comment was automatically generated.*"
- name: '❌ Comment on failure'
if: failure()
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
gh issue comment ${{ github.event.issue.number }} --body "## ❌ Release PR Creation Failed
Please check the [workflow logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}).
Common issues:
- Version format incorrect (use: v3.5.0)
- Tag already exists
- User not authorized
---
*This comment was automatically generated.*"
# ===========================================
# JOB 2: Create tag and release after PR merge
# ===========================================
create-release-after-merge:
name: Create Release After Merge
runs-on: ubuntu-latest
# Only run when a release PR is merged
if: |
github.event_name == 'pull_request' &&
github.event.pull_request.merged == true &&
startsWith(github.event.pull_request.head.ref, 'release/')
steps:
- name: '📋 Extract version from branch name'
id: version
run: |
BRANCH="${{ github.event.pull_request.head.ref }}"
# Extract version from branch name (release/3.5.0 -> 3.5.0)
VERSION=${BRANCH#release/}
VERSION_TAG="v${VERSION}"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "version_tag=$VERSION_TAG" >> $GITHUB_OUTPUT
echo "✅ Version: $VERSION | Tag: $VERSION_TAG"
- name: '📄 Checkout main'
uses: actions/checkout@v4
with:
ref: main
fetch-depth: 0
token: ${{ secrets.TOKENBuild }}
- name: '🏷️ Create and push tag'
env:
GH_TOKEN: ${{ secrets.TOKENBuild }}
run: |
TAG="${{ steps.version.outputs.version_tag }}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
git tag -a "$TAG" -m "Release $TAG"
git push origin "$TAG"
echo "✅ Tag '$TAG' created and pushed"
- name: '🚀 Create GitHub Release'
env:
GH_TOKEN: ${{ secrets.TOKENBuild }}
PR_BODY: ${{ github.event.pull_request.body }}
run: |
TAG="${{ steps.version.outputs.version_tag }}"
# Write PR body to file to avoid shell interpretation issues
echo "$PR_BODY" > release_notes.md
gh release create "$TAG" \
--title "Release $TAG" \
--notes-file release_notes.md \
--target main
echo "✅ GitHub Release created"
- name: '🗑️ Delete release branch'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
BRANCH="${{ github.event.pull_request.head.ref }}"
gh api -X DELETE "repos/${{ github.repository }}/git/refs/heads/$BRANCH" || true
echo "✅ Branch '$BRANCH' deleted"
- name: '🔄 Create PR to sync develop with main'
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
VERSION_TAG="${{ steps.version.outputs.version_tag }}"
SYNC_BRANCH="sync/main-to-develop-${VERSION_TAG}"
git config user.name "github-actions[bot]"
git config user.email "github-actions[bot]@users.noreply.github.com"
# Check if develop branch exists
if ! git ls-remote --heads origin develop | grep -q develop; then
echo "ℹ️ develop branch does not exist, skipping sync"
exit 0
fi
# Create sync branch from main
git checkout -b "$SYNC_BRANCH"
git push origin "$SYNC_BRANCH"
# Create PR to merge into develop
PR_URL=$(gh pr create \
--base develop \
--head "$SYNC_BRANCH" \
--title "Sync develop with main after $VERSION_TAG" \
--body "## 🔄 Sync develop with main
This PR syncs the \`develop\` branch with \`main\` after release **$VERSION_TAG**.
---
*This PR was automatically generated.*")
echo "✅ Sync PR created: $PR_URL"