Search code examples
jenkinsjenkins-groovyjenkins-job-dsl

Cannot create freeStyleJob jobs in a loop - No signature of method: script.freeStyleJob() is applicable for argument types: Integer, Closure


I am trying to create many Jenkins jobs for many branches given in the bitbucket repo. So, for the moment I developed a groovy script that retrieves all branches. Note that I am using the Use the provided DSL script, to do that :

def project = "Test-Jobs"
def command = "git ls-remote -h $GIT_URL"

def proc = command.execute()
proc.waitFor()             

// trouver toutes les branches

def branches = proc.in.text.readLines().collect { 
    it.replaceAll(/[a-z0-9]*\trefs\/heads\//, '') 
}

println branches

This return :

[feature/655, feature/BPDC-655, master, release/1.0.0, release/1.2.0, release/2.4.0]

Then, I tried to create a name job for each branch using this code :

def jobNames = branches.collect { it + "_test" }
println jobNames

This was fine also. But, my problem was about creating freeStyleJob for each branch :

for (ii = 0; ii < jobNames.size(); ii++)
{    
    
  freeStyleJob(ii) {
    logRotator(-1, 10)
    steps {
        shell ("echo first programm")
    }
        }
    } 

But this does not work at all. And, I get this error :

ERROR: (script, line 26) No signature of method: script.freeStyleJob() is applicable for argument types: (java.lang.Integer, script$_run_closure3) values: [0, script$_run_closure3@4e08608d] Possible solutions: freeStyleJob(java.lang.String), freeStyleJob(java.lang.String, groovy.lang.Closure) Finished: FAILURE

In fact, I tried many times to iterate through the jobNames list object, but I got usually errors. Thank you for your help.


Solution

  • The freeStyleJob block requires a String variable. ii keeps an integer. You have to convert it to String. Few options:

    freeStyleJob(ii as String) {
    
    freeStyleJob(Integer.toString(ii)) {
    
    freeStyleJob("${ii}") {