Sviatlana Sviatlana - 1 month ago 34
Groovy Question

Set quietPeriod in cycle Jenkins pipeline

I have this pipeline script:

node{
String[] testNames = ["A", "B", "C"]
def tests = [:]
for ( int i = 0; i < testNames.size(); i++ )
{
def testJobName = testNames[i]
tests[testJobName] =
{
build job: testJobName, quietPeriod: 10*i
}
}
parallel tests
}


I need to set Different quiet period for each job in cycle. In this case: 0 seconds for job A, 10 seconds for job B and 20 sec for C. And jobs A, B and C should be launched in parallel: A job starts, B job waits for 10 seconds and starts too, ะก job waits 10 seconds after B started (20 sec from very beginning) and starts. Instead of this, all builds wait for 30 seconds and then start at the same time. Please, help. Thanks.

Answer

This seems groovy language specification of closure with loop counter. A possible workaround is to set period variable out of the closure like this:

node{
    String[] testNames = ["A", "B", "C"]
    def tests = [:]
    for ( int i = 0; i < testNames.size(); i++ )
    {
        def testJobName = testNames[i]
        int period = i*10
        tests[testJobName] =
        {
            build job: testJobName, quietPeriod: period
        }
    }
    parallel tests
}

You can see a more detailed answer here: http://stackoverflow.com/a/32252464/6894050