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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-461.png)
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-462.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-463.png)
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-464.png)
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-465.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-467.png)
vì status khách nhau nên post change được run
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-468.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-469.png)
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-470.png)
giờ click lại luôn và không sửa pipeline
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-471.png)
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
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-472.png)
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-473.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-474.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-475.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-476.png)
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"
}
}
}
![](https://nimtechnology.com/wp-content/uploads/2021/10/image-477.png)