Jenkins管道:中止阶段中的输入无法触发该阶段中止的post操作
我的目的很简单,只想在用户单击该阶段输入步骤中的“中止”按钮时在该阶段中执行一些post操作。我阅读了jenkins.io中的一些文档,发现使用post指令似乎有一种隐式的方法。因此,我在下面做了一些简单的测试: 首先是:Jenkins管道:中止阶段中的输入无法触发该阶段中止的post操作,jenkins,jenkins-pipeline,Jenkins,Jenkins Pipeline,我的目的很简单,只想在用户单击该阶段输入步骤中的“中止”按钮时在该阶段中执行一些post操作。我阅读了jenkins.io中的一些文档,发现使用post指令似乎有一种隐式的方法。因此,我在下面做了一些简单的测试: 首先是: pipeline { agent any stages { stage('test') { input { message 'Proceed?' ok 'y
pipeline {
agent any
stages {
stage('test') {
input {
message 'Proceed?'
ok 'yes'
submitter 'admin'
}
steps {
echo "helloworld"
}
post {
aborted {
echo "stage test has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
如果单击“中止”按钮,日志输出将仅显示:
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] input
Proceed?
yes or Abort
Aborted by admin
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Rejected by SYSTEM
Finished: ABORTED
这意味着输入中止只会触发管道的操作后部分,而不会触发该阶段内的部分。
然后我尝试另一个:
pipeline {
agent any
stages {
stage('test') {
steps {
sh "sleep 15"
}
post {
aborted {
echo "stage test has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
我在15秒内中止此作业,输出将显示
[Pipeline] { (hide)
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] sh
+ sleep 15
Sending interrupt signal to process
Aborted by admin
Terminated
script returned exit code 143
Post stage
[Pipeline] echo
stage test has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: ABORTED
这意味着可以触发阶段中中止后的操作部分
当中止一个阶段内的输入步骤而不是整个管道时,我如何执行一些post操作?我想我已经弄清楚了上面两个示例之间的区别,以及如何自己解决这个问题:) 似乎只有当相关步骤部分的currentResult匹配,而不是上面的输入部分匹配时,才会触发阶段中的Post操作。所以我做了一点小小的改变,效果很好。即使输入部分成为步骤部分内的脚本
pipeline {
agent any
stages {
stage('test') {
steps {
script {
input message: 'Proceed?', ok: 'Yes', submitter: 'admin'
}
echo "helloworld"
}
post {
aborted{
echo "test stage has been aborted"
}
}
}
}
post {
aborted {
echo "pipeline has been aborted"
}
}
}
单击中止按钮时,输出日志为:
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/jenkins_home/workspace/pipeline-demo
[Pipeline] {
[Pipeline] stage
[Pipeline] { (test)
[Pipeline] script
[Pipeline] {
[Pipeline] input
Proceed?
Yes or Abort
[Pipeline] }
[Pipeline] // script
Post stage
[Pipeline] echo
test stage has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Declarative: Post Actions)
[Pipeline] echo
pipeline has been aborted
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Rejected by admin
Finished: ABORTED
这让我很满意:)。我现在可以通过用户确认和自动回滚组成一个复杂的管道。希望它也能帮助你