jenkinsyamljenkins-job-builder

Add extra parameters at project or inherited job-template level


I'm looking for a way to allow some instances of a jjb job-template to add additional parameters, or add them via an inherited child template.

The same thing really applies to any array/list/sequence-valued key. Instead of overriding the whole key, I want to append to it. Possibly some 'n' levels deep, needing something like YAML anchors and merge-keys.

So either:

I want a working version of this:

- job-template: &base-template
    name: base-params
    parameters:
       - string:
           name: foo
           default: FOO
           description: Foofoo

- project:
    name: thing
    jobs:
       - more-params
         parameters:
           - !APPEND          # This is wrong
           - string:
               name: bar
               default: BAR
               description: Baaaa

or this:

- job-template: &base-template
    name: base-params
    parameters:
       - string:
           name: foo
           default: FOO
           description: Foofoo

- job-template:
    name: more-params
    << : *base-template
    parameters:
      - !APPEND             # This is wrong
      - string:
           name: bar
           default: BAR
           description: Baaaa

- project:
    name: thing
    jobs:
       - more-params

or this:

- job-template: &base-template
    name: base-params
    parameters: &base-template-parameters
       - string:
           name: foo
           default: FOO
           description: Foofoo

- job-template:
    name: more-params
    << : *base-template
    parameters:
      - << &base-template-parameters     # THIS IS WRONG
      - string:
           name: bar
           default: BAR
           description: Baaaa

- project:
    name: thing
    jobs:
       - more-params

I looked at making a child job-template with YAML inheritance, then using that in the project. But that doesn't look like it'll work because yaml doesn't have a way to extend/append sequences/lists. Merge keys don't work for lists and the yaml spec doesn't plan on accepting anything like them; in fact, merge keys are being gently deprecated.

JJB itself doesn't appear to offer a way to say "this job-template extends this other job-template, and you should merge the parameters: lists". It relies on YAML inheritance, implemented in JJB itself not the YAML reader, but doesn't have a (findable/documented) list equivalent.

I suspect it may be possible with !j2 Jinja2 tags, but I'm not at all sure how, or if it's sensible to go that way.

Surely this is a common need? I had similar issues with Ansible some time ago.

The only way I found to do it so far is with snippets in include files, which is ugly as hell, like (untested)

# File base-template-params.yml.inc
- string:
    name: foo
    default: FOO
    description: Foofoo

# File templates.yml
- job-template: &base-template
    name: base-params
    parameters:
        !include base-template-params.yml.inc


- job-template:
    name: more-params
    << : *base-template
    parameters:
      !include base-template-params.yml.inc
      - string:
           name: bar
           default: BAR
           description: Baaaa

- project:
    name: thing
    jobs:
       - more-params

Solution

  • Unfortunately you are right, JJB does not facilitate the request being asked here. The closest you can get with current JJB is to use the "obj:" key when declaring a variable {obj:variable_name_must_use_underscores}

    - job-template:
        name: base-params
    
        ######################
        # Default parameters #
        ######################
    
        job_params:
    
        #####################
        # Job configuration #
        #####################
    
        parameters: '{obj:job_params}'
    
    - project:
        name: thing
        jobs:
          - base-params
        job_params:
          - string:
              name: foo
              default: FOO
              description: Foofoo
           - string:
               name: bar
               default: BAR
               description: Baaaa
    

    What this does is sets a default job_params in the job-template that will be applied to all users of the job-template. It optionally provides the ability to override the default job_params but as you can see from the example, unfortunately the user needs to redefine the default job_params if they want to append new params along with it.

    Current JJB 2.x does not support a way to inherit and extend existing template configurations, if you override you will need to override the entire section.