4

We have got a Jenkins setup (at the moment not managed with the Jenkins Job Builder) where every web project has 5 very similar Jenkins jobs. This is a simplified Jenkins Job Builder yaml file which could work for our needs:

- defaults:
    name: global
    project-type: 'freestyle'
    disabled: false
    buildTargets: 'build'
- job-template:
    name: '{jobNameFirstSegment}-{jobNameSecondSegment}'
    defaults: global
    builders:
      - ant:
          targets: '{buildTargets}'
          buildfile: 'build.xml'
- project:
    name: lorem
    jobNameFirstSegment:
      - foo:
          displayNameFirstSegment: 'Foo'
          urlGitRepository: 'git@bitbucket.org:mycompany/foo.git'
      - bar:
          displayNameFirstSegment: 'Bar'
          urlGitRepository: 'git@bitbucket.org:mycompany/bar.git'
      - baz:
          displayNameFirstSegment: 'Baz'
          urlGitRepository: 'git@bitbucket.org:mycompany/baz.git'
    jobNameSecondSegment:
      - lorem:
          buildTargets: 'build-lorem'
      - ipsum:
          buildTargets: 'build-ipsum'
      - dolor:
          buildTargets: 'build-dolor'
      - sit:
          buildTargets: 'build-sit'
      - amet:
          buildTargets: 'build-amet'
    jobs:
      - '{jobNameFirstSegment}-{jobNameSecondSegment}'

This yaml file results in 15 Jenkins jobs - all are NOT disabled:

  • foo-lorem
  • foo-ipsum
  • foo-dolor
  • foo-sit
  • foo-amet
  • bar-lorem
  • bar-ipsum
  • bar-dolor
  • bar-sit
  • bar-amet
  • baz-lorem
  • baz-ipsum
  • baz-dolor
  • baz-sit
  • baz-amet

Is it possible with this kind of setup to disable a specific Jenkins job? I could not find out how to override the "disabled" attribute for e.g. the Jenkins job "foo-lorem". How can this be achieved?

Infact we would need to have the possibility to be able to override any attribute of any Jenkins job if needed. Is this doable? How?

mostwanted
  • 1,549
  • 3
  • 13
  • 21
  • 1
    this could be useful for you. http://docs.openstack.org/infra/jenkins-job-builder/definition.html#variable-references . passing a variable reference for that instance cane be helpful – DevD Sep 08 '15 at 07:37

1 Answers1

2

In the job template, you want to add a disabled property which takes its value from a parameter. Parameters default value can be set at the defaults and project level. So in theory you could pass the disabled value at the job level:

- defaults:
    name: global
    disabled: false

- job-template:
    name: 'build-{name}'
    defaults: global
    disabled: '{obj:disabled}'

- project:
    name: myproject
    jobs: 
      - 'build-{name}'

- project:
    name: mydeadproject
    jobs:
      - 'build-{name}':
          disabled: true

Note obj: prefix is to pass the boolean as is. Turns out there is an issue in JJB specific to the disabled parameter, so you need to use a different variable (I named it prevent_run below):

- defaults:
    name: global
    prevent_run: false

- job-template:
    name: 'build-{name}'
    defaults: global
    disabled: '{obj:prevent_run}'

- project:
    name: myproject
    jobs: 
      - 'build-{name}'

- project:
    name: mydeadproject
    jobs:
      - 'build-{name}':
          prevent_run: true

In the global default, the variable prevent_run is set to false and is passed to projects and job templates. For the last job, a more specific value is passed to the mydeadproject. The snippet above generates two jobs:

  • build-mydeadproject disabled
  • build-myproject enabled

You can reuse the same trick in your example template:

--- reference.yaml  2015-09-08 09:27:56.000000000 +0200
+++ foo.yaml    2015-09-08 10:02:38.000000000 +0200
@@ -1,11 +1,13 @@
 - defaults:
     name: global
     project-type: 'freestyle'
-    disabled: false
+    prevent_run: false
     buildTargets: 'build'
+
 - job-template:
     name: '{jobNameFirstSegment}-{jobNameSecondSegment}'
     defaults: global
+    disabled: '{obj:prevent_run}'
     builders:
       - ant:

You can then pass prevent_run: true on a per job basis:

- project:
    name: lorem
    jobNameFirstSegment:
      - foo:
          displayNameFirstSegment: 'Foo'
          urlGitRepository: 'git@bitbucket.org:mycompany/foo.git'
          prevent_run: true

That disable all jobs having foo for the value of jobNameFirstSegment. I. e. it disables the jobs:

  • foo-amet
  • foo-dolor
  • foo-ipsum
  • foo-lorem
  • foo-sit

When a project is realized, a cartesian products of the variables is created to generate the jobs. There is no way here to pass a parameter to a specific couple of values.