Running nested commands in Jenkins pipeline shell

邮差的信 提交于 2019-12-01 22:10:09

问题


I want to run nested shell command for example like this in Jenkins pipeline:

docker stop $(docker ps -aq)

Unfortunately when I format it into pipeline syntax:

sh('docker stop $(docker ps -aq)')

Jenkins does not seem to run them correctly, but outputs that:

"docker stop" requires at least 1 argument(s).

I tried to run the command under bash like told here: Run bash command on jenkins pipeline But end up with similar issue. Any ideas how to solve this?


回答1:


This becomes easier for Jenkins Pipeline if you expand the shell command into two lines:

  • The first to capture the Docker containers that you want to stop.
  • The second to stop those Docker containers captured in the first command.

We use the first line to capture the output of the shell command into a variable:

containers = sh(returnStdout: true, script: 'sudo /usr/bin/docker ps -aq')

We then use the second command to operate on the captured output from the first command stored in a variable:

sh("sudo /usr/bin/docker stop $containers")

Note that the docker command is normally comfortable with the output of docker ps -aq for operating on with its other commands, but if it dislikes the output stored in the variable, you can reformat it like the following:

containers = sh(returnStdout: true, script: 'sudo /usr/bin/docker ps -aq').trim()

This would, for example, strip the leading whitespace and trailing newlines. The Docker CLI normally does not care about that, but some reformatting may prove necessary here.

Since removing the newlines here would result in a long combined container ID, we need to (as you noted) replace it with a whitespace to delimit the container IDs. That would make the formatting for the string stored in the containers variable:

containers = sh(returnStdout: true, script: 'sudo /usr/bin/docker ps -aq').replaceAll("\n", " ")




回答2:


I've not used docker stop command this way but syntax is same for docker rm command. Block of pipeline code + OP's line for example:

...
    withEnv(["port=$port", "user=$user", "env=$env"]) {
        sh '''
            ssh -p $port $user@$env docker rm \$(docker ps -aq) || true; \
            ssh -p $port $user@$env docker rmi \$(docker images -aq) || true; \
            ssh -p $port $user@$env docker stop \$(docker ps -aq) || true
        '''
    }
...



回答3:


Have you tried somethink like:

docker stop `docker ps -aq`

?



来源:https://stackoverflow.com/questions/47392267/running-nested-commands-in-jenkins-pipeline-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!