Search code examples
gitshelldeploymentbitbucket-pipelinessemantic-versioning

How best to do bitbucket pipeline automatic versioning?


These days at work, I was asked if it was possible to do automated pipeline versioning.

After researching about I found some examples using date versioning, like:2021.21.09.153507

My sh that performs the versioning:

git pull --rebase origin dev
npm install -g json

year=$(date +'%Y')
month=$(date +'%m')
day=$(date +'%d')
hours=$(date +'%H')$(date +'%M')$(date +'%S')

newVersion="$year.$month.$day.$hours"
json -I -f package.json -e "this.version=\"$newVersion\""

git add package.json
git commit -m "[skip ci]"
git push origin dev

Everything worked out with this solution, but we always use versioning manual with the semantic like this: 1.0.0

Is it possible to identify which branch is being merged and thus create conditions to increase the version depending on some tag in the commit?


Solution

  • Check out this doc.

    Bitbucket Pipelines provide a set of default variables that are available for builds, and can be used in scripts. Values include BITBUCKET_BRANCH (source branch), BITBUCKET_PR_DESTINATION_BRANCH (pull request destination branch), BITBUCKET_TAG, BITBUCKET_COMMIT and many others.

    Moreover, I can tell from my own experience that the list provided by that link is not exhaustive - there are more potentially interesting variables available for your build which are undocumented for some reason. Running printenv somewhere in your pipeline will reveal them in their entirety.