Auto-incrementing counter variable support on Gitlab

Azure DevOps offers counter variables functionality:

With them you can let CI track the value of your environment variable, which is auto-incremented on every pipeline run.

Their counters are scoped to a pipeline, not the project.

The use case I have is that I want to use this as an environment variable to determine the build number (which would always be unique, +1 higher than the previous build number). It goes without saying that plain commit hashes do not fit my use case, as I need a numeric representation which is always higher than the previous value.

I’m looking for equivalent functionality in Gitlab to achieve this, without introducing any new commits in the codebase.

What are the options on Gitlab that provide equivalent to the aforementioned functionality (counter does not necessarily need to be scoped to a pipeline in particular)?

If your only requirement is a number that is always higher than the previous number, then you could just use the environment variable CI_PIPELINE_CREATED_AT (you might need to transform it to seconds though). Or directly use date in a script.

1 Like

Predefined CI/CD variables provide more possibilities, i.e. CI_PIPELINE_ID which is unique across the instance, and of the integer value type. Alternatively, use the internal ID.

@tschoesi - good point, however I forgot to highlight an additional requirement which is quite important.

Because ID maximum numeric value is finite, the solution should yield an ID which is not huge in numerical value.
Additionally, it ideally should not grow super fast as well – the timestamp up to second precision will increase the numeric value by 31536000 which is too much in my case.

@dnsmichi looking at the options you mentioned, (although it doesn’t support a seed or initial value) the CI_PIPELINE_IID looks perfect since it’s low in numerical value & does not grow fast.

However there’s one caveat I’ve read up in these threads:

In case of CI_PIPELINE_IID - mapping the Internal ID back to a pipeline should such need arise in the future.
@dnsmichi Is there a solution to that yet?

Alternatively, I may go for CI_PIPELINE_ID as suggested.

I do not think that this exists in GitLab yet. Suggest creating a feature proposal :slight_smile:

That should be possible with an API request to the pipeline endpoint. The only caveat is that you will need to loop over all pipelines and filter by iid manually. Something like this using python-gitlab, untested:


pipelines = project.pipelines.list()

for pipeline in pipelines:
  if pipeline['iid'] eq IID: