-
Notifications
You must be signed in to change notification settings - Fork 20
212 lines (194 loc) · 8.34 KB
/
create-release-branch.yml
File metadata and controls
212 lines (194 loc) · 8.34 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
name: Create Release Branch
on:
workflow_dispatch:
inputs:
issue_number:
description: 'Issue number for traceability'
type: string
required: true
dry_run:
description: 'Dry run: simulate the process without making any changes'
type: boolean
required: false
default: false
permissions:
contents: read
concurrency:
group: create-release-branch
cancel-in-progress: false
jobs:
validate-permissions:
name: Validate permissions
runs-on: ubuntu-latest
steps:
- name: Check default branch
env:
REF: ${{ github.ref_name }}
DEFAULT_BRANCH: ${{ github.event.repository.default_branch }}
run: |
if [ "$REF" != "$DEFAULT_BRANCH" ]; then
echo "::error::This workflow can only be run from the default branch (${DEFAULT_BRANCH}), but was triggered from '${REF}'."
exit 1
fi
- name: Check admin permission
env:
GH_TOKEN: ${{ github.token }}
REPO: ${{ github.repository }}
ACTOR: ${{ github.actor }}
run: |
permission=$(gh api "repos/${REPO}/collaborators/${ACTOR}/permission" --jq '.permission')
if [ "$permission" != "admin" ]; then
echo "::error::User ${ACTOR} does not have admin permission (current: ${permission})"
exit 1
fi
echo "User ${ACTOR} has admin permission"
create-release-branch:
name: Create release branch
needs: validate-permissions
runs-on: ubuntu-latest
outputs:
release_branch: ${{ steps.detect-version.outputs.release_branch }}
current_major: ${{ steps.detect-version.outputs.current_major }}
current_minor: ${{ steps.detect-version.outputs.current_minor }}
branch_created: ${{ steps.check-branch.outputs.branch_created }}
steps:
- name: Get GitHub App token
id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.APP_REPO_WRITER_ID }}
private-key: ${{ secrets.APP_REPO_WRITER_PRIVATE_KEY }}
- name: Checkout master
uses: actions/checkout@v6
with:
ref: master
fetch-depth: 0
token: ${{ steps.app-token.outputs.token }}
- name: Detect current version
id: detect-version
run: |
revision=$(grep -oP '<revision>\K[^<]+' pom.xml)
current_major=$(echo "$revision" | cut -d. -f1)
current_minor=$(echo "$revision" | cut -d. -f2)
release_branch="release-${current_major}.${current_minor}"
echo "current_major=$current_major" >> "$GITHUB_OUTPUT"
echo "current_minor=$current_minor" >> "$GITHUB_OUTPUT"
echo "release_branch=$release_branch" >> "$GITHUB_OUTPUT"
echo "Detected version: ${current_major}.${current_minor}"
echo "Release branch: $release_branch"
- name: Check if release branch already exists
id: check-branch
env:
RELEASE_BRANCH: ${{ steps.detect-version.outputs.release_branch }}
run: |
if git ls-remote --exit-code --heads origin "$RELEASE_BRANCH" > /dev/null 2>&1; then
echo "::warning::Branch ${RELEASE_BRANCH} already exists on remote. Skipping."
echo "branch_created=false" >> "$GITHUB_OUTPUT"
else
echo "Branch ${RELEASE_BRANCH} does not exist. Proceeding."
echo "branch_created=true" >> "$GITHUB_OUTPUT"
fi
- name: Create and push release branch
if: inputs.dry_run != true && steps.check-branch.outputs.branch_created == 'true'
env:
RELEASE_BRANCH: ${{ steps.detect-version.outputs.release_branch }}
run: |
git checkout -b "$RELEASE_BRANCH"
git push origin "$RELEASE_BRANCH"
echo "Pushed branch $RELEASE_BRANCH"
- name: Dry run — skip push
if: inputs.dry_run == true && steps.check-branch.outputs.branch_created == 'true'
env:
RELEASE_BRANCH: ${{ steps.detect-version.outputs.release_branch }}
run: echo "Dry run — would create branch $RELEASE_BRANCH"
create-version-bump-pr:
name: Create version bump PR
needs: create-release-branch
runs-on: ubuntu-latest
if: inputs.dry_run != true && needs.create-release-branch.outputs.branch_created == 'true'
outputs:
pr_url: ${{ steps.create-pr.outputs.pr_url }}
steps:
- name: Get GitHub App token
id: app-token
uses: actions/create-github-app-token@v2
with:
app-id: ${{ vars.APP_REPO_WRITER_ID }}
private-key: ${{ secrets.APP_REPO_WRITER_PRIVATE_KEY }}
- name: Checkout master
uses: actions/checkout@v6
with:
ref: master
token: ${{ steps.app-token.outputs.token }}
- name: Calculate next version
id: next-version
env:
CURRENT_MAJOR: ${{ needs.create-release-branch.outputs.current_major }}
CURRENT_MINOR: ${{ needs.create-release-branch.outputs.current_minor }}
run: |
next_minor=$((CURRENT_MINOR + 1))
echo "next_minor=$next_minor" >> "$GITHUB_OUTPUT"
echo "Next version: ${CURRENT_MAJOR}.${next_minor}"
- name: Create bump branch and update version
env:
CURRENT_MAJOR: ${{ needs.create-release-branch.outputs.current_major }}
NEXT_MINOR: ${{ steps.next-version.outputs.next_minor }}
run: |
bump_branch="chore/bump-version-${CURRENT_MAJOR}.${NEXT_MINOR}"
git checkout -b "$bump_branch"
sed -i "s|<revision>${CURRENT_MAJOR}\.[0-9]\+</revision>|<revision>${CURRENT_MAJOR}.${NEXT_MINOR}</revision>|" pom.xml
echo "Updated pom.xml:"
grep '<revision>' pom.xml
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git add pom.xml
git commit -m "Bump version for next release ${CURRENT_MAJOR}.${NEXT_MINOR}"
git push origin "$bump_branch"
- name: Create PR
id: create-pr
env:
GH_TOKEN: ${{ steps.app-token.outputs.token }}
CURRENT_MAJOR: ${{ needs.create-release-branch.outputs.current_major }}
CURRENT_MINOR: ${{ needs.create-release-branch.outputs.current_minor }}
NEXT_MINOR: ${{ steps.next-version.outputs.next_minor }}
ISSUE_NUMBER: ${{ inputs.issue_number }}
run: |
body="Bump \`revision\` to \`${CURRENT_MAJOR}.${NEXT_MINOR}\` following the creation of \`release-${CURRENT_MAJOR}.${CURRENT_MINOR}\`."
body="${body}"$'\n\n'"Issue: ${ISSUE_NUMBER}"
pr_url=$(gh pr create \
--head "chore/bump-version-${CURRENT_MAJOR}.${NEXT_MINOR}" \
--base master \
--title "chore: bump version to ${CURRENT_MAJOR}.${NEXT_MINOR}" \
--body "$body")
echo "pr_url=$pr_url" >> "$GITHUB_OUTPUT"
echo "Created PR: $pr_url"
summary:
name: Summary
needs: [create-release-branch, create-version-bump-pr]
runs-on: ubuntu-latest
if: always()
steps:
- name: Write summary
env:
DRY_RUN: ${{ inputs.dry_run }}
RELEASE_BRANCH: ${{ needs.create-release-branch.outputs.release_branch }}
PR_URL: ${{ needs.create-version-bump-pr.outputs.pr_url }}
CURRENT_MAJOR: ${{ needs.create-release-branch.outputs.current_major }}
CURRENT_MINOR: ${{ needs.create-release-branch.outputs.current_minor }}
run: |
echo "## Release Branch Creation — JavaClasses" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
if [ "$DRY_RUN" == "true" ]; then
echo "### Dry run — no changes were made" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "- Detected version: **${CURRENT_MAJOR}.${CURRENT_MINOR}**" >> "$GITHUB_STEP_SUMMARY"
echo "- Would create branch: \`${RELEASE_BRANCH}\`" >> "$GITHUB_STEP_SUMMARY"
else
echo "- Release branch: \`${RELEASE_BRANCH}\`" >> "$GITHUB_STEP_SUMMARY"
if [ -n "$PR_URL" ]; then
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "### Next steps" >> "$GITHUB_STEP_SUMMARY"
echo "" >> "$GITHUB_STEP_SUMMARY"
echo "- [ ] Review and approve version bump PR: $PR_URL" >> "$GITHUB_STEP_SUMMARY"
fi
fi