简体   繁体   中英

Using a variable in GitLab CI inside a POST request

I can't understand what YML does to my script and why everything reads as text. Can you please explain why everything is read as text and how to insert variables here?

  script:
    - >
      curl --silent --show-error
      --request POST 
      --header 'Content-Type: application/json'
      --url "https:webhook"
      --data '{"@type": "MessageCard","@context": "http://schema.org/extensions","themeColor": "0076D7","summary": "New changes ","sections": [{"activityTitle": "New changes","activitySubtitle": "Some Build","activityImage": "png","facts": [{"name": "Assigned to","value": "$GITLAB_USER_LOGIN"}, {"name": "Due date","value": "$CI_JOB_STARTED_AT"}, {"name": "Status","value": "$CI_JOB_STATUS"}],}]}'

The result is something like

错误

Try with the triple funny quotes around the variables. Example: "'"$CI_JOB_STARTED_AT"'" . A more cleaner way would be to move the curl command to a shell script.

It's because of difference between single and double quotes in bash - when you use single quotes the variables aren't going to be expanded.

So, here is a workaround for the instance:

script:
    - >
      curl --silent --show-error
      --request POST 
      --header 'Content-Type: application/json'
      --url "https:webhook"
      --data "'{"@type": "MessageCard","@context": "http://schema.org/extensions","themeColor": "0076D7","summary": "New changes ","sections": [{"activityTitle": "New changes","activitySubtitle": "Some Build","activityImage": "png","facts": [{"name": "Assigned to","value": "$GITLAB_USER_LOGIN"}, {"name": "Due date","value": "$CI_JOB_STARTED_AT"}, {"name": "Status","value": "$CI_JOB_STATUS"}],}]}'"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM