【发布时间】:2019-10-30 17:05:21
【问题描述】:
我的目的很简单,只是想在用户在该阶段的输入步骤中单击“中止”按钮时在该阶段执行一些发布操作。我从 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"
}
}
}
如果我点击 Abort 按钮,日志输出只会显示:
[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
这意味着可以触发阶段中中止后的动作部分。
在一个阶段中中止输入步骤时,我如何执行一些发布操作,而不是整个管道?
【问题讨论】: