GITLAB CI: File shell can not get environment variables

Viewed 335

I have file shell scripe deploy.sh:

#!/bin/sh

echo "CI_COMMIT_SHORT_SHA=$CI_COMMIT_SHORT_SHA" >> .env
exit

I create a file gitlab-ci.yml with script:

...
script:
  - ...
  - ssh -T -i "xxx.pem" -o "StrictHostKeyChecking=no" $EC2_ADDRESS 'bash -s' < deploy.sh

I connect to EC2 and check file .env result:

CI_COMMIT_SHORT_SHA=

deploy.sh file can not get value of variable CI_COMMIT_SHORT_SHA.

I want to result:

CI_COMMIT_SHORT_SHA=xxxx

How can I do that? Please help me!

1 Answers

The script appears to be executed on another server using ssh. Because of that the environment variables are not present.

You may pass the env variables directly using the ssh command:

ssh <machine> CI_COMMIT_SHORT_SHA=$CI_COMMIT_SHORT_SHA <command>
Related