0

In my .gitlab-ci.yml, I would like to give an incremental ID to every pipeline executed, but only relative to the merge request (the pipeline-ID is too large for my purposes), so the first pipeline will have a 0 (or 1, depending how I can get this number), the second a 1 (or 2), etc.

I've thought about using the number of root pipelines executed at that moment (I'm not sure how to name them, I mean the pipelines launched on a push, tag, etc., but not the downstream ones). I refer to this value:

enter image description here

I have found a solution using the API (see the answer below), but I would like to know if there is any better way to do it (I'm new in GitLab CI and probably I've just killed a fly with a sledgehammer).

cbuchart
  • 10,847
  • 9
  • 53
  • 93

1 Answers1

0

So far I've been able to retrieve the value from the list of pipelines in the MR with this API:

GET /projects/:id/merge_requests/:merge_request_iid/pipeline

So, just query the header of that request and parse the total number of items:

curl --header "JOB-TOKEN: ${CI_JOB_TOKEN}" \
  "${PROJECT_URL}/merge_requests/${CI_MERGE_REQUEST_IID}/pipelines" \
  --silent --show-error -I | awk '/^x-total:/{print $2}'

But I find this solution a bit over-engineered. If you know about any better approach (like a CI_ variable), I'll be delighted to hear about it.

cbuchart
  • 10,847
  • 9
  • 53
  • 93