Search code examples
mavenjenkinsjenkins-pluginscloudbees

Jenkins Templates


I am new to the Cloudbees Enterprise of Jenkins and to the concept of "templates". I am trying to define a new template and this template will be used by 20-30 number of jobs. The job is a basic build job. After the build, I would like to have code analysis plugin. How can I define it in the Jenkins Template. I can define it while creating a direct job in "Post Build Actions" but not sure how to define the same in a template.

Do you have any solutions/ suggestions ?


Solution

  • Cloudbees templates plugin is very powerful but not easy to master. Creation and administration of templates is not as user-friendly as one wishes it to be. ;)

    For an introduction I recommend reading the following resources:

    Make sure you understand the difference between builder template and job template. I assume you want to create a number of jobs using the job template. Follow these steps:

    1. First of all create a normal job that contains all actions you want to perform by all templated jobs.
    2. Make sure this job works as expected for one example configuration.
    3. Now create a new job template:
      • You will need to decide, which parts of the job configuration need to be adapted to each jobs configuration (e.g. source code repository). Create an parameter for each configuration option.
      • You might want to do some pre-processing on the job templates parameters using some transformation script - but we skip that for now.
      • Now you need to add an XML description of what the generated job should do. I recommend copying this XML description from our example job created in step #1. You can access it via this URL: http://your-jenkins/job/this-job/config.xml. Simply copy&paste the XML code in the browser. Newer Jenkins versions also allow to read the jobs XML configuration via the user interface.
      • Finally you need to fill in the templates arguments within the XML configuration. Simply replace the specific (hard-coded) values by a reference to the name of the templates parameters created before: ${param_name}
      • Save the template
    4. Now create a new job. On the job creation screen you should be able to select your newly created job template as job type. After creating a job of the templates type you can define all template parameters for this specific job.
    5. Try to run the template-based job and make sure it works as expected.
    6. Create more template-based jobs as needed.

    All template-based jobs share the build steps defined by the job template. If you change the job template later on, all depending jobs are updated accordingly. This is a very efficient way to administrate a large number of similar jobs. It is very much worth the effort. Good luck!