How to set current build result in Pipeline
1、You can use a try catch block for this. 强制打印失败结果
node {
try {
// do something that fails or do something that doesn't fail
sh "exit 1" //forces a failure then prints result or
currentBuild.result = 'SUCCESS'
} catch (Exception err) {
currentBuild.result = 'FAILURE'
}
echo "RESULT: ${currentBuild.result}"
}
node {
sh './set-up.sh'
try {
sh 'might fail'
echo 'Succeeded!'
} catch (err) {
echo "Failed: ${err}"
} finally {
sh './tear-down.sh'
}
echo 'Printed whether above succeeded or failed.'
}
// …and the pipeline as a whole succeeds
2、catchError
node {
catchError {
sh 'might fail'
}
step([$class: 'Mailer', recipients: 'admin@somewhere'])
}
替代方案1 :
node {
try {
sh 'might fail'
} catch (err) {
echo "Caught: ${err}"
currentBuild.result = 'FAILURE'
}
step([$class: 'Mailer', recipients: 'admin@somewhere'])
}
替代方案2 : 如上try catch clock