Getting incremental commit number

45 views Asked by At

I am using

"git rev-list --count HEAD" 

to get the current commit number in the GitLab-CI/CD .yml file.

But to my surprise, every time I commit some code, the above number keeps on decreasing instead of increasing sequentially.

Please let me know, if I am missing some steps or tags, which will make the incremental HEAD.

1

There are 1 answers

0
sytech On

This is most likely caused by your git clone depth configuration in GitLab CI. By default, GitLab uses a shallow clone (by default a depth of 50) when fetching your repository for CI jobs. That means that the result of git rev-parse --count will only show the count of commits up to the configured depth.

As you make more commits, revs related to your .gitlab-ci.yml file fall out of the depth 'window' and therefore, the count for a specific file may decrease in your pipelines with each successive commit.

You can fix this by configuring your GitLab project settings to ensure a shallow clone is not used (or that the depth is otherwise sufficient for your use case). Alternatively, you can run a command like git fetch --unshallow inside of your GitLab job before running git rev-parse to ensure you're operating on a complete copy.