I'd like to unify Jenkins job generation process and want to have one single file that I would pass to Process Job DSLs step.
So I could vary files that included in current release of DSL. Say I have these files:
..
Release.groovy
FirstPipeline.groovy
SecondPipeline.groovy
And I want Release.groovy to include either both pipelines or maybe just single pipeline.
Inside pipeline files there is no Class structure, so I cannot import them as if they were a library. It has just something like this:
import mylibs.jobs.UsefulJob1
import mylibs.jobs.UsefulJob2
import mylibs.jobs.FirstPipeline
def firstPipeline = new FirstPipeline()
multiJob(firstPipeline.name) {
// multijob steps
}
I tried to use evaluate, but it turned out that it works only for simple scripts. And if you use it with more complex hierarchy of imported libraries and meta programming it fails with hardly interpretable errors.
For those who are wondering what's the answer. I ended up with refactoring these scripts into classes. And in my case firstPipeline looks something like this:
package pipelines
import mylibs.jobs.UsefulJob1
import mylibs.jobs.UsefulJob2
import javaposse.jobdsl.dsl.DslFactory
class FirstPipeline {
public FirstPipeline(DslFactory dslFactory) {
dslFactory.multiJob('first_pipeline') {
// multijob steps
}
}
}
Then you can gather them all in one file:
import pipelines.FirstPipeline
import pipelines.SecondPipeline
new FirstPipeline(this)
new SecondPipeline(this)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With