Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get branch name when running pipeline on tag?

Tags:

gitlab

I run pipeline from a tag (let's say v1.0.0) on a branch (let's say staging). My output file is created with ${CI_PROJECT_NAME}-${CI_COMMIT_REF_NAME}.apk. The result is a file named MyProject-v1.0.0.apk.

I wish add branch name in the output filename to get MyProject-staging-v1.0.0.apk.

From the gitlab documentation, i could use CI_COMMIT_TAG and CI_COMMIT_BRANCH like this ${CI_PROJECT_NAME}-${CI_COMMIT_BRANCH}-${CI_COMMIT_TAG}.apk. But the documentation says:

CI_COMMIT_BRANCH : The commit branch name. Present only when building branches.

CI_COMMIT_TAG : The commit tag name. Present only when building tags.

So how to get the branch name?

like image 825
Troopers Avatar asked May 20 '20 12:05

Troopers


People also ask

Can tag and branch have same name?

You should never name a tag and a branch the same name! It makes sense in a case like this to use naming conventions on the tags to keep from colliding on the branch names. Versus when we releasing code from the master branch. You can find the common parent in git using merge-base to do a Tag on code from the past.

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.


1 Answers

You can find which branch a tag is part of.

The issue is: a tag can be referenced (part of the history of) multiple branch.

So, as in here, your gitlab.yml could call a script setting that branch (settings an environment variable), provided you have a convention in place to select the branch you want out of the (possibly) more than one branch which could refer said tag.

like image 166
VonC Avatar answered Sep 19 '22 09:09

VonC