5

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?

Marcus
  • 8,230
  • 11
  • 61
  • 88

2 Answers2

2

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.

DanEEStar
  • 6,140
  • 6
  • 37
  • 52
  • "then increment the number in this environment variable directly in the script which runs in Bitbucket Pipelines" - can you elaborate on how this would be done? As I have understood it I do not have write access to the environment variables? – Marcus Nov 19 '16 at 15:03
  • 3
    It's possible to update repository level custom variables using the bitbucket API https://developer.atlassian.com/bitbucket/api/2/reference/resource/repositories/%7Busername%7D/%7Brepo_slug%7D/pipelines_config/variables/%7Bvariable_uuid%7D. I'm working on an approach to use this to update a counter – Brady Emerson Dec 14 '16 at 19:37
1

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

bhanu
  • 76
  • 10
  • I'm using Bitbucket Pipelines with Amazon CodeDeploy. How can I pass `$BITBUCKET_BUILD_NUMBER` to Amazon CodeDeploy so that I can use the variable to display on my website? – Kelvin Low Dec 17 '20 at 17:21