GITLAB CI: File shell can not get environment variables - gitlab

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!

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

How to load environment path during executing command via ssh?

I'm trying to run a script (let's call it test.sh) via ssh as follows
ssh ${user}#${ip} "python3 test.py"
and the test.py is as follows
import os
# Do sth
...
os.system("emulator xxx")
...
The android environemnt paths are exported in ~/.bashrc, but the above cmd failed due to missing ${ANDROID_SDK_ROOT}. I know it's because ssh ${user}#{ip} cmd will setup a non-login and non-interactive shell, but I wonder if there is any solution?
PS:
I have tried #!/bin/bash --login and it failed.
Try this:
ssh -t ${user}#${ip} "bash -i -c 'python3 test.py'"

How SSH Environment Variables works with shell script file?

If I run the command bellow and my install.sh has the following section:
export S3_URL=$PRD_URL
export S3_ACCESS_KEY=$PRD_S3_ACCESS_KEY
export S3_SECRET_KEY=$PRD_S3_SECRET_KEY
cat install.sh | ssh $PRD_USER#$PRD_HOST
The $PRD_S3_ACCESS_KEY is going to be resolved from my host or the environment variables from the remote server?
Assuming you have gettext installed (which contains envsubst), you can do
envsubst < install.sh | ssh $PRD_USER#$PRD_HOST

Undefined variable in shell script by using ssh

I use shell script to run R program as following:
host_list="server#com" Directory="/home/program/" ssh -f "$host_list" 'cd $Directory && nohup Rscript L_1.R> L_1_sh.txt'
But it always says
Directory: Undefined variable.
SSH does not propagate all your environment variables. You're only setting on the environment of the local client ssh program, not on the server side. As a hack, just stick it inside the commands that ssh is running remotely, instead of the pre-command environment setup.
host_list="server#com" ssh -f "$host_list" 'Directory="/home/program/"; cd "$Directory" && nohup ...'
Here's a simpler version of the command that will let you test it without depending on your particular program setup.
ssh localhost Dir="/etc"; echo Dir is "$Dir"; cd "$Dir" && pwd && ps
I'm not sure but maybe you can try those:
In bash single quotes '' does not repace variables Manual
Try to use ${Directory} or change the variable name (maybe is
reserved)

Assign contents of file to variable over remote ssh from a script running in Jenkins

I have opened a remote ssh session from a script and on remote server there is a file containing version information.
I am trying to assign that version to variable and move current version contents to folder name same as version.
The main script is running in jenkins
I am doing something like this
ssh -i /home/user/.ssh/id_rsa -t -t remoteServer<<EOF
cd $WEB_DIR
VERSION=$(cat $WEB_DIR/version.info)
mv -f $WEB_DIR $BACKUP_DIR/$VERSION
exit
EOF
My VERSION variable is always empty. When I run same locally on that server it gives me version value. Something is different over remote ssh session within a script
Actually I found the way to do it in 2 steps.
$WEB_DIR is set as local variable set in main script.
$WEB_DIR="/usr/local/tomcat/webapps/ROOT"
OLD_VERSION=$(ssh -i /home/user/.ssh/id_rsa -tt user#remoteServer "cat $WEB_DIR/version.info")
ssh -i /home/user/.ssh/id_rsa -t -t user#remoteServer<<EOF
cd $WEB_DIR
mv -f $WEB_DIR $BACKUP_DIR/$OLD_VERSION
# I am executing more commands in here
exit
EOF
Use of double quotes "" in first command is must if want to use local variable.

How to cd on remote server via SSH bash script?

I have a bash script I'm using to connect to a remote server via ssh. That works, however I want the script to immediately pass the command of cd /some/dir after connecting. That doesn't seem to be working. Here's my code:
#!/bin/bash
echo "SSHing.."
ssh -i ~/.ssh/some-site.pem xxx#yyy.com
cd /some/dir
read
How can I have the cd command be executed right after SSH connection is established?
There are two easy ways to execute commands via SSH from inside the script:
1) ssh user#host 'command'
2)
ssh user#host <<<EOF
command1
command2
<...>
commandn
EOF
Normally you'd just edit your ~/.profile on the remote machine.
If that is not an option, you could do something like this:
ssh -t theserver.com 'cd /some/dir && bash -i'
You can use the following command
ssh user#watevr <the_cmd_to_be_executed>
You can try this :
ssh abc#hostname :/pathto/specific directory

Resources