admin管理员组文章数量:1392050
I'm running a GitLab CI/CD pipeline that connects to a remote server via SSH and sends a message to a Telegram bot using curl. The message is stored in a variable and should be passed to the remote server. However, the message appears to be empty when curl executes on the remote machine.
Here’s my current .gitlab-ci.yml snippet:
script:
- start_build_message="The project build has started! I will notify you when it's completed. Author $GITLAB_USER_NAME."
- echo "$start_build_message"
- ssh -o "StrictHostKeyChecking=no" "$SSH_USERNAME"@"$SSH_HOST"
"curl -s -X POST '/$TELEGRAM_BOT_TOKEN/sendMessage' \
-d chat_id=$TELEGRAM_CHAT_ID \
--data-urlencode text=\"\${start_build_message}\""
I'm running a GitLab CI/CD pipeline that connects to a remote server via SSH and sends a message to a Telegram bot using curl. The message is stored in a variable and should be passed to the remote server. However, the message appears to be empty when curl executes on the remote machine.
Here’s my current .gitlab-ci.yml snippet:
script:
- start_build_message="The project build has started! I will notify you when it's completed. Author $GITLAB_USER_NAME."
- echo "$start_build_message"
- ssh -o "StrictHostKeyChecking=no" "$SSH_USERNAME"@"$SSH_HOST"
"curl -s -X POST 'https://api.telegram./$TELEGRAM_BOT_TOKEN/sendMessage' \
-d chat_id=$TELEGRAM_CHAT_ID \
--data-urlencode text=\"\${start_build_message}\""
Share
Improve this question
asked Mar 11 at 15:56
SwAsKkSwAsKk
251 silver badge3 bronze badges
1 Answer
Reset to default 0You should export the start_build_message
variable if you want to do it this way. Variables in bash are not saved between different commands. But this way it is saved to the environment variables and after that all the commands following can read it from there.
script:
- export start_build_message="The project build has started! I will notify you when it's completed. Author $GITLAB_USER_NAME."
- echo "$start_build_message"
- ssh -o "StrictHostKeyChecking=no" "$SSH_USERNAME"@"$SSH_HOST"
"curl -s -X POST 'https://api.telegram./$TELEGRAM_BOT_TOKEN/sendMessage' \
-d chat_id=$TELEGRAM_CHAT_ID \
--data-urlencode text=\"\${start_build_message}\""
You could also use gitlab variables to set the start_build_message
variable instead of exporting it to environment variables in script part of the job.
variables:
start_build_message: "The project build has started! I will notify you
when it's completed. Author $GITLAB_USER_NAME."
script:
- echo "$start_build_message"
- ssh -o "StrictHostKeyChecking=no" "$SSH_USERNAME"@"$SSH_HOST"
"curl -s -X POST 'https://api.telegram./$TELEGRAM_BOT_TOKEN/sendMessage' \
-d chat_id=$TELEGRAM_CHAT_ID \
--data-urlencode text=\"\${start_build_message}\""
版权声明:本文标题:GitLab CI: How to Pass a Variable from GitLab CICD to SSH and Use it in a Remote Curl Command? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744783355a2624848.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论