Multiple remote SSH commands

Esser Emmerik July 6, 2017

Hi there,

I am trying to figure out how to write down multiple SSH commands on the remote server. For now I created environment variables to make it less painfull to do a ssh login per command, but I prefer to open one SSH connection and then execute all the commands (more efficient).

My only working solution now is to create separate lines with SSH login and commands:

 - ssh $DEPLOY_USER@$DEPLOY_SERVER tar -xzf $DEPLOY_PATH/deployarchive.tar.gz --directory="$DEPLOY_PATH/folder" 
- ssh $DEPLOY_USER@$DEPLOY_SERVER wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh
etc etc
 
I tried some varations (see below), but that leads to build failures as the SSH command is not working (interpreted as one string or command not remotely executed but on docker image)
 
Variation 1
- ssh $DEPLOY_USER@$DEPLOY_SERVER 'tar -xzf $DEPLOY_PATH/deployarchive.tar.gz --directory="$DEPLOY_PATH/folder"; wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh;'
Variation 2
 - ssh $DEPLOY_USER@$DEPLOY_SERVER << EOF
              tar -xzf $DEPLOY_PATH/deploy.tar.gz --directory="$DEPLOY_PATH/folder";
              wget --cache=off --output-document $DEPLOY_PATH/deploy.sh https://a-file-on-the-internet.sh;
            EOF;
As I use ENV variables for most commands, I was not able to find a way to include those variables in the remote bash script, otherwise I had to execute only one bashscript to do all the tasks. And besides, when you put multiple commands in 1 bash script, you don't have the advantage of the step by step logging in bitbucket pipelines, as it is interpreted as 1 command.
 
Hope anyone has a solution!
 
Peter
 

4 answers

1 vote
nelo_ahartmann August 11, 2019

Hi,

found a solution that worked for me here by julien amblard:

https://community.atlassian.com/t5/Bitbucket-questions/how-bitbucket-listen-a-bash-script-execution-end/qaq-p/737327

Helped me to come up with this solution for the deployment of craftcms site:

image: php:7.2-cli
pipelines:
branches:
dev:
- step:
script:
- apt-get update
- apt-get install -y ssh
- ssh -tt $DEV_USER_AND_SERVER << EOF
- cd $DEV_DIR;
- git config core.sparseCheckout true
- echo "*" > .git/info/sparse-checkout;
- echo "!storage" >> .git/info/sparse-checkout;
- echo "!public/build" >> .git/info/sparse-checkout;
- git add .;
- git reset --hard origin/dev;
- git pull;
- rm -f .env;
- mv .env.vs1010 .env;
- rm web/.htaccess;
- mv web/.htaccessVs1010 web/.htaccess;
- COMPOSER=composer.json php72 composer.phar install --no-interaction --prefer-dist --optimize-autoloader;
- COMPOSER=composer.json php72 composer.phar dump-autoload --classmap-authoritative;
- ./craft cache/flush-all;
- ./craft migrate/all;
- ./craft project-config/sync;
- exit $?
- EOF
0 votes
Uros Cvijan June 19, 2019

Still no answer for this one? I would also like to use multiple commands in one SSH connection and not use deploy script, as inside of it I have some variables that i would like to use from bitbucket.

If i use deploy script it does not pick up variables from bitbucket.

0 votes
Federico Garcia November 14, 2018

FYI this worked for me:

Example:

ssh $DEPLOY_USER@$DEPLOY_SERVER "command1; command2; if [ $? -ne 0 ]; then echo 'Failed'; exit 1; fi;"

Using "" outside and '' inside.

 

Regards.

0 votes
Federico Garcia November 13, 2018

Didn't you have any reply on this topic? I am trying to do the same, but I need all the commands to run in the same ssh connection :(

Esser Emmerik November 13, 2018

No, not yet. We just did a line by line execution. To run all commands in the same SSH connection we ended up by executing a remote .sh script (download and run, see my example with wget).

Federico Garcia November 13, 2018

Oh, I see now, I think I will do something like that so, thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events