Bài này chúng ta chỉ xoay quanh viết check status của build hiện tại và build trước đó.
Bạn tạo 1 jenkins pipeline nhé
node {
try {
stage('Build') {
script{
currentBuild.result = 'SUCCESS' //SUCCESS, ABORTED, UNSTABLE, OR FAILURE
}
}
}
catch(err)
{
//does nothing here
}
finally {
def currentResult = currentBuild.result
//this section isis always
echo "always() code is executed"
//changed
if(currentBuild.previousBuild != null) {
def previousResult = currentBuild.previousBuild.result
if (previousResult != null && previousResult != currentResult) {
echo 'changed() code is executed'
}
}
//fixed
if(currentBuild.previousBuild != null) {
def previousResult = currentBuild.previousBuild.result
if (previousResult != null && (previousResult == 'FAILURE'
|| previousResult == 'UNSTABLE')
&& currentResult == 'SUCCESS') {
echo 'fixed() code is executed'
}
}
//regression
if(currentBuild.previousBuild != null) {
def previousResult = currentBuild.previousBuild.result
if (previousResult != null && (previousResult == 'FAILURE'
|| previousResult == 'UNSTABLE'
|| previousResult == 'ABORTED')
&& currentResult == 'SUCCESS') {
echo 'regeression() code is executed'
}
}
//aborted
if(currentResult == 'ABORTED') {
echo 'aborted() code is executed'
}
//failure
if (currentResult == 'FAILURE') {
echo 'failure() code is executed'
}
//failure
if(currentResult == 'SUCCESS') {
echo 'success() code is executed'
}
//unstable
if(currentResult == 'UNSTABLE') {
echo 'unstable() code is executed'
}
//cleanup
echo 'cleanup() code is executed'
}
}

Hình ảnh build hiện tại và build trước thành công

Build này thành công và build trước GÃY
