Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Branch name in gitlab ci

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.

like image 615
Max R. Avatar asked Sep 04 '18 15:09

Max R.


People also ask

What is branch name in GitLab?

GitLab has announced that the default branch name would permanently be renamed to 'main' from master once it releases Git version 2.31. 0 as part of GitLab's 13.11 release in April. The company also assured Git developers that the new renaming wouldn't affect the existing projects in the repository.

What is $Ci_commit_ref_name?

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.

What is $Ci_pipeline_source?

merge_request_event is defined as follows: For pipelines created when a merge request is created or updated. Required to enable merge request pipelines, merged results pipelines, and merge trains. As per your question.


2 Answers

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

like image 164
King Chung Huang Avatar answered Sep 25 '22 22:09

King Chung Huang


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") 
like image 30
mecampbellsoup Avatar answered Sep 23 '22 22:09

mecampbellsoup