Hello Readers! In this blog we will see how we can create a Jenkins job using Jenkins job Builder. Firstly let me introduce you to Jenkins job Builder. What is it? How can we get start using Jenkins Job Builder? How can we set it up?
Jenkins Job Builder:
As the name suggests builder, i.e used for building and managing things. So, Basically Jenkins Job Builder is used for maintaining configurations of jenkins jobs. And for configuration we use config files. Jenkins stores configuration inside config.xml. Jenkins Job Builder takes simple descriptions of Jenkins jobs in YAML or JSON format and uses them to configure Jenkins. These files are in human readable format.
Installation and Setup:
We will install Jenkins Job Builder using pip. Make sure pip is installed in your system. If not use this command:
$ sudo apt install pip
Use the following command for installing jenkins job builder:
$ pip install --user jenkins-job-builder
To check its version use the following command:
$ pip freeze | grep jenkins-job-builder
So, it is now successfully installed. Now we will configure Jenkins job builder. Move inside your /etc/jenkins_jobs/jenkins_jobs.ini and add your jenkins API token, username and url.
$ cd /etc/jenkins_jobs
$ nano jenkins_jobs.ini
Let’s get started!
Creating a Jenkins Job using JJB:
For creating a job using Jenkins job builder we need a config.yaml file inside which we will use in configuring our job. So, here is my test1.yml file:
- job:
name: my_job
project-type: pipeline
sandbox: true
dsl: |
pipeline {
agent any
environment{
cred=credentials('dockerhub')
}
stages {
stage("Checkout") {
steps {
git(
url: 'https://github.com/NaincyKumariKnoldus/node-hello',
branch: "main"
)
}
}
stage("Build docker image") {
steps {
sh "docker build -t image:01 ."
}
}
}
}
The first line indicates the name of the job i.e my_job. The job I am creating is a pipeline. The purpose of the Jenkins sandbox is to allow projects to test their JJB setups before merging. And inside dsl I am writing my pipeline script. There are two stages one is for my github url and one is for building the docker image.
So, By using the update command we can easily create our jenkins job. Let’s create the job using the following command:
$ jenkins-jobs update test1.yml
The output will be like this:
Move to your Jenkins and see your new job. Its created for me as you can see here:
JJB creates a Jenkins XML configuration file from a YAML/JSON file and uploads it to Jenkins for creating Jenkins jobs. When you open that pipeline you will find all the configurations there. These are my configurations that I have mentioned in my test1.yml file.
Your pipeline is now ready for building.
Jenkins internally stores its config in XML format. So, for getting the config.xml from the yaml file in the console use the test command:
$ jenkins-jobs test test1.yml
It will validate your yaml file and will print xml in your console. You can also find your config.xml for any job inside /var/lib/jenkins/jobs.
Here is where jenkins used to store its configurations.
Let’s try a simple example of creating a freestyle job in Jenkins. This is my test.yml for the freestyle job:
- job:
scm:
- freestyle-Job
builders:
- shell: |
TIME=`date`
echo "It is ${TIME}"
parameters:
- string:
name: BRANCH
default: '{branch|main}'
name: freestyle-job
project-type: freestyle
- scm:
name: freestyle-Job
scm:
- git:
url: https://github.com/NaincyKumariKnoldus/node-hello
branches:
- main
Here the project-type is freestyle and I am giving here a parameter of string type. Inside the execute shell I am printing the date and time. And Inside scm I am giving my github project url.
Let’s create it by using this command:
$ jenkins-jobs update test.yml
It’s created now. You can see below my new jenkins freestyle job.
You can also see my configurations:
My console output after building looks like:
We are done now for creating a jenkins job.
Deleting a Jenkins job:
Use the following command for deleting a jenkins job:
$ jenkins-jobs --conf ./jenkins_jobs.ini delete jobs/simple-job.yaml
Conclusion
Thank you for sticking to the end. In this blog we have seen what jenkins job builder is. It’s setup and how we can create and delete a job using jenkins job builder. If you like this blog, please do show your appreciation by giving thumbs ups and share this blog and give me suggestions on how I can improve my future posts to suit your needs.
HAPPY LEARNING!