edouard
edouard

Reputation: 152

Jenkins pipeline - build a job with default input value from another job

I have a Jenkins Pipeline called pipeline1, described by the following 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)
    }
}

It asks the user to choice some input in the Configuration stage and just echos them in the Run stage. I would like to trigger this job from another one and automatically fill the input to avoid human action. I've tried to use the same syntax we use to build a parametrized job and come to something like this:

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

But it doesn't work. It does trigger the pipeline1 job, but it wait for the user to fill the input...

EDIT: I would like to keep the input feature in pipeline1 instead of having a standard parametrized job.

Do you have any idea to achieve this ?

Thanks a lot.

Upvotes: 2

Views: 3587

Answers (1)

3sky
3sky

Reputation: 890

OK so, I have got a complete answer for You. Use properties in pipeline1 and if/else block:

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)
    }
}

Job 2 with little changes:

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"]
        ]       
    }
}

If You want to run job2, and then use User Input, just change useIn for Yes. So at this moment You can run directly pipeline1 job with User Input:

[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 

or trigger it by job2, without User Input:

[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 

Upvotes: 3

Related Questions