Skip to content

Merge pull request #5 from arielszmerla/feat/user-input #9

Merge pull request #5 from arielszmerla/feat/user-input

Merge pull request #5 from arielszmerla/feat/user-input #9

Workflow file for this run

name: Semantic Version Tagging
on:
push:
branches:
- main
permissions:
contents: write # 👈 THIS IS REQUIRED TO PUSH TAGS
jobs:
bump-version:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Get source branch name
id: get_branch
run: |
echo "PR_BRANCH=unknown" >> $GITHUB_OUTPUT
if [ "${{ github.event_name }}" == "pull_request" ]; then
echo "PR_BRANCH=${{ github.event.pull_request.head.ref }}" >> $GITHUB_OUTPUT
else
BRANCH_NAME=$(git log -1 --pretty=%B | grep -oP '(feat|fix|rel)/[^\s]+')
echo "PR_BRANCH=${BRANCH_NAME:-unknown}" >> $GITHUB_OUTPUT
fi
- name: Decide bump type
id: bump_type
run: |
BRANCH=${{ steps.get_branch.outputs.PR_BRANCH }}
if [[ "$BRANCH" == feat/* ]]; then
echo "BUMP=minor" >> $GITHUB_ENV
elif [[ "$BRANCH" == fix/* ]]; then
echo "BUMP=patch" >> $GITHUB_ENV
elif [[ "$BRANCH" == rel/* ]]; then
echo "BUMP=major" >> $GITHUB_ENV
else
echo "BUMP=patch" >> $GITHUB_ENV
fi
- name: Get latest version
id: current_version
run: |
TAG=$(git tag --sort=-v:refname | head -n1)
echo "CURRENT_VERSION=${TAG:-v0.0.0}" >> $GITHUB_OUTPUT
- name: Bump version
id: bump
run: |
VERSION=${{ steps.current_version.outputs.CURRENT_VERSION }}
VERSION=${VERSION#v} # Strip leading v
# Split version manually
MAJOR=$(echo "$VERSION" | cut -d. -f1)
MINOR=$(echo "$VERSION" | cut -d. -f2)
PATCH=$(echo "$VERSION" | cut -d. -f3)
echo "Current: $MAJOR.$MINOR.$PATCH, bump type: $BUMP"
# Increment according to bump type
if [[ "$BUMP" == "major" ]]; then
MAJOR=$((MAJOR + 1))
MINOR=0
PATCH=0
elif [[ "$BUMP" == "minor" ]]; then
MINOR=$((MINOR + 1))
PATCH=0
else
PATCH=$((PATCH + 1))
fi
NEW_VERSION="v$MAJOR.$MINOR.$PATCH"
echo "NEW_VERSION=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "Bumping version to $NEW_VERSION"
- name: Create and push tag
run: |
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git tag -a ${{ steps.bump.outputs.NEW_VERSION }} -m "Release ${{ steps.get_branch.outputs.PR_BRANCH }}"
git push origin ${{ steps.bump.outputs.NEW_VERSION }}