Jenkins输入步骤在达到等待阈值限制后执行某些操作

ergxz8rk  于 2023-02-07  发布在  Jenkins
关注(0)|答案(1)|浏览(147)

我的管道作业中有一个阶段需要用户输入才能继续到下一个阶段。问题是有时候我在等待N分钟后忘记单击继续按钮。我想发送一条Slack消息,通知我该阶段已暂停N分钟。有什么方法可以实现吗?
下面是我的管道脚本示例:

pipeline {
    agent any

    stages {
        stage('A') {
            steps {
                echo 'Starting Stage A'
                input message: 'Continue to the next stage?'
                // send Slack message after 15 minutes user didn't click Proceed/Abort button
                // but still wait for user input (don't mark it as failed even after 15 minutes)
            }
        }

        stage('B') {
            steps {
                echo 'Starting Stage B'
            }
        }
    }
}

我已尝试使用下面的管道脚本使用Jenkins超时功能
但是,如果超时,它会自动继续到下一个阶段。我想要的是,即使超时后,仍然等待用户在阶段A的输入(不要立即继续到阶段B)

pipeline {
    agent any

    stages {
        stage('A') {
            steps {
                script {
                    try {
                        echo 'Starting Stage A'
                        timeout(time: 15, unit: 'MINUTES') {
                            input message: 'Continue to the next stage?'
                        }
                    } catch (err) {
                        def user = err.getCauses()[0].getUser()
                        if ('SYSTEM' == user.toString()) { // failed because of timeout
                            // send Slack message
                            // how to still wait for user input on this stage even after timeout is reached?
                        }
                    }
                }
            }
        }

        stage('B') {
            steps {
                echo 'Starting Stage B'
            }
        }
    }
}

谢谢

bnlyeluc

bnlyeluc1#

我用下面的脚本计算出来的:

pipeline {
    agent any

    stages {
        stage('A') {
            steps {
                script {
                    echo 'Starting Stage A'

                    def showInputAgain = false
                    def proceed = true

                    try {
                        timeout(time: 15, unit: 'SECONDS') {
                            input message: 'Continue?'
                        }
                    } catch (err) {
                        def user = err.getCauses()[0].getUser()
                        if ('SYSTEM' == user.toString()) {
                            showInputAgain = true
                            echo 'Stage A failed. Reach timeout. Sending message to Slack'
                        } else {
                            proceed = false
                            echo 'Stage A failed. User abort'
                        }
                    }

                    if (proceed && showInputAgain) {
                        input message: 'Continue?'
                    }
                }
            }
        }

        stage('B') {
            steps {
                echo 'Starting Stage B'
            }
        }
    }
}

但是任何其他输入/回答都将受到赞赏

相关问题