4

I am using predefined variables like $CI_COMMIT_REF_SLUG in my gitlab ci pipeline and it would be very useful to access those variables via the gitlab api.

I have read through the documentation and went through all gitlab-ci related GET routes (branches/, jobs/, pipelines/) but could only find the original branch names/tags for each job and pipeline.

Is there any to access this variable?

edit: Use-case would be I'd like to query the urls after a successful pipeline. During the pipeline a url like this is generated example.com/$_CI_COMMIT_REF_SLUG/.

I need a response like this coming from the API:

{
    "ref_slug":"foo-12",
    "ref":"-/foo_12-"
}
Theo
  • 2,262
  • 3
  • 23
  • 49
  • Do you have a specific use case? I think all infos set in predefined variables is available with Gitlab API but as json data. – Ekans Jul 04 '18 at 08:23
  • @Ekans edited question to answer use-case question – Theo Jul 04 '18 at 11:01
  • Did you try to solve your problem using the [webhooks](https://gitlab.com/help/user/project/integrations/webhooks)? – Ekans Jul 04 '18 at 11:52
  • @Ekans could you explain? I don't understand how a webhook could help me getting the branch name in SLUG? – Theo Jul 04 '18 at 15:21
  • Perhaps I misunderstood. I deducted from your comment you want to trigger `example.com/$_CI_COMMIT_REF_SLUG` after the pipeline. Am I right? – Ekans Jul 04 '18 at 15:38
  • @Ekans I'd like to list every `example.com/$_CI_COMMIT_REF_SLUG` after the pipeline has run on a website for users to click without having to convert the ref name to ref_slug myself. – Theo Jul 05 '18 at 08:38
  • Ok I understand better your aim. As you said this information is not available in the API. The only workaround I see is to have a final job that collects all `example.com/$_CI_COMMIT_REF_SLUG` contained inside some job artifacts. Then you use this result: send it somewhere or upload as artifact and uses the API to get it etc... – Ekans Jul 05 '18 at 13:09

2 Answers2

4

I don't think that such API exists.

However you can use in parallel your own local variable calculated based on their function: https://gitlab.com/gitlab-org/gitlab-runner/-/blob/main/Makefile.build.mk#L25

BRANCH=$(git branch --show-current)
CI_COMMIT_REF_SLUG=$(echo $BRANCH | cut -c -63 | sed -E 's/[^a-z0-9-]+/-/g' | sed -E 's/^-*([a-z0-9-]+[a-z0-9])-*$$/\1/g')

I don't think that their implementation will change anytime soon(because of backward compatibility implications) to require sync on your side. If you don't want to be exposed to side effects caused by changes in their implementation you can use your calculated value everywhere.

  • Such a GitLab API exists, and can be used like mentionned in this documentation: https://docs.gitlab.com/ee/api/project_level_variables.html#get-a-single-variable – KrazyMax Jul 11 '23 at 02:30
2

I have similiar requirement and googling brought me here. Unfortuately the accepted answer said no. Then I found something useful in gitlab ci document and I think this is what I want.

GET /projects/:id/pipelines/:pipeline_id/variables

[
  {
    "key": "RUN_NIGHTLY_BUILD",
    "variable_type": "env_var",
    "value": "true"
  },
  {
    "key": "foo",
    "value": "bar"
  }
]

Update

/projects/:id/variables gets the predefined variables in ci/ci variable settings, while /projects/:id/pipelines/:pipeline_id/variables gets the variables you manually input when triggering the pipeline.

Lei Yang
  • 3,970
  • 6
  • 38
  • 59
  • Did this actually work for you? I'm running into a similar issue but using that endpoint I just get an empty array back (the call returns successfully...just with no variables). – Dwight Mar 28 '23 at 22:18
  • 1
    i just tried and found it also shows `[]` by default, but **does show** the variables you manually input when triggering the pipeline. I think that means variables that are unique to this specific pipeline_id. – Lei Yang Mar 29 '23 at 01:23
  • Ah, I wonder how that returns for API triggered pipelines - while testing I've only been working with manual runs using the default project variables for the most part. – Dwight Mar 30 '23 at 06:02
  • i think the best is to experiment yourself, the docs could be outdated, and the apis are variant across gitlab versions. i guess, the return values has nothing to do with how they are triggered. you can add variables no matter trigger manually or using api. – Lei Yang Mar 30 '23 at 06:29
  • Yea that's what I'm gonna do just had to switch gears for a bit, I will update here with my findings though. – Dwight Mar 31 '23 at 15:44