Reputation: 2603
I am building a Jenkins jobs to build docker container on AWS EC2 instance and this is a sample of a Jenkins script that is giving errors:
#!/bin/bash -e
# Not giving the IP here but I guess you can understand
HOST = Some IP address of EC2 instance in AWS
# Current Project workspace
# Download source code and create a tar and then SCP it in to AWS EC2
# So my Code is copied in to AWS EC2 instance now ...
# Now do the SSH and run the script on AWS EC2 instance
ssh -o StrictHostKeyChecking=no -i MySecrets.pem ec2-user@$HOST \
"tar xvf pc.tar && \
cd my_project_source_code && \
docker stop $(docker ps -a -q) && \
docker rmi $(docker images -a -q) && \
sh -c 'nohup docker-compose kill > /dev/null 2>&1 &' && \
docker-compose build --no-cache && \
sh -c 'nohup docker-compose up > /dev/null 2>&1 &' "
When I build this job in Jenkins, it fails with following error on output console :
"docker stop" requires at least 1 argument(s). See 'docker stop --help'.
Usage: docker stop [OPTIONS] CONTAINER [CONTAINER...]
Stop one or more running containers Build step 'Execute shell' marked build as failure
So my question is what is wrong with my bash script here ?
On a separate note :
I am able to run the docker stop $(docker ps -a -q) when I ssh into the EC2 on a CLI. But when same commands run in the Jenkins jobs bash shell script it does not recognize this as valid script. What am I doing wrong here ? This appears to be some misunderstanding from my side on how to run this command in Jenkins Job's bash shell script, but I am not entirely sure.
Upvotes: 1
Views: 1004
Reputation: 295716
If you want substitutions within your script to run on the remote side, it needs to be passed to ssh
in a context where the local shell won't try to evaluate it first. Double quotes aren't that context.
A quoted heredoc will fit the bill:
ssh -o StrictHostKeyChecking=no -i MySecrets.pem "ec2-user@$HOST" 'bash -s' <<'EOF'
tar xvf pc.tar || exit
cd my_project_source_code || exit
docker stop $(docker ps -a -q) || exit
docker rmi $(docker images -a -q) || exit
sh -c 'nohup docker-compose kill > /dev/null 2>&1 &' || exit
docker-compose build --no-cache || exit
sh -c 'nohup docker-compose up > /dev/null 2>&1 &' || exit
EOF
Upvotes: 1