Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increment version numbers in GitHub Protected Branches?

I am looking for a good process to manage the version number of my project when my master branch has GitHub branch protection turned on.

In an ideal world, when you merge to your 'release' branch your continuous integration server would run its tests then automatically increment the version number of your project and commit back to your SCM system.

However, with branch protection, you cannot commit to your branch without a Pull Request, so you have a catch-22 where your CI server cannot push to your protected branch when it tries to update the version number.

I can think of a few work arounds, all of which are sub optimal:

  1. Rig up a system so your CI server would make a PR to update the version. I'm not sure if you can even do that with GitHub, but even if it is that ends up creating two Pull Requests for every one 'real' PR, which is awkward.
  2. Remove Branch Protection - now anyone can push anything to the branches, and you have to manage your developers through a manual process
  3. Update the version manually before the pull request. this is slightly better than #2, but it opens the door for developers to make mistakes when choosing the new version number, or merging the right version number to the wrong branch.

I'm hoping there are other options I haven't thought of.

We are using Javascript and npm, but I believe the problem is language agnostic, certainly the same problem would exist with Java and Maven for example.

like image 798
JBCP Avatar asked Sep 21 '16 16:09

JBCP


People also ask

Can you rebase a protected branch?

Interactive rebase and reword are disabled for pushed commits of protected branches, however, it is possible to do a regular rebase.

What are protected branches in GitHub?

You can protect important branches by setting branch protection rules, which define whether collaborators can delete or force push to the branch and set requirements for any pushes to the branch, such as passing status checks or a linear commit history.


1 Answers

I used git tags to automate versioning using Jenkins. So every time the job ran it took the last tag and incremented it. Since tags are different than commits, adding a tag doesn't conflict with Branch Protection.

# get last tag
last=$(git describe --abbrev=0 --tags)
# increment and tag again
git tag $(($last + 1))
git push origin $(($last + 1))

This script is for a simple integer version number but you could follow semver if you like. This was for a Android project so I added a gradle function that would take out the last tag and use if for version number while building.

/*
 * Gets the version name from the latest Git tag
 */
def getVersionName = { ->
    def stdout = new ByteArrayOutputStream()
    exec {
        commandLine 'git', 'describe', '--tags'
        standardOutput = stdout
    }
    return stdout.toString().trim()
}

I am sure you could use a similar setup with Javascript.

like image 118
Shikher Verma Avatar answered Sep 21 '22 05:09

Shikher Verma