Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Auto - Increment patch version on master while merging branches to master

Given: Bitbucket repo, and 2 developers, Steve and Bob who are working on 2 branches, feature/alpha & feature/beta. On master branch and feature branches we have a properties file with version - let say 8.3.0.

How to... ensure that after Bob or Steve merges their pull request to the master the patch version on the master is incremented? I.e.:

  • Steve finishes his feature/alpha first, his pull request is approved and while he merges it to master the version on master becomes 8.3.1. After Bob does the same - 8.3.2.

Looking for the less painful solution. Thx in advance!

UPDATE 2020-12-07:

mnestorov's answer with some modifications led to the desired result. The difference was that I had no rights to add pre-merge hooks on the server. In case someone is strugling with the same issue - here is a hackish recepie:

  • create a pre-push hook and put incrementation logic there.
  • use one of existing maven plugins for git hooks and make the created hook a part of your repo.

In this way you will ensure your incrementation script will run any time your teammates push without begging devops for help :)

like image 810
Nick Avatar asked Feb 15 '26 20:02

Nick


1 Answers

One way is to create a pre-merge-commit hook and place it on your bitbucket server.

The hook can test a bunch of things, like check into what branch you are merging in, but it can also create or append files that specify the version. Basically, it can be as simple as:

Let's say that your version.txt looks like this:

version = 8.3.0

Then you can have something like:

#!/bin/sh
#
# An example hook script to verify what is about to be committed.
# Called by "git merge" with no arguments.  The hook should
# exit with non-zero status after issuing an appropriate message to
# stderr if it wants to stop the merge commit.
#
# To enable this hook, rename this file to "pre-merge-commit".

echo "Bumping Service version"

OLD_VERSION=$(cat version.txt | grep -o "=.*" | tr -d '= ')
NEW_NUM=$(( $(echo $OLD_VERSION | grep -o "\.[0-9]*$" | tr -d ".") + 1 ))
LAST_NUM=$(echo $OLD_VERSION | grep -o "\.[0-9]*$" | tr -d ".")
NEW_VERSION=$(echo $OLD_VERSION | tr $LAST_NUM $NEW_NUM)

# More conversions to bump the version if needed
# more code code code
# ...

sed -i 's/\(^version = \).*/\1'"$NEW_VERSION"'/' ~/service/version/file/txt

Of course, I'm just guessing what it might be and how it can possible be automated to bump the sub-sub version that you have. But it can definitely be automated and calculated how to bump a number up, whatever it is.

Note: that this hook does not take in any arguments, so you can't pass a version number to it.

like image 200
mnestorov Avatar answered Feb 18 '26 16:02

mnestorov