Skip to content

Commit d922be3

Browse files
committed
Make version bumps an action
1 parent 0aac2b6 commit d922be3

File tree

3 files changed

+138
-100
lines changed

3 files changed

+138
-100
lines changed
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
name: "Branch Version Update"
2+
description: "Creates a PR to update the version of a specific branch."
3+
4+
# The target branch when starting this workflow should be:
5+
# "branch/{major}.{minor}.x" if it exists, or "main"
6+
7+
inputs:
8+
new_version:
9+
description: "Version 'X.Y.Z' for the release branch."
10+
type: string
11+
required: true
12+
default: "0.0.0"
13+
target_branch:
14+
description: "Target branch for the version update"
15+
type: string
16+
required: false
17+
default: "main"
18+
force:
19+
description: "Enable overwriting existing PR branches (this does not force overwrite the target branch or skip creating a PR)"
20+
type: boolean
21+
required: true
22+
default: false
23+
24+
runs:
25+
using: "composite"
26+
steps:
27+
- name: Checkout the repository
28+
uses: actions/checkout@v4
29+
with:
30+
ref: ${{ inputs.target_branch }}
31+
32+
- name: Prepare environment
33+
id: prepare-env
34+
run: |
35+
log_and_export_vars() {
36+
for var in "$@"; do
37+
printf "%-15s %s\n" "$var:" "${!var}" | tee -a $GITHUB_STEP_SUMMARY
38+
echo "${var}=${!var}" | tee -a $GITHUB_ENV | tee -a $GITHUB_OUTPUT
39+
done
40+
}
41+
42+
full_version=${{ inputs.new_version }}
43+
major_version=$(echo ${full_version} | cut -d. -f1)
44+
minor_version=$(echo ${full_version} | cut -d. -f2)
45+
patch_version=$(echo ${full_version} | cut -d. -f3)
46+
branch_name=${{ inputs.target_branch }}
47+
enable_force_push="${{ inputs.force }}"
48+
pr_title="[Version] Update ${branch_name} to v${full_version}"
49+
pr_body="Bump ${branch_name} to ${full_version}."
50+
pr_branch="pr/ver/${branch_name}-v${full_version}"
51+
52+
log_and_export_vars \
53+
full_version major_version minor_version patch_version \
54+
branch_name pr_title pr_branch pr_body enable_force_push
55+
56+
echo "Branch ref: $GITHUB_REF" | tee -a $GITHUB_STEP_SUMMARY
57+
echo "Branch SHA: $GITHUB_SHA" | tee -a $GITHUB_STEP_SUMMARY
58+
echo "Branch commit: $(git show --oneline --no-patch ${GITHUB_SHA})" | tee -a $GITHUB_STEP_SUMMARY
59+
60+
- name: Verify environment
61+
id: verify-env
62+
run: |
63+
# Target branch must already exist
64+
if ! git ls-remote --exit-code origin ${branch_name}; then
65+
echo " Target branch must already exist" | tee -a $GITHUB_STEP_SUMMARY
66+
exit 1
67+
fi
68+
69+
#Ensure that target branch version is compatible.
70+
if [[ "${branch_name}" =~ ^branch/[0-9]+\.[0-9]+\.x$ ]]; then
71+
branch_version=$(echo ${branch_name} | cut -d/ -f1 --complement)
72+
branch_major=$(echo ${branch_version} | cut -d. -f1)
73+
branch_minor=$(echo ${branch_version} | cut -d. -f2)
74+
if [ "${branch_major}" != "${major_version}" ]; then
75+
echo " Target branch major version mismatch"
76+
exit 1
77+
fi;
78+
if [ "${branch_minor}" != "${minor_version}" ]; then
79+
echo " Target branch minor version mismatch"
80+
exit 1
81+
fi
82+
fi
83+
84+
# PR branch must *not* exist
85+
if [ "${enable_force_push}" == "false" ]; then
86+
if git ls-remote --exit-code origin ${pr_branch}; then
87+
echo " PR branch cannot already exist - Delete branch and retry workflow or enable 'force'" | tee -a $GITHUB_STEP_SUMMARY
88+
exit 1
89+
fi
90+
fi
91+
92+
if [[ ! $full_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
93+
echo "Invalid version number: $full_version"
94+
exit 1
95+
fi
96+
97+
- name: Update version numbers in target branch
98+
id: create-pr-branch
99+
run: |
100+
git checkout -b ${pr_branch}
101+
echo "::group::Running update_version.sh"
102+
./ci/update_version.sh ${major_version} ${minor_version} ${patch_version}
103+
echo "::endgroup::"
104+
105+
if ! git diff --quiet; then
106+
echo "::group::Diff"
107+
git diff
108+
echo "::endgroup::"
109+
110+
git add .
111+
112+
git config user.name "github-actions[bot]"
113+
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
114+
git commit -m "${pr_body}"
115+
116+
# Push the changes to the release branch:
117+
git push --force origin ${pr_branch}
118+
fi
119+
120+
- name: Create pull request for target branch
121+
id: create-pr
122+
run: |
123+
gh pr create \
124+
-B "${branch_name}" \
125+
-b "${pr_body}" \
126+
-t "${pr_title}" \
127+
-H "${pr_branch}"

.github/workflows/release-create-new.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ jobs:
107107
git show --oneline --no-patch HEAD | tee -a $GITHUB_STEP_SUMMARY
108108
109109
- name: Update version numbers in main
110-
id: update_main
111-
run: |
112-
gh workflow run update-branch-version.yml --ref main -f new_version="$main_version" -f target_branch="main" | tee -a $GITHUB_STEP_SUMMARY
110+
uses: ${{ github.repository }}/.github/actions/version-update@main
111+
with:
112+
new_version: ${{ inputs.main_version }}
113+
target_branch: "main"
113114

114115
- name: Notify Slack
115116
if: ${{ success()}}
@@ -142,4 +143,4 @@ jobs:
142143
slack-message: |
143144
An error has occurred while initiating a new release cycle for `v${{ env.BRANCH_VERSION }}`.
144145
145-
Details: ${{ env.SUMMARY_URL }}
146+
Details: ${{ env.SUMMARY_URL }}

.github/workflows/update-branch-version.yml

Lines changed: 6 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -55,99 +55,9 @@ jobs:
5555
with:
5656
ref: ${{ inputs.target_branch }}
5757

58-
- name: Prepare environment
59-
id: prepare-env
60-
run: |
61-
log_and_export_vars() {
62-
for var in "$@"; do
63-
printf "%-15s %s\n" "$var:" "${!var}" | tee -a $GITHUB_STEP_SUMMARY
64-
echo "${var}=${!var}" | tee -a $GITHUB_ENV | tee -a $GITHUB_OUTPUT
65-
done
66-
}
67-
68-
full_version=${{ inputs.new_version }}
69-
major_version=$(echo ${full_version} | cut -d. -f1)
70-
minor_version=$(echo ${full_version} | cut -d. -f2)
71-
patch_version=$(echo ${full_version} | cut -d. -f3)
72-
branch_name=${{ inputs.target_branch }}
73-
enable_force_push="${{ inputs.force }}"
74-
pr_title="[Version] Update ${branch_name} to v${full_version}"
75-
pr_body="Bump ${branch_name} to ${full_version}."
76-
pr_branch="pr/ver/${branch_name}-v${full_version}"
77-
78-
log_and_export_vars \
79-
full_version major_version minor_version patch_version \
80-
branch_name pr_title pr_branch pr_body enable_force_push
81-
82-
echo "Branch ref: $GITHUB_REF" | tee -a $GITHUB_STEP_SUMMARY
83-
echo "Branch SHA: $GITHUB_SHA" | tee -a $GITHUB_STEP_SUMMARY
84-
echo "Branch commit: $(git show --oneline --no-patch ${GITHUB_SHA})" | tee -a $GITHUB_STEP_SUMMARY
85-
86-
- name: Verify environment
87-
id: verify-env
88-
run: |
89-
# Target branch must already exist
90-
if ! git ls-remote --exit-code origin ${branch_name}; then
91-
echo " Target branch must already exist" | tee -a $GITHUB_STEP_SUMMARY
92-
exit 1
93-
fi
94-
95-
#Ensure that target branch version is compatible.
96-
if [[ "${branch_name}" =~ ^branch/[0-9]+\.[0-9]+\.x$ ]]; then
97-
branch_version=$(echo ${branch_name} | cut -d/ -f1 --complement)
98-
branch_major=$(echo ${branch_version} | cut -d. -f1)
99-
branch_minor=$(echo ${branch_version} | cut -d. -f2)
100-
if [ "${branch_major}" != "${major_version}" ]; then
101-
echo " Target branch major version mismatch"
102-
exit 1
103-
fi;
104-
if [ "${branch_minor}" != "${minor_version}" ]; then
105-
echo " Target branch minor version mismatch"
106-
exit 1
107-
fi
108-
fi
109-
110-
# PR branch must *not* exist
111-
if [ "${enable_force_push}" == "false" ]; then
112-
if git ls-remote --exit-code origin ${pr_branch}; then
113-
echo " PR branch cannot already exist - Delete branch and retry workflow or enable 'force'" | tee -a $GITHUB_STEP_SUMMARY
114-
exit 1
115-
fi
116-
fi
117-
118-
if [[ ! $full_version =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
119-
echo "Invalid version number: $full_version"
120-
exit 1
121-
fi
122-
123-
- name: Update version numbers in target branch
124-
id: create-pr-branch
125-
run: |
126-
git checkout -b ${pr_branch}
127-
echo "::group::Running update_version.sh"
128-
./ci/update_version.sh ${major_version} ${minor_version} ${patch_version}
129-
echo "::endgroup::"
130-
131-
if ! git diff --quiet; then
132-
echo "::group::Diff"
133-
git diff
134-
echo "::endgroup::"
135-
136-
git add .
137-
138-
git config user.name "github-actions[bot]"
139-
git config user.email "41898282+github-actions[bot]@users.noreply.github.com"
140-
git commit -m "${pr_body}"
141-
142-
# Push the changes to the release branch:
143-
git push --force origin ${pr_branch}
144-
fi
145-
146-
- name: Create pull request for target branch
147-
id: create-pr
148-
run: |
149-
gh pr create \
150-
-B "${branch_name}" \
151-
-b "${pr_body}" \
152-
-t "${pr_title}" \
153-
-H "${pr_branch}"
58+
- name: Update version
59+
uses: ${{ github.repository }}/.github/actions/version-update@main
60+
with:
61+
new_version: ${{ inputs.new_version }}
62+
target_branch: ${{ inputs.target_branch }}
63+
force: ${{ inputs.force }}

0 commit comments

Comments
 (0)