我的目的很简单,只想在该阶段的输入步骤中用户单击“中止”按钮时在该阶段中执行一些后期操作。我已经从jenkins.io阅读了一些文档,发现似乎存在使用post指令执行此操作的隐式方法。因此,我在下面做了一些简单的测试:
首先是这个:
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
这意味着Input Abort仅触发流水线的后操作部分,而不触发该阶段内的部分。 然后我尝试另一个:
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
这意味着可以触发阶段中的后中止动作部分。
当中止某个阶段(而不是整个管道)中的输入步骤时,我该如何执行一些后期操作?
答案 0 :(得分:1)
我想我想出了上述两个示例之间的区别以及如何自己解决此问题:)。
似乎只有在相关的“步骤”部分的currentResult匹配但在上面的“输入”部分不匹配时,才会触发阶段中的“发布”操作。因此,我进行了一些更改,并且该方法有效。也就是说,将“输入”部分设为“步骤”部分中的脚本。
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
这令我满意:)。我可以通过用户确认和自动回滚来组成一个复杂的管道。希望它也能帮助您。