Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass GitLab variable in multiline script with json input?

I'm searching for a way to pass the $CI_COMMIT_TAG within my .gitlab-ci.yml to a multiline curl command with json data. But every time I do so I get the variable-key-string istead of the value.

production:
  stage: deploy
  script:
    - "openssl aes-256-cbc -k $DEPLOY_KEY -in config/deploy_id_rsa_enc_gitlab -d -a -out config/deploy_id_rsa"
    - chmod 600 config/deploy_id_rsa
    - eval `ssh-agent -s`
    - ssh-add config/deploy_id_rsa
    - ssh-keyscan -H $HOST_PRODUCTION >> ~/.ssh/known_hosts
    - bundle exec cap production deploy tag=$CI_COMMIT_TAG
    - "curl --request POST -u $GRAFANA_USR:$GRAFANA_PWD \
      --url https://stats.domain.mil/grafana/api/annotations/graphite \
      --header 'content-type: application/json' \
      --data '{\"what\": \"Deploy: CORE\",\"tags\": [\"production_release\"],\"data\": \"$CI_COMMIT_TAG\"}'"
  environment:
    name: production
    url: https://$HOST_PRODUCTION
  only:
    - tags
  when: manual

How do I pass the $CI_COMMIT_TAG the correct way?

like image 455
Thomas Schwärzl Avatar asked Dec 13 '17 10:12

Thomas Schwärzl


People also ask

How do I set environment variables in GitLab-CI?

Go to Settings > CI/CD. Click Expand in the Variables section. Select the State and Masked values you want for your variable.

What is Before_script in GitLab-CI?

These are scripts that you choose to be run before the job is executed or after the job is executed. These can also be defined at the top level of the YAML file (where jobs are defined) and they'll apply to all jobs in the . gitlab-ci. yml file.

What is Ci_commit_tag?

CI_COMMIT_TAG - The commit tag name. Present only when building tags. Therefore in the variables section CI_COMMIT_TAG is not defined, hence equals to "". So if you want to use CI_COMMIT_TAG use in job where tags are defined.


1 Answers

Inside single-quotes, the shell expands nothing. Place them inside double-quotes like this:

- "curl --request POST -u $GRAFANA_USR:$GRAFANA_PWD \
  --url https://stats.domain.mil/grafana/api/annotations/graphite \
  --header 'content-type: application/json' \
  --data '{\"what\":\"Deploy: CORE\",\"tags\":[\"production_release\"],\"data\":\"'"$CI_COMMIT_TAG"'\"}'"
like image 75
1resu Avatar answered Sep 28 '22 19:09

1resu