bot
bot

Reputation: 1423

How can I return parameters in Jenkins pipeline based upon the job name?

I have two Jenkins pipeline (a)deploy and (b)test_deploy. I want to run both these pipeline from one Jenkinsfile as there functionality is same.

I want to only show test environments as parameters in test_deploy pipeline. I want to add something like if/else conditions on choice parameters that will return environment names in parameters based upon the job name. How can I condition choice/parameters based upon job name?

Upvotes: 1

Views: 495

Answers (1)

Dibakar Aditya
Dibakar Aditya

Reputation: 4203

You can add this at the top of your Jenkinsfile:

if (JOB_NAME == 'deploy') {
    properties([
        parameters([
            choice(name: 'deployEnv', choices: ['deployEnv1', 'deployEnv2'], description: '')
        ])
    ])
} else if (JOB_NAME == 'test_deploy') {
    properties([
        parameters([
            choice(name: 'testEnv', choices: ['testEnv1', 'testEnv2'], description: '')
        ])
    ])
}

This works in both Declarative and Scripted Pipelines, and populates the choice parameters based on the job names.

Upvotes: 2

Related Questions