Sameer Atharkar
Sameer Atharkar

Reputation: 442

Jenkins Parameterized Project For Multiple Parameters

I have Jenkins Pipeline which is triggering for different projects. However the only difference in all the pipelines is just the name. So I have added a parameter ${project} in parameter of jenkins and assigned it a value of the name of the project.

We have a number of projects and I am trying to find a better way through which I can achieve this.

I am thinking how can we make the parameter run with different parameters for all the projects without actually creating different projects under jenkins.

I am pasting some screenshot for you to understand what exactly I want to achieve.

enter image description here

As mentioned here, this is a radioserver project, having a pipeline which has ${project} in it.

How can I give multiple values to that {project} from single jenkins job? IF you have any doubts please message me or add a comment.

enter image description here

You can see those 2 projects I have created, it has all the contents same but just the parameterized value is different, I am thinking how can I give the different value and make You can see those 2 projects I have created, it has all the contents same but just the parameterized value is different, I am thinking how can I give the different value to that parameter.

enter image description here

As you can see the 2 images is having their default value as radioserver, nrcuup. How can I combine them and make them run seemlessly ?

Upvotes: 0

Views: 2678

Answers (1)

Sourav
Sourav

Reputation: 3392

I hope this will help. Let me know if any changes required in answer.

You can use conditions in Jenkins. Based on the value of ${PROJECT}, you can then execute the particular stage.

Here is a simple example of a pipeline, where I have given choices to select the value of parameter PROJECT i.e. test1, test2 and test3.

So, whenever you select test1, jenkins job will execute the stages that are based on test1

Sample pipeline code

pipeline {
    agent any
    parameters {
        choice(
            choices: ['test1' , 'test2', 'test3'],
            description: 'PROJECT NAME',
            name: 'PROJECT')
    }

    stages {
        stage ('PROJECT 1 RUN') {
            when {
                expression { params.PROJECT == 'test1' }
            }
            steps {
                echo "Hello, test1"
            }
        }
        
        stage ('PROJECT 2 RUN') {
            when {
                expression { params.PROJECT == 'test2' }
            }
            steps {
                echo "Hello, test2"
            }
        }
    }
}

Output:

  1. when test1 is selected

enter image description here

enter image description here

  1. when test2 is selected

enter image description here

enter image description here


Updated Answer

Yes, it is possible to trigger the job periodically with a specific parameter value using the Jenkins plugin Parameterized Scheduler

After you save the project with some parameters (like above mentioned pipeline code), go back again to the Configure and under Build Trigger, you can see the option of Build periodically with parameters

Example:

I will here run the job for PROJECT=test1 every even minutes and PROJECT=test2 every uneven minutes. So, below is the configuration

enter image description here

*/2 * * * * %PROJECT=test1
1-59/2 * * * * %PROJECT=test2

Please change the crontab values according to your need

Output:

enter image description here

Upvotes: 1

Related Questions