Jenkins

When your Jenkins landscape grows with multiple pipeline jobs and projects, the maintenance of the same will become a burden for the engineers. To mitigate such scenarios, Jenkins Job Builder really comes in handy. Jenkins Job Builder takes simple descriptions of Jenkins jobs in YAML or JSON format and uses them to configure Jenkins. You can keep your job descriptions in human-readable text format in a version control system to make changes and auditing easier.

In the article, I will walk through a simple Jenkins Pipelines Template and procedures for the setup. The following are the min requirements for the Jenkins Job Builder.

1. Python 3 and Pip 3
2. Jenkins API Token

Environment Setup

I am using an Ubuntu machine for the setup. The instructions mentioned aligns with this particular Linux distro. If you are using RHEL or CoreOS, the commands would differ a bit. As a first step, we need to install Pip 3. The latest Linux distros out there will have Python 3 installed by default and the step is to install pip3. The next step is to install the Jenkins Job Builder package.

sudo apt install python3-pip
sudo pip3 install --user jenkins-job-builder

Once we installed the package, let's retrieve the API token from Jenkins. This is required for programmatically accessing the Jenkins functions using API calls. I am using a token generated by an admin account. You may create the token using the below link. Replace JenkinsInstance and UserID with yours.

https://JenkinsInstance/user/ UserID/configure

Let’s Automate

The first thing we have to do is to create a config ini file that can be used along with Job Builder CLI. The token and Jenkins URL are added to the file.

Secondly we will create a YAML template file that tells the JBB CLI to create Project, Job Group & Job based on a Jenkinsfile located in a git repository. For illustration purposes, I have marked the sequence of execution in which YAML is processed by the JBB CLI.

  1. The project template is processed, which internally calls the Job group
  2. The Job group contains two jobs, that need to be created. Job group is required when you have multiple Jenkins pipelines to be created and it is achieved via Job Template
  3. The Job Template contains the definition of the jobs. In this example, we are creating two different jobs. The project type is mentioned as a pipeline. Each job needs to embedded inside a Job Template
    3.1 scm template is invoked for the job. This one points to a git repository and it behaves likes a common function
  4. The second job inside Job Template will be processed.
    4.1 scm template invoked for the second job

Lastly, we need to execute with Jenkins Job Builder CLI

jenkins-jobs --conf config.ini update jjb-pipeline.yaml

This will result in two jobs created with specified configuration in the YAML file. There are other parameters available with Jenkins Job Builder CLI for other CRUD operations.

That’s all for now. Please feel free to try out and let me know your thoughts on this folks.

References
Job Builder
GitHub

Join FAUN: Website 💻|Podcast 🎙️|Twitter 🐦|Facebook 👥|Instagram 📷|Facebook Group 🗣️|Linkedin Group 💬| Slack 📱|Cloud Native News 📰|More.

If this post was helpful, please click the clap 👏 button below a few times to show your support for the author 👇


Automate Jenkins Pipelines management was originally published in FAUN on Medium, where people are continuing the conversation by highlighting and responding to this story.