Search code examples
gitlabgitlab-cigitlab-api

GitLab CI get predefined variable with API?


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-"
}

Solution

  • 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.