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?
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With