Importing JobDSL methods in groovy

88 views
Skip to first unread message

Zeke Santamaria

unread,
Sep 20, 2022, 11:45:54 AM9/20/22
to job-dsl-plugin
This might have been answered already, but I don’t even know what to look for so I’m posting openly.
I have a seeder job that is a seeder.Jenkinsfile that has some logic to create jobs using a class I created jobs.groovy.
Everything works well with this model. I use a List to abstract complexity out of the job’s creation process and I end up iterating over that list/map and I use the Job(jobName) and other parameters that I pass to setup cron, enable/disable, params, etc...
works beautifully.
Problem is I have to replicate that logic in every seeder job to iterate over the data structure (List/Map) I created (I have plenty of seeder jobs). So if I update the logic o add a new parameter, I need to update the logic in every seeder.
I could move that logic into the class Jobs.groovy and that would be ideal because I only need to modify the logic in the class and I can call the Job() from that groovy file.
The problem I face when doing this is that the Jobs.groovy file doesn’t have access to the Job() method, or any JobDSL method for that matter.
I tried importing the javaposse.jobdsl.dsl.Job method into my Jobs.groovy but this didn’t work.
I end up with a "No signature of method: com.team.Jobs$_allJobs_closure1.job() error.

Any help is appreciated.
Thank you

Jared Patterson

unread,
Sep 20, 2022, 3:11:56 PM9/20/22
to job-dsl...@googlegroups.com
Hey Zeke,

You can pass in additionalParameters to the jobDsl method (see below). This allows you to load the list/map structure in the Jenkinsfile, then pass that data in as a param, which is then accessible inside Jobs.groovy as an envVariable, eg

Jenkinsfile
----------------
List<Map> config = [
  [
    jobName: 'my-test-job'
  ]
]

jobDsl(targets: "some/src/path/Jobs.groovy",
  additionalClasspath: 'src/main/groovy',
  removedJobAction: 'DELETE',
  removedViewAction: 'DELETE',
  removedConfigFilesAction: 'DELETE',
  additionalParameters: [MY_LIST_OF_MAPS: config],
  sandbox: true
)
----------------

And then inside Jobs.groovy, you can access the param directly, eg

Jobs.groovy
----------------
MY_LIST_OF_MAPS.each { Map item ->
  println item.jobName
}
---------------

If your also testing these scripts, you can pass in a test structure via the envVars property:
---------------
JobManagement jm = new JenkinsJobManagement(System.out, [MY_LIST_OF_MAPS:config], new File('.'))
---------------


Cheers
Jared
--
You received this message because you are subscribed to the Google Groups "job-dsl-plugin" group.
To unsubscribe from this group and stop receiving emails from it, send an email to job-dsl-plugi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/job-dsl-plugin/689311f7-3773-4e41-b7e8-789b9ce69976n%40googlegroups.com.

Reply all
Reply to author
Forward
0 new messages