Skip to content

Commit dc4aacb

Browse files
committed
phips28/gh-action-bump-version based workflow
1 parent ff5ff0a commit dc4aacb

1 file changed

Lines changed: 35 additions & 34 deletions

File tree

.github/workflows/tag-version.yml

Lines changed: 35 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Tag on PR Merge
1+
name: Semantic Version Tagging
22

33
on:
44
push:
@@ -7,53 +7,54 @@ on:
77
pull_request:
88
branches:
99
- main
10+
1011
jobs:
1112
bump-version:
1213
runs-on: ubuntu-latest
14+
if: github.event_name == 'push' || github.event_name == 'pull_request'
1315

1416
steps:
1517
- name: Checkout repo
1618
uses: actions/checkout@v3
1719
with:
18-
fetch-depth: 0 # fetch full history to get tags
20+
fetch-depth: 0
1921

20-
- name: Get the branch that triggered the workflow
21-
id: vars
22+
- name: Get source branch name
23+
id: get_branch
2224
run: |
23-
echo "MERGE_BRANCH=$(git log -1 --pretty=%B | grep -oP '(feat|fix|rel)/[^\s]+')" >> $GITHUB_OUTPUT
24-
echo "MERGE_BRANCH=${MERGE_BRANCH:-unknown}" >> $GITHUB_OUTPUT
25+
echo "PR_BRANCH=unknown" >> $GITHUB_OUTPUT
26+
if [ "${{ github.event_name }}" == "pull_request" ]; then
27+
echo "PR_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT
28+
else
29+
# For push events, get the last commit message branch info fallback
30+
BRANCH_NAME=$(git log -1 --pretty=%B | grep -oP '(feat|fix|rel)/[^\s]+')
31+
echo "PR_BRANCH=${BRANCH_NAME:-unknown}" >> $GITHUB_OUTPUT
32+
fi
2533
26-
- name: Determine version bump type
27-
id: bump
34+
- name: Decide bump type based on branch prefix
35+
id: bump_type
2836
run: |
29-
if [[ "${{ steps.vars.outputs.MERGE_BRANCH }}" == feat/* ]]; then
30-
echo "BUMP=minor" >> $GITHUB_ENV
31-
elif [[ "${{ steps.vars.outputs.MERGE_BRANCH }}" == fix/* ]]; then
32-
echo "BUMP=patch" >> $GITHUB_ENV
33-
elif [[ "${{ steps.vars.outputs.MERGE_BRANCH }}" == rel/* ]]; then
34-
echo "BUMP=major" >> $GITHUB_ENV
37+
BRANCH=${{ steps.get_branch.outputs.PR_BRANCH }}
38+
echo "Branch detected: $BRANCH"
39+
if [[ "$BRANCH" == feat/* ]]; then
40+
echo "bump=minor" >> $GITHUB_ENV
41+
elif [[ "$BRANCH" == fix/* ]]; then
42+
echo "bump=patch" >> $GITHUB_ENV
43+
elif [[ "$BRANCH" == rel/* ]]; then
44+
echo "bump=major" >> $GITHUB_ENV
3545
else
36-
echo "BUMP=patch" >> $GITHUB_ENV
46+
echo "bump=patch" >> $GITHUB_ENV
3747
fi
48+
echo "Bump type set to $bump"
3849
39-
- name: Show current tags
40-
run: git tag
41-
42-
- name: Get current version
43-
id: current_version
44-
run: echo "CURRENT_VERSION=$(git describe --tags --abbrev=0)" >> $GITHUB_OUTPUT
45-
46-
- name: Bump version using npm
50+
- name: Bump version and create tag
4751
id: bump_version
48-
run: |
49-
echo "Bumping version with type $BUMP"
50-
npm version $BUMP --no-git-tag-version > new_version.txt
51-
NEW_VERSION=$(cat new_version.txt)
52-
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
52+
uses: phips28/gh-action-bump-version@v9
53+
with:
54+
tag-prefix: "v"
55+
bump: ${{ env.bump }}
56+
create-tag: true
57+
push-tag: true
5358

54-
- name: Create git tag for new version
55-
run: |
56-
git config user.name "github-actions"
57-
git config user.email "github-actions@github.com"
58-
git tag ${{ steps.bump_version.outputs.NEW_VERSION }}
59-
git push origin ${{ steps.bump_version.outputs.NEW_VERSION }}
59+
- name: Show new tag
60+
run: echo "New tag created: ${{ steps.bump_version.outputs.new_tag }}"

0 commit comments

Comments
 (0)