【问题标题】:Jenkins pipeline - build a job with default input value from another jobJenkins 管道 - 使用来自另一个作业的默认输入值构建作业
【发布时间】:2018-08-06 15:51:49
【问题描述】:

我有一个名为 pipeline1 的 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)
    }
}

它要求用户在配置阶段选择一些输入,然后在运行阶段回显它们。 我想从另一个工作触发这项工作并自动填充输入以避免人为操作。 我尝试使用我们用来构建参数化作业的相同语法并得出这样的结果:

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 中,而不是使用标准的参数化作业。

你有什么想法来实现这个目标吗?

非常感谢。

【问题讨论】:

  • 只需删除stage("Configuration"),并使用params.server和params.action。
  • @3sky 我没有明确表示我希望能够手动运行我的工作并保持动态输入而不是标准的参数化工作。

标签: jenkins jenkins-pipeline


【解决方案1】:

好的,我已经为你找到了完整的答案。在 pipeline1 和 if/else 块中使用 properties:

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 

【讨论】:

  • 我认为这是一个很好的解决方案,对我来说更好的是,您不需要在第一个管道中使用“手动”参数,那么您就不会询问您手动触发工作的用户或一个额外的参数来填充。我在这里学到的是,您可以将参数 (useIn) 传递给未参数化的作业,这很棒!
  • 我只想涵盖所有情况:)
猜你喜欢
  • 2018-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-22
  • 2022-01-24
  • 2022-06-10
  • 1970-01-01
  • 2014-04-08
相关资源
最近更新 更多