Jenkins声明性管道 - 用户输入参数

Jac*_*ack 7 jenkins-pipeline

我已经使用Jenkins声明性管道查找了一些用户输入参数的示例,但是所有示例都使用脚本化管道.这是我正在努力工作的代码示例:

pipeline {
    agent any

    stages {
        stage('Stage 1') {
            steps {
                 input id: 'test', message: 'Hello', parameters: [string(defaultValue: '', description: '', name: 'myparam')]
                 sh "echo ${env}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我似乎无法弄清楚如何访问myparam变量,如果有人可以帮助我,那将会很棒.谢谢

Rob*_*les 11

使用输入时,在全局管道级别使用agent none并将代理分配给各个阶段非常重要.将输入过程放在一个单独的阶段,该阶段也使用无代理.如果为输入阶段分配代理节点,则该代理执行程序将保留此构建的保留,直到用户继续或中止构建过程.

此示例应该有助于使用输入:

def approvalMap             // collect data from approval step

pipeline {
    agent none

    stages {
        stage('Stage 1') {
            agent none
            steps {

                timeout(60) {                // timeout waiting for input after 60 minutes
                    script {
                        // capture the approval details in approvalMap.
                        approvalMap = input id: 'test', message: 'Hello', ok: 'Proceed?', parameters: [choice(choices: 'apple\npear\norange', description: 'Select a fruit for this build', name: 'FRUIT'), string(defaultValue: '', description: '', name: 'myparam')], submitter: 'user1,user2,group1', submitterParameter: 'APPROVER'
                    }
                }
            }
        }
        stage('Stage 2') {
            agent any

            steps {
                // print the details gathered from the approval
                echo "This build was approved by: ${approvalMap['APPROVER']}"
                echo "This build is brought to you today by the fruit: ${approvalMap['FRUIT']}"
                echo "This is myparam: ${approvalMap['myparam']}"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

当输入函数返回时,如果它只返回一个参数,则直接返回该值.如果输入中有多个参数,则返回值的映射(散列,字典).为了捕获这个值,我们必须放弃groovy脚本.

最好将输入代码包装在超时步骤中,以使构建在长时间内不会保持未解析状态.