Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace a variable in yml file with a gitlab ci pipeline variable using sed

I want to replace a variable in my .yml file with a variable from the gitlab ci pipeline.

gitlab-ci.yml

deploy_test:
  stage: deploy
  script:
    - sed -i 's/$TAG/$CI_COMMIT_REF_NAME/g' deploy/test.yml
    - kubectl apply -f deploy/test.yml
  when: manual
  only:
    - master
    - tags

This says within the deploy/test.yml file it should replace $TAG with the value of $CI_COMMIT_REF_NAME?

deploy/test.yml

image: git.<removed>.com:5005/eng/my-group/my-image:$TAG
like image 785
Kay Avatar asked Feb 19 '19 15:02

Kay


1 Answers

Use double quotes(") instead of single quotes (') in sed and also take note that forward slashes (/) need to be escaped too.

so given the following (assuming you are using docker hub)

$CI_COMMIT_REF_NAME=docker_user/repo:v1

You will firstly need to escape the '/' character first, like so

$CI_COMMIT_REF_NAME=docker_user\/repo:v1

Then finally use double quotes in the sed command

sed -i "s/$TAG/$CI_COMMIT_REF_NAME/g" deploy/test.yml
like image 190
allkenang Avatar answered Sep 21 '22 07:09

allkenang