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.
@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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.