1+ name : Version Bump
2+
3+ on :
4+ push :
5+ branches : [main]
6+
7+ permissions :
8+ contents : write
9+ pull-requests : write
10+
11+ jobs :
12+ version-bump :
13+ runs-on : ubuntu-latest
14+ steps :
15+ - uses : actions/checkout@v4
16+ with :
17+ token : ${{ secrets.GITHUB_TOKEN }}
18+ fetch-depth : 0
19+
20+ - name : Get PR title
21+ id : pr-title
22+ env :
23+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
24+ run : |
25+ pr_number=$(git log -1 --pretty=%s | grep -o '#[0-9]\+' | sed 's/#//')
26+ if [ -n "$pr_number" ]; then
27+ pr_title=$(gh pr view $pr_number --json title --jq '.title')
28+ echo "title=$pr_title" >> $GITHUB_OUTPUT
29+ else
30+ echo "title=$(git log -1 --pretty=%B)" >> $GITHUB_OUTPUT
31+ fi
32+
33+ - name : Check if commit is a version bump
34+ id : skip-check
35+ run : |
36+ if [[ "${{ steps.pr-title.outputs.title }}" == "Version bump to"* ]]; then
37+ echo "skip=true" >> $GITHUB_OUTPUT
38+ echo "Skipping version bump workflow - commit is already a version bump"
39+ else
40+ echo "skip=false" >> $GITHUB_OUTPUT
41+ fi
42+
43+ - name : Determine version bump type
44+ if : steps.skip-check.outputs.skip == 'false'
45+ id : bump-type
46+ run : |
47+ if [[ "${{ steps.pr-title.outputs.title }}" == *"[Major Release]"* ]]; then
48+ echo "type=major" >> $GITHUB_OUTPUT
49+ elif [[ "${{ steps.pr-title.outputs.title }}" == *"[Minor Release]"* ]]; then
50+ echo "type=minor" >> $GITHUB_OUTPUT
51+ else
52+ echo "type=patch" >> $GITHUB_OUTPUT
53+ fi
54+
55+ - name : Calculate new version
56+ if : steps.skip-check.outputs.skip == 'false'
57+ id : version
58+ run : |
59+ if [ ! -f VERSION ]; then
60+ major=0
61+ minor=0
62+ patch=0
63+ else
64+ current=$(cat VERSION | sed 's/v//')
65+ IFS='.' read -r major minor patch <<< "$current"
66+ fi
67+
68+ case "${{ steps.bump-type.outputs.type }}" in
69+ major)
70+ major=$((major + 1))
71+ minor=0
72+ patch=0
73+ ;;
74+ minor)
75+ minor=$((minor + 1))
76+ patch=0
77+ ;;
78+ patch)
79+ patch=$((patch + 1))
80+ ;;
81+ esac
82+
83+ new_version="v${major}.${minor}.${patch}"
84+ echo "new_version=$new_version" >> $GITHUB_OUTPUT
85+
86+ - name : Check for existing version bump PR
87+ if : steps.skip-check.outputs.skip == 'false'
88+ id : check-pr
89+ env :
90+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
91+ run : |
92+ existing_pr=$(gh pr list --state open --search "Version bump to" --json number,headRefName --jq '.[0] // empty')
93+ if [ -n "$existing_pr" ]; then
94+ echo "exists=true" >> $GITHUB_OUTPUT
95+ echo "branch=$(echo "$existing_pr" | jq -r '.headRefName')" >> $GITHUB_OUTPUT
96+ echo "number=$(echo "$existing_pr" | jq -r '.number')" >> $GITHUB_OUTPUT
97+ else
98+ echo "exists=false" >> $GITHUB_OUTPUT
99+ fi
100+
101+ - name : Update existing PR
102+ if : steps.skip-check.outputs.skip == 'false' && steps.check-pr.outputs.exists == 'true'
103+ env :
104+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
105+ run : |
106+ git config --local user.email "[email protected] " 107+ git config --local user.name "GitHub Action"
108+ git fetch origin "${{ steps.check-pr.outputs.branch }}"
109+ git checkout "${{ steps.check-pr.outputs.branch }}"
110+ git merge origin/main
111+ echo "${{ steps.version.outputs.new_version }}" > VERSION
112+ git add VERSION
113+ git commit -m "Bump version to ${{ steps.version.outputs.new_version }}" || echo "No changes to commit"
114+ git push origin "${{ steps.check-pr.outputs.branch }}" --force
115+ gh pr edit "${{ steps.check-pr.outputs.number }}" --title "Version bump to ${{ steps.version.outputs.new_version }}"
116+
117+ - name : Create new PR
118+ if : steps.skip-check.outputs.skip == 'false' && steps.check-pr.outputs.exists == 'false'
119+ env :
120+ GH_TOKEN : ${{ secrets.GITHUB_TOKEN }}
121+ run : |
122+ git config --local user.email "[email protected] " 123+ git config --local user.name "GitHub Action"
124+ branch="version-bump-${{ steps.version.outputs.new_version }}"
125+ git checkout -b "$branch"
126+ echo "${{ steps.version.outputs.new_version }}" > VERSION
127+ git add VERSION
128+ git commit -m "Bump version to ${{ steps.version.outputs.new_version }}"
129+ git push origin "$branch"
130+ gh pr create --title "Version bump to ${{ steps.version.outputs.new_version }}" \
131+ --body "Automated version bump based on commit message.\n\n- Bump type: ${{ steps.bump-type.outputs.type }}\n- New version: ${{ steps.version.outputs.new_version }}"
0 commit comments