This is basically what the sh step does. If you do not commit the result to a variable, you can simply run:
sh "./build"
This will end if the script returns a non-zero exit code.
If you need to make the material first, and you need to fix the result, you can use the shell step to exit the task
stage('Building') { def result = sh returnStatus: true, script: './build.sh' if (result != 0) { echo '[FAILURE] Failed to build' currentBuild.result = 'FAILURE' // do more stuff here // this will terminate the job if result is non-zero // You don't even have to set the result to FAILURE by hand sh "exit ${result}" } }
But the following will give you the same thing, but it seems more reasonable to do
stage('Building') { try { sh './build.sh' } finally { echo '[FAILURE] Failed to build' } }
You can also call a return in code. However, if you are inside the stage , it will only return at this stage. So,
stage('Building') { def result = sh returnStatus: true, script: './build.sh' if (result != 0) { echo '[FAILURE] Failed to build' currentBuild.result = 'FAILURE' return } echo "This will not be displayed" } echo "The build will continue executing from here"
cannot complete the task but
stage('Building') { def result = sh returnStatus: true, script: './build.sh' } if (result != 0) { echo '[FAILURE] Failed to build' currentBuild.result = 'FAILURE' return }
will be.
Rik
source share