Anastazja  Bondarenko
Anastazja Bondarenko

Reputation: 199

How can I run a job in Jenkins n-times?

Is it possible in Jenkins to create a job, that will run n-times?

I would like to write a script in configuration (windows batch command / groovy) which allows me to do it. In this script, I would like to have an array with parameters and then run this job with each parameter in the cycle. It should look like that:

paramArray [] = ["a","b","c"];
for(int i = 0; i < paramArray.length; i++)
{
    //Here I want to run this job with each parameter
    job.run(paramArray[i]);
}

Please, help me with that issue.

Upvotes: 1

Views: 6784

Answers (3)

Phani Kandula
Phani Kandula

Reputation: 397

Please see the example from https://jenkins.io/doc/book/pipeline/jenkinsfile/ in "Handling parameters" section: With a Jenkinsfile like this (example here copied from that doc), you can launch "Build with parameters" and give params. Since you want multiple params, you can delimit them with , or ; or something else based on your data. You just need to parse the input params to get the values using the delimiter you chose.

pipeline {
    agent any
    parameters {
        string(name: 'Greeting', defaultValue: 'Hello', description: 'How should I greet the world?')
    }
    stages {
        stage('Example') {
            steps {
                echo "${params.Greeting} World!"
            }
        }
    }
}

Upvotes: 0

Anastazja  Bondarenko
Anastazja Bondarenko

Reputation: 199

I found the answer!

We need to create 2 pipelines in Jenkins: downstream and upstream jobs.

1. The downstream job is parameterized and take 1 string parameter in 'General' section enter image description here

Then, it just prints the choosing parameter in 'Pipeline' section: enter image description here

Here is the result of this downstream job: enter image description here

2. The upstream job has an array with all possible parameters for a downstream job. And in the loop, it runs a downstream job with each parameter from an array. enter image description here

In the result, an upstream job will run a downstream job 3 times with each parameter.

:)

Upvotes: 4

Subhash
Subhash

Reputation: 762

I think you can't run Jenkins job according to your above code. But you can configure the cronjob in Jenkins using “Build periodically” for run Jenkins job periodically.

go to Jenkins job > Configure > tick Build periodically in build Triggers and put cronjob syntax like below image and Save.

enter image description here

This job runs every 15 minutes. and also you can set a specific time in the schedule.

Upvotes: 3

Related Questions