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

Har*_*lon 5 jenkins jenkins-pipeline

所需 Jenkins 管道的描述:

  • 等待 1 分钟输入(“这是发布版本吗?是/否”)
    • 如果输入为是,则执行发布类型构建(带有构建号和部署)
    • 如果超时或用户说不,则进行测试构建
    • 如果用户按下 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()
}
Run Code Online (Sandbox Code Playgroud)

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

这是超时的输出:

[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
Run Code Online (Sandbox Code Playgroud)

当我按“中止”时,除了 Rejection@ ​​之后的十六进制之外,它是相同的

如果输入步骤可以在超时后选择继续使用默认选项,那就太好了。

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

Leo*_*kin 9

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

  • 保持不动作(将使用默认值)
  • 确认默认值
  • 输入新值
  • 按 [Abort] 中止执行

是的,记得按照 Blazej 写的那样确认 Groovy 函数

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 a user for the 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]}]])
            }
    }
    
}
Run Code Online (Sandbox Code Playgroud)


hak*_*iri 5

您可以从异常原因中提取有关被拒绝的用户的信息。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}"
}
Run Code Online (Sandbox Code Playgroud)

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