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.
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 ofgit rev-parse --countwill only show the count of commits up to the configured depth.As you make more commits, revs related to your
.gitlab-ci.ymlfile 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 --unshallowinside of your GitLab job before runninggit rev-parseto ensure you're operating on a complete copy.