67

In my Gitlab CI, I have a stage that triggers another stage by api call trigger and I want to pass the current branch name as parameter to the other project holding the trigger. I used the CI_COMMIT_REF_NAME for this, it seemed to work, but now that I call the stage only when merging the branch to master, the CI_COMMIT_REF_NAME always says "master".

In the documentation it says "The branch or tag name for which project is built", do I understand it correctly that it kind of holds the target branch of my working branch?

I tried to get the current branch in gitlab ci with git symbolic-ref HEAD | sed 's!refs\/heads\/!!' as well but it was empty.

Is CI_COMMIT_REF_NAME the variable I am looking for and something is going wrong or do I need something else?

Thanks in advance.

1

2 Answers 2

100

I'm not sure what you mean by “a stage that triggers another stage by api call trigger”. But, generally speaking, GitLab CI jobs are part of a CI pipeline, and CI pipelines are created for a branch or tag.

The CI_COMMIT_REF_NAME variable contains the name of the branch or tag that the pipeline was created for.

The CI_MERGE_REQUEST_TARGET_BRANCH_NAME is "The target branch name of the merge request."

from GitLab Predefined Variables reference

2
18

Note that a rule like the following that checks the value of $CI_COMMIT_BRANCH (intended to prevent running this step when a PR has been merged into master branch) will not work as expected:

build-python-c4d:
  stage: pre
  rules:
    - if: '$CI_COMMIT_BRANCH != "master"'

From docs for $CI_COMMIT_BRANCH:

The commit branch name. Available in branch pipelines, including pipelines for the default branch. Not available in merge request pipelines or tag pipelines.

Instead you should use:

build-python-c4d:
  stage: pre
  rules:
    - if: ($CI_COMMIT_BRANCH != "master" && $CI_COMMIT_REF_NAME != "master")

Not the answer you're looking for? Browse other questions tagged or ask your own question.