带有超时的输入步骤,使用Jenkins管道继续默认设置

时间:2016-07-26 18:08:06

标签: jenkins jenkins-pipeline

所需Jenkins管道的描述:

  • 等待1分钟输入(“这是发布版本吗?是/否”)
    • 如果输入为yes,则执行发布类型构建(使用内部版本号和部署)
    • 如果超时或用户说不,那么进行测试构建
    • 如果用户按下Abort,则执行正常中止

我目前的代码是:

try {
    timeout(1) {
        input message: 'Do you want to release this build?',
              parameters: [[$class: 'BooleanParameterDefinition',
                            defaultValue: false,
                            description: 'Ticking this box will do a release',
                            name: 'Release']]
    }
} catch (err) {
    def hi = err.getCauses()
    echo "Exception thrown:\n ${hi}"

    echo err.getLocalizedMessage()
    echo err.getCause()
    echo err.toString()
    echo err.getClass().getName()
}

但这会给出相同的(据我所知)行为并且为用户按下“Abort”并输入超时时捕获错误。

以下是超时的输出:

[Pipeline] timeout
[Pipeline] {
[Pipeline] input
Input requested
[Pipeline] }
[Pipeline] // timeout
[Pipeline] echo
Exception thrown:
 [org.jenkinsci.plugins.workflow.support.steps.input.Rejection@5ac94906]
[Pipeline] echo
null
[Pipeline] echo
null
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] echo
org.jenkinsci.plugins.workflow.steps.FlowInterruptedException
[Pipeline] End of Pipeline
Finished: SUCCESS

当我按下“Abort”时,除了Rejection @

后的十六进制除外

如果输入步骤可以选择在超时后继续使用默认选项,那将会很棒。

编辑:为错误添加了更多打印以尝试确定其类型

2 个答案:

答案 0 :(得分:2)

我最近实施了类似的行为 这个声明性管道代码需要在启动时提供BRANCH_TO_BUILD参数(仅当它是手动启动时,否则将使用默认值)然后激活Git分支的交互式输入。 /> 因此用户有几种选择:

  • 不执行任何操作(将使用默认值)
  • 确认默认值
  • 输入新值
  • 中止执行预防[Abort]

是的,记得确认Groovy函数是Blazej写的

pipeline {
    agent none

    parameters {
        string(name: 'BRANCH_TO_BUILD', defaultValue: "develop", description: 'GIT branch to build')
    }   

    environment { 
        GIT_URL = 'ssh://user@git/repo.git'
        BRANCH_TO_BUILD_DEFAULT = 'develop'
        BRANCH_TO_BUILD_REQUESTED = "${params.BRANCH_TO_BUILD}"
    }


    stage('Configure the build') {
        agent none
        steps {
            echo "Prompt user for a branch to build (default: ${BRANCH_TO_BUILD_DEFAULT})"
            script {
                try {
                    timeout(time:30, unit:'SECONDS') {
                    BRANCH_TO_BUILD_REQUESTED = input(
                        message: 'Input branch to build', 
                        parameters: [
                                [$class: 'TextParameterDefinition', 
                                 defaultValue: BRANCH_TO_BUILD_DEFAULT, 
                                 description: 'Branch name', name: 'Enter branch name (or leave default) and press [Proceed]:']
                            ])
                        echo ("User has entered the branch name: " + BRANCH_TO_BUILD_REQUESTED)
                    }
                } catch(err) { // timeout reached or input Aborted
                    def user = err.getCauses()[0].getUser()
                        if('SYSTEM' == user.toString()) { // SYSTEM means timeout
                            echo ("Input timeout expired, default branch will be used: " + BRANCH_TO_BUILD_DEFAULT)
                            BRANCH_TO_BUILD_REQUESTED = BRANCH_TO_BUILD_DEFAULT
                        } else {
                            echo "Input aborted by: [${user}]"
                            error("Pipeline aborted by: [${user}]")
                        }
                }
            }
        }
    }

    stage('Checkout') {
        agent{node {label 'worker-1'}}

        steps {
            echo "Checkout will be done for Git branch: ${BRANCH_TO_BUILD_REQUESTED}"
            checkout([$class: 'GitSCM', branches: [[name: "*/${BRANCH_TO_BUILD_REQUESTED}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[url: ${GIT_URL]}]])
            }
    }

}

答案 1 :(得分:1)

您可以提取有关从异常原因中拒绝的用户的信息。 org.jenkinsci.plugins.workflow.support.steps.input.Rejection有一个 getUser()方法(返回' SYSTEM'表示超时和中止的完整用户名)。

try {
    timeout(time: 15, unit: 'SECONDS') {
        input message: 'Do you want to release this build?',
              parameters: [[$class: 'BooleanParameterDefinition',
                            defaultValue: false,
                            description: 'Ticking this box will do a release',
                            name: 'Release']]
    }
} catch (err) {
    def user = err.getCauses()[0].getUser()
    echo "Aborted by:\n ${user}"
}

请注意,这需要批准groovy沙箱(管理Jenkins>进程内脚本批准)。