Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jenkins: join multiple DSL files in one

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.

like image 941
Misha Slyusarev Avatar asked Aug 17 '16 12:08

Misha Slyusarev


1 Answers

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)
like image 130
Misha Slyusarev Avatar answered Sep 29 '22 10:09

Misha Slyusarev