Bitbucket pipelines (CI) using GitVersion

 Its time to make an article on how to use GitVersion in Bitbucket CI (pipelines). Its not documented, the built in pipelines do not include it and unless you want to use a specific docket image with all the prebuilt bits in it and use up extra time on your pipelines, you might just want to look here first.



What is GitVersion?

GitVersion is a tool that can calculate next build numbers, its part of GitTools and is supported fairly well with GitHub and Azure Deployments as its a DotNet product.


Why GitVersion?

GitVersion is a mainstream tool that supports using git tags to manage version control. It provides semantic versioning based on a set of rules that you can control for your development, release and other git branches.


How do I setup a bitbucket pipeline?

They are really simple to setup. Either, provide a bitbucket-pipelines.yml file in the root of your project or use the built in wizard to get started. The Wizard will also add the same file to the root of your project so you may as well pre-empt that and do it yourself.

You will need:

  • A basic understanding of YML
  • some knowledge of copy and paste
YML must be structured precisely, its as annoying as python if you get it wrong. Key rule, 2 spaces for indentation. If you want to find out more about YML, that's not what this article is for.

Here is a bitbucket-pipelines.yml file example

image: composer:2.0
pipelines:
default:
- step:
name: Composer version
image: gittools/gitversion

script:
- git fetch --unshallow
- wget https://github.com/GitTools/GitVersion/releases/download/5.8.1/gitversion-linux-x64-5.8.1.tar.gz
- tar -xvf gitversion-linux-x64-5.8.1.tar.gz
- chmod 0777 gitversion
- ./gitversion
- ./gitversion /showvariable semver
- export GV_SEMVER=`./gitversion /showvariable semver`
- echo $GV_SEMVER
- git tag $GV_SEMVER ${BITBUCKET_COMMIT}
- git push origin --tags

This is a simple working version of a pipeline. Not every line here is required but you can delete as needed.

What does this do?


First we get all commit data from bitbucket. Without it, GitVersion fails, thats what the unshallow command is for

Next we get the tool. Its a sandbox VM (docker) instance so we either have it loaded in the image or get our own. Adjust the version as required.

Once its untarred and given execute rights (0777) then we can run the application.
The first gitversion command is to confirm it works
The second line is to get the semver variable out

Then we set it to a shell variable and echo it to make sure it was set

Lastly we add the variable to a tag against the commit we deployed and finally push the tags back.

You can adjust the pipeline as necessary to work for certain branches or use this for everything.

Do I need anything else?


YES, don't go yet because this alone won't work. You need a GitVersion.yml file in the root of your project too. Read the instructions on setting it up on GitVersions configuration page on their website. This will determine the versioning schemas to use and their usage.

Comments