Jenkins管道-使用另一个作业的默认输入值构建一个作业

时间:2018-08-03 14:38:52

标签: jenkins jenkins-pipeline

我有一个名为pipe1的Jenkins管道,由以下Jenkinsfile描述:

node()
{
    def server
    def action

    stage("Configuration")
    {
        def userInput = input(
            id: 'userInput', message: 'Let\'s configure this run !', parameters: [
                choice(choices: "server1\nserver2", name: "server", description: "Which server do you want to use ?"),
                choice(choices: "stop\nstart\nrestart", name: "action", description: "What do you want to do ?")
        ]);

        server = userInput.server;
        action = userInput.action;
    }

    stage("Run")
    {
        echo(server)
        echo(action)
    }
}

它要求用户在Configuration阶段选择一些输入,并在Run阶段回显它们。 我想从另一项触发该作业,并自动填写输入内容,以避免人工操作。 我尝试使用与构建参数化工作所用的语法相同的语法,

node()
{
    stage("Run")
    {
        build job: 'pipeline1', wait: true, parameters: [
            [$class: 'StringParameterValue', name: 'userInput.server', value: "server1"],
            [$class: 'StringParameterValue', name: 'userInput.action', value: "stop"]
        ]       
    }
}

但是它不起作用。它确实触发了pipeline1作业,但是它等待用户填充输入...

编辑:我想将输入功能保留在pipeline1中,而不要执行标准的参数化工作。

您有实现此目标的想法吗?

非常感谢。

1 个答案:

答案 0 :(得分:2)

好的,我已经为您提供了完整的答案。在管道1和properties块中使用if/else

properties([
  parameters([
    choice(name: 'manually',
           description: 'Do you whish to use a user input?',
           choices: 'No\nYes')
  ])
])

node()
{
    def server
    def action

    stage("Configuration") {
        if ( params.useIn == 'Yes' || params.manually == 'Yes' ) {
            def userInput = input(
            id: 'userInput', message: 'Let\'s configure this run !', parameters: [
                choice(choices: "server1\nserver2", name: "server", description: "Which server do you want to use ?"),
                choice(choices: "stop\nstart\nrestart", name: "action", description: "What do you want to do ?")]
            );
            server = userInput.server;
            action = userInput.action;
        } else {
            server = params.server
            action = params.action
        }

    }

    stage("Run")
    {
        echo(server)
        echo(action)
    }
}

作业2几乎没有变化:

node()
{
    stage("Run")
    {
        build job: 'pipeline1', wait: true, parameters: [
            [$class: 'StringParameterValue', name: 'useIn', value: "No"],
            [$class: 'StringParameterValue', name: 'server', value: "server1"],
            [$class: 'StringParameterValue', name: 'action', value: "start"]
        ]       
    }
}

如果要运行job2,然后使用User Input,只需将useIn更改为Yes。因此,此时您可以使用用户输入直接运行pipeline1作业:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Configuration)
[Pipeline] input
Input requested
Approved by 3sky
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run)
[Pipeline] echo
server2
[Pipeline] echo
restart
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS 

或由job2触发,无需用户输入:

[Pipeline] {
[Pipeline] stage
[Pipeline] { (Configuration)
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run)
[Pipeline] echo
server1
[Pipeline] echo
start
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline 
Finished: SUCCESS