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
Add a comment  | 

1 Answer 1

Reset to default 0

You 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 CommandStack Overflow