Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

Adding release tags automatically based on semVer versioning whenever we merge a branch into master?

Vishal Kashi
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 31, 2023

I want to add git tags to when I merge a PR into my master branch. Is there any better way to do this ? 
Currently I was looking in bitbucket pipeline but I don't know how to run the script when we have a merge commit and also to conditionally create the version name based on the branch name.

Let's say out version is like major.minor.patch then if the branch name is feature/ I will bump the minor version if it's fix/ I will bump the patch version.

 

currently I am trying to do this using this script below but I don't know how to get the branch name I am merging into master $BITBUCKET_BRANCH gives me master and not the feature/ branch

pipelines:

branches:

master:

- step:

name: Version and build

script:

- echo "Made a change in build ${BITBUCKET_BUILD_NUMBER}" >> changes.txt

- git add changes.txt

- git commit -m "Updating changes.txt with latest build number."

- BRANCH_NAME=$(echo ${BITBUCKET_PR_DESTINATION_BRANCH} | sed 's/\//./g')

- echo $BITBUCKET_PR_DESTINATION_BRANCH

- echo $BITBUCKET_BRANCH

- LAST_TAG=$(git describe --tags $(git rev-list --tags --max-count=1))

- echo $LAST_TAG

- MAJOR=$(echo $LAST_TAG | awk -F "." '{print $1}')

- MINOR=$(echo $LAST_TAG | awk -F "." '{print $2}')

- PATCH=$(echo $LAST_TAG | awk -F "." '{print $3}')

- echo $MAJOR

- echo $MINOR

- echo $PATCH

- if [ "$BRANCH_NAME" == "feature.*" ]; then NEW_TAG="$MAJOR.$(($MINOR + 1)).0"

- else NEW_TAG="$MAJOR.$MINOR.$(($PATCH + 1))"

- fi

- echo $NEW_TAG

- git tag -am "Tagging for release ${NEW_TAG}" ${NEW_TAG}

- git push origin ${NEW_TAG}



If there is any other better way to do this automated versioning it will be helpful too.

1 answer

0 votes
Igor Stoyanov
Atlassian Team
Atlassian Team members are employees working across the company in a wide variety of roles.
February 3, 2023

@Vishal Kashi hi. Try this:

git-release.sh file context

#!/usr/bin/env bash

# Release new git version

set -ex

##
# Step 1: Get version tag
##
tag=$(semversioner current-version)

##
# Step 2: Commit back to the repository
##
echo "Committing updated files to the repository..."
git add .
git commit -m "Update files for new version '${tag}' [skip ci]"
git push origin "${BITBUCKET_BRANCH}"

Add this to your yaml file:

script:
- pip install semversioner
- ./git-release.sh

Before run pipeline you should use semversioner for semantic versioning. Check this docs for more details.

Example with semversioner tool:

You should have `.semversioner` folder;

Then add the change:

semversioner add-change --type patch --description "Fix security vulnerability with authentication."

Then run your pipeline with script above.


Regards, Igor

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events