Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify gitlab ci yaml branch name variable to replace / with -

Tags:

gitlab-ci

I am following the good practices of naming my branches as dev/myname/featurename How ever this does not play well with the docker image tag name as the forward slashes are not allowed. I am trying to modify the branch name gitlab CI CD variable CI_COMMIT_REF_NAME by replacing / with _ But the below gitlab ci yaml does not do the trick. Any ideas what can be done here ?

build_branch_image:
  stage: build
  script:
    - export
    - export BR=$($CI_COMMIT_REF_NAME | tr / _)
    - docker build -t mygitlab.com:projectname/containername:$BR -f docker/Dockerfile .
  except:
    - master
like image 919
Kishore Vanapalli Avatar asked Jun 26 '26 04:06

Kishore Vanapalli


2 Answers

You can use the environment variable $CI_COMMIT_REF_SLUG which replaces the slashes with minus signs.

$CI_COMMIT_REF_NAME=features/my-branch

becomes

$CI_COMMIT_REF_SLUG=features-my-branch
like image 129
Guillaume F. Avatar answered Jun 30 '26 09:06

Guillaume F.


You're using the $CI_COMMIT_REF_NAME directly as a command, which isn't what you meant. You're missing an echo there:

export BR=$(echo $CI_COMMIT_REF_NAME | tr / _)
like image 43
Mureinik Avatar answered Jun 30 '26 07:06

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!