Post
https://www.jenkins.io/doc/book/pipeline/syntax/#post
post ở đây là khi bạn completed 1 Pipeline hay 1 stage thì chúng ta chạy “post” để kiểm tra cuối
Nghe vẫn khó hiểu thì bay ngay vào ví dụ.
success, failure, always
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a "success" status
success{
echo "post->success is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
failure{
echo "post->failure is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
always{
echo "post->always is called"
}
}
}


Giờ ta sửa cho stages bị error
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
error("Build error")
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a "success" status
success{
echo "post->success is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
failure{
echo "post->failure is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
always{
echo "post->always is called"
}
}
}



run post always
run post failure
change
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
// error("Build error")
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a different completion status from its previous run
change{
echo "post->change is called"
}
}
}

vì status khách nhau nên post change được run

pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
//error("Build error")
script{
currentBuild.result = "UNSTABLE"
}
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a different completion status from its previous run
changed{
echo "post->change is called"
}
}
}


giờ click lại luôn và không sửa pipeline

unstable
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
//error("Build error")
script{
currentBuild.result = "UNSTABLE"
}
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a different completion status from its previous run
changed{
echo "post->change is called"
}
// Only runs if the current Pipeline's or stage's run has an "unstable", usually caused by test failure, code violations, etc
unstable{
echo "post->unstable is called"
}
}
}
thêm post unstable thì ta sẽ có kết quả như sau


fixed
nó giống như alert OK ở hiện tại sau khi các build trước đã bị failure hay unstable
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
//error("Build error")
// script{
// currentBuild.result = "UNSTABLE"
// }
}
}
}
post{
// Only runs if the current Pipeline's or stage's run is successful and the previous run failure and was unstable
fixed{
echo "post->fixed is called"
}
}
}

regression
chỉ run nếu build hiện tại failure, unstable, aborted và build trước đó thành công
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
error("Build error")
// script{
// currentBuild.result = "UNSTABLE"
// }
}
}
}
post{
// Only runs if the current Pipeline's or stage's run is failure, unstable, aborted and the previous run was successful
regression{
echo "post->regession is called"
}
}
}

abored
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
//error("Build error")
script{
currentBuild.result = "ABORTED"
}
}
}
}
post{
// Only runs if the current Pipeline's or stage's run is failure, unstable, aborted and the previous run was successful
aborted{
echo "post->aborted is called"
}
}
}

cleanup
nó sẽ run sau tất cả các post và ko quan tâm đến status stages hay pipeline
pipeline{
agent{
label "k8s-permanent"
}
stages{
stage("Build"){
steps{
echo "Building"
error("Build error")
}
}
}
post{
// Only runs if the current Pipeline's or stage's run has a "success" status
success{
echo "post->success is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
failure{
echo "post->failure is called"
}
// Only runs if the current Pipeline's or stage's run has a "failed" status
always{
echo "post->always is called"
}
//Run after every other post condition has been evaluated, regardless of the status of Pipeline or Stage
cleanup{
echo "post->cleanup is called"
}
}
}
