Skip to content

Commit 307f6ef

Browse files
authored
Merge pull request #11 from git-mastery/woojiahao/support-auto-tag-bumping
Add Action to auto-bump versions using tags
2 parents 85b40a7 + 6519fa7 commit 307f6ef

File tree

1 file changed

+64
-0
lines changed

1 file changed

+64
-0
lines changed

.github/workflows/bump-version.yml

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
name: Bump version tag on merge
2+
3+
on:
4+
pull_request:
5+
types: [closed]
6+
branches: [main]
7+
8+
jobs:
9+
tag:
10+
if: github.event.pull_request.merged == true
11+
runs-on: ubuntu-latest
12+
13+
permissions:
14+
contents: write
15+
16+
steps:
17+
- uses: actions/checkout@v4
18+
with:
19+
fetch-depth: 0
20+
21+
- name: Get latest tag
22+
id: latest
23+
run: |
24+
git fetch --tags
25+
tag=$(git tag --sort=-v:refname | head -n 1)
26+
echo "tag=${tag:-v0.0.0}" >> $GITHUB_OUTPUT
27+
28+
- name: Determine bump
29+
id: bump
30+
run: |
31+
labels='${{ toJson(github.event.pull_request.labels.*.name) }}'
32+
if echo "$labels" | grep -q "bump:major"; then
33+
echo "type=major" >> $GITHUB_OUTPUT
34+
elif echo "$labels" | grep -q "bump:minor"; then
35+
echo "type=minor" >> $GITHUB_OUTPUT
36+
elif echo "$labels" | grep -q "bump:patch"; then
37+
echo "type=patch" >> $GITHUB_OUTPUT
38+
else
39+
echo "No bump label found" >&2
40+
exit 1
41+
fi
42+
43+
- name: Calculate next version
44+
id: version
45+
run: |
46+
v=${{ steps.latest.outputs.tag }}
47+
v=${v#v}
48+
IFS=. read major minor patch <<< "$v"
49+
50+
case "${{ steps.bump.outputs.type }}" in
51+
major) major=$((major+1)); minor=0; patch=0 ;;
52+
minor) minor=$((minor+1)); patch=0 ;;
53+
patch) patch=$((patch+1)) ;;
54+
esac
55+
56+
echo "next=v$major.$minor.$patch" >> $GITHUB_OUTPUT
57+
58+
- name: Create and push tag
59+
run: |
60+
git config --global user.name "GitHub Actions"
61+
git config --global user.email "actions@github.com"
62+
63+
git tag ${{ steps.version.outputs.next }}
64+
git push origin ${{ steps.version.outputs.next }}

0 commit comments

Comments
 (0)