Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increment a number in Bitbucket pipelines

I have a netcoreapp1.0 that I build using Bitbucket pipelines and pack with dotnet pack, and push to Octopus deploy as a package MyAssembly.Api.1.0.0-beta-*.nupkg where * is supposed to be a commit number/build number (or any other stable incremented number).

Since the commit identifiers in GIT are UUIDs, I have tried the following commands (see below) to get the commit count, but the resulting commit count is very unreliable and does not work as expected. Locally I get it working just fine and the commit count is incremented for every commit I make to my local repo. Unfortunately, none of the commands work in the pipeline (running in a Docker container). For some reason the commit count stays the same or even decreases sometimes.

I read somewhere that it has to do with "shallow/unshallow" git repo blabla..., and that it might be solved by logging in (to GIT) every time. I do not wish to do this if I can avoid it, and I find it kinda ironic that I would need to login to GIT within Bitbucket itself.

git shortlog | grep -cE '^[ ]+\w+'
git rev-list HEAD --count
git rev-list --all --count
git rev-list --no-merges --count HEAD
git log --pretty=format:'' | wc -l
git log master --pretty=oneline | wc -l

Q: Is there any other way to increment a value and access it as a variable in a pipeline?

like image 359
Marcus Avatar asked Oct 17 '22 22:10

Marcus


2 Answers

As you already discovered it is not that easy or not even meant to get an incremental number directly from the git repository or history.

Another way to read and set variables in Bitbucket Pipelines is "Environement variables".

What I would do to solve this problem is to set an environment variable with the desired initial value and then increment the number in this environment variable directly in the script which runs in Bitbucket Pipelines.

like image 199
DanEEStar Avatar answered Oct 21 '22 05:10

DanEEStar


We can directly call $BITBUCKET_BUILD_NUMBER to get the build number now, an incrementing build number for each build in your repository that is available as an environment variable.

https://bitbucket.org/site/master/issues/12838/build-number-that-increments-on-every

like image 20
bhanu Avatar answered Oct 21 '22 04:10

bhanu