linux - How to run docker rmi $(docker images -a -q) in Jenkins as part of ssh script -


i building jenkins jobs build docker container on aws ec2 instance , sample of jenkins script giving errors:

#!/bin/bash -e # not giving ip here guess can understand host = ip address of ec2 instance in aws   # current project workspace  # download source code , create tar , scp in aws ec2 # code copied in aws ec2 instance ... # ssh , run 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 > /dev/null 2>&1 &' " 

when build job in jenkins, fails following error on output console :

"docker stop" requires @ least 1 argument(s). see 'docker stop --help'.

usage: docker stop [options] container [container...]

stop 1 or more running containers build step 'execute shell' marked build failure

so question wrong bash script here ?

on separate note :

i able run docker stop $(docker ps -a -q) when ssh ec2 on cli. when same commands run in jenkins jobs bash shell script not recognize valid script. doing wrong here ? appears misunderstanding side on how run command in jenkins job's bash shell script, not entirely sure.

if want substitutions within script run on remote side, needs passed ssh in context local shell won't try evaluate first. double quotes aren't context.

a quoted heredoc fit 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 > /dev/null 2>&1 &'    || exit eof 

Comments

Popular posts from this blog

angular - Ionic slides - dynamically add slides before and after -

minify - Minimizing css files -

Add a dynamic header in angular 2 http provider -