1

https://github.com/jenkinsci/job-dsl-plugin/wiki/JCasC provides the following example of a separate groovy file referenced in the Jenkins Configuration as Code (CasC) syntax.

jobs:
  - providedEnv:
      SUPERHERO: 'Midnighter'
  - file: ./jobdsl/job.groovy
//job.groovy
job('awesome-job') {
    description("favorite job of ${SUPERHERO}")
}

I'm looking for a way of defining a few very similar jobs which only differ by a value or two. In the example above, the SUPERHERO variable looks to be a global, but I need a way to reuse the same groovy include with per-include variables.

Pseudo-code example:

jobs:
 - file: ./jobdsl/job.groovy
   providedEnv:
      SUPERHERO: 'Superman'
 - file: ./jobdsl/job.groovy
   providedEnv:
      SUPERHERO: 'Batman'

Does such a construct exist?

Jamie Jackson
  • 1,158
  • 3
  • 19
  • 34

2 Answers2

0

This appears to work:

jobs:
  - providedEnv:
      SUPERHERO: 'Batman' 
  - file: ./jobdsl/job.groovy
  - providedEnv:
      SUPERHERO: 'Superman' 
  - file: ./jobdsl/job.groovy

But I don't know if I'm exploiting a fragile, undocumented behavior or not.

I would prefer to see an explicit association of the environment variables with the file reference; something like this:

jobs:
 - file: ./jobdsl/job.groovy
   providedEnv:
      SUPERHERO: 'Batman'
 - file: ./jobdsl/job.groovy
   providedEnv:
      SUPERHERO: 'Superman'

...but alas this (guess at a) syntax doesn't work. (Jenkins starts but the jobs are absent.)

Jamie Jackson
  • 1,158
  • 3
  • 19
  • 34
  • Well, I'm not crazy about it, but it gets the job done and it's the best workaround I know for my use case, so I'm marking this as the answer. – Jamie Jackson Jan 20 '20 at 19:39
0

How about just doing this?

jobs:
  - providedEnv:
      JOB_NAMES: 'Batman,Superman'
  - file: ./jobdsl/jobs.groovy

You then have a loop inside jobs.groovy to split JOB_NAMES into a list.

ctran
  • 21
  • 3
  • Thanks. That gets more and more awkward the more environment variables and values that you have, though. My current use case has about four jobs with five variables a piece, so synchronizing (and visually parsing) the lists wouldn't work well for me. However, it might make sense for some use cases. – Jamie Jackson Jan 07 '20 at 23:28