【问题标题】:Jenkins Pipeline: abort an input in a stage cannot trigger the aborted post action of that stageJenkins Pipeline:中止一个阶段中的输入不能触发该阶段的中止后操作
【发布时间】: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

这意味着可以触发阶段中中止后的动作部分。

在一个阶段中中止输入步骤时,我如何执行一些发布操作,而不是整个管道?

【问题讨论】:

    标签: jenkins jenkins-pipeline


    【解决方案1】:

    我想我弄清楚以上两个示例之间的区别以及如何自己解决这个问题:)。

    似乎只有在相关 Steps 部分的 currentResult 匹配但不匹配上面的 Input 部分时才会触发阶段中的 Post 操作。所以我做了一点改变,它起作用了。即让 Input 部分成为 Steps 部分中的脚本。

    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"
            }
        }
    }
    

    当点击Abort按钮时,输出日志为:

    [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
    

    这让我很满意:)。我现在可以通过用户确认和自动回滚来组成一个复杂的管道。希望对你也有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多