|
1 | | -name: Tag on PR Merge |
| 1 | +name: Semantic Version Tagging |
2 | 2 |
|
3 | 3 | on: |
4 | 4 | push: |
|
7 | 7 | pull_request: |
8 | 8 | branches: |
9 | 9 | - main |
| 10 | + |
10 | 11 | jobs: |
11 | 12 | bump-version: |
12 | 13 | runs-on: ubuntu-latest |
| 14 | + if: github.event_name == 'push' || github.event_name == 'pull_request' |
13 | 15 |
|
14 | 16 | steps: |
15 | 17 | - name: Checkout repo |
16 | 18 | uses: actions/checkout@v3 |
17 | 19 | with: |
18 | | - fetch-depth: 0 # fetch full history to get tags |
| 20 | + fetch-depth: 0 |
19 | 21 |
|
20 | | - - name: Get the branch that triggered the workflow |
21 | | - id: vars |
| 22 | + - name: Get source branch name |
| 23 | + id: get_branch |
22 | 24 | 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 |
25 | 33 |
|
26 | | - - name: Determine version bump type |
27 | | - id: bump |
| 34 | + - name: Decide bump type based on branch prefix |
| 35 | + id: bump_type |
28 | 36 | 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 |
35 | 45 | else |
36 | | - echo "BUMP=patch" >> $GITHUB_ENV |
| 46 | + echo "bump=patch" >> $GITHUB_ENV |
37 | 47 | fi |
| 48 | + echo "Bump type set to $bump" |
38 | 49 |
|
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 |
47 | 51 | 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 |
53 | 58 |
|
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