Jenkins Job Builder - one template with different params

61 views
Skip to first unread message

Mike Giles

unread,
Jul 20, 2017, 4:35:41 PM7/20/17
to Jenkins Users
About 48 hours new to yaml and JJB as we migrate from Hudson to Jenkins and have thousands of jobs to move (hence trying to find a better way to manage them).  In one case we have 4 sets of jobs (about 20 each) that have very similar options except for some of the params.  For the other jenkins options in these jobs, I am making variables under the job definitions and using them in the shell builder calls and in the job names and such.  I also have some defaults that are being used on a global level.

Currently I am implementing different templates for each set of jobs as the choice: params have different options per user.  I don't want to set these per job (even though I am not sure how to pass a nested option like that), as it would mean a lot of maintenance whenever I need to update them across a whole pack of jobs.  I want to make sure we can use JJB like the config slicing plugin on steroids... change in one place for a whole bunch of jobs.  So, basically everything but these dynamic items can be altered in one template that all the 80 jobs use.

I am thinking there has to be a way for me to define different choice: blocks and key them based on one of the params used in the job: definitions.  For example 

    jobs:
        - 'dev-deploy':
            type: dev
            product: productA
            deploy_user: "{dev_user}"

    jobs:
        - 'qa-deploy':
            type: qa
            product: productA
            deploy_user: "{qa_user}"

Now these are obviously using different templates (dev-deploy and qa-deploy).  The difference between the templates may be as small as email recipients and a choice: param like:

    parameters:
        - choice:
            name: DEPLOY_SERVER
            choices: ['hostname1', 'hostname2']

In the qa-deploy template it might look like:

    parameters:
        - choice:
            name: DEPLOY_SERVER
            choices: ['hostname3', 'hostname4']

I would like to do something like setup a default (which does not work like this but you get the idea):

- defaults:
    name: global
    dev_deploy_hosts: ['hostname1', 'hostname2']
    qa_deploy_hosts: ['hostname3', 'hostname4']

And then use those in a single template that all the jobs leverage, since the job names are parameterized.  Maybe a multi-level interpolation or something like:

    parameters:
       - choice:
           name: DEPLOY_SERVER
           choices: {{type}_deploy_hosts}

Where in the above type changes to dev or qa and then the value is replaced from globals by {dev_deploy_hosts} or {qa_deploy_hosts}

What am I missing?  I am sure it is either not possible or something simple I am missing.  Hoping it is the second.

Thanks.

P.S. Bonus points for an issue we also came across.  I can't seem to use variables in nested jenkins blocks.  For example this works:
    properties:
        - build-discarder:
            days-to-keep: -1
            num-to-keep: 15
            artifact-days-to-keep: -1
            artifact-num-to-keep: -1

But this does not when I set a defaults: of builds_to_keep: 10
    properties:
        - build-discarder:
            days-to-keep: -1
            num-to-keep: {builds_to_keep}
            artifact-days-to-keep: -1
            artifact-num-to-keep: -1


Mike Giles

unread,
Aug 4, 2017, 11:37:44 AM8/4/17
to Jenkins Users
I did get this one part working.  Turns out I just needed to quote the value in defaults and in the num-to-keep area.  

Still unable to figure out the templating of nested param values.
Reply all
Reply to author
Forward
0 new messages