How do I fail a Jenkins build if a Docker Pipeline Plugin withRun command returns a non-zero exit code? How do I fail a Jenkins build if a Docker Pipeline Plugin withRun command returns a non-zero exit code? jenkins jenkins

How do I fail a Jenkins build if a Docker Pipeline Plugin withRun command returns a non-zero exit code?


One of possible solutions:

docker.withRegistry("https://${REGISTRY}", 'creds-id') {    stage("RUN CONTAINER"){        Image = docker.image("${IMAGE}-${PROJECT}:${TAG}")        try {            c = Image.run("-v /mnt:/mnt")            sh "docker logs -f ${c.id}"            def out = sh script: "docker inspect ${c.id} --format='{{.State.ExitCode}}'", returnStdout: true            sh "exit ${out}"        } finally {            c.stop()        }    }}


I couldn't find any more information on exit codes from the withRun() command, so I ended up just executing a docker run command from an sh step:

node() {  sh 'touch ./test.sh'  sh 'echo "exit 1" >> ./test.sh'  sh 'chmod 755 ./test.sh'  sh "docker run --rm -v ${WORKSPACE}:/newDir alpine /bin/sh /newDir/test.sh"}


How about running a script that exits based upon the output from docker wait?

sh "exit \$(docker wait ${container.id})"

wait prints the container's exit code, which in case of error causes the build to fail according to sh docs:

Normally, a script which exits with a nonzero status code will cause the step to fail with an exception.