Jenkins pipeline tutorial : First step guide to Continuous Delivery
By: Kuldeep Bhatt
Tags: Jenkins, Ci Cd Pipeline, Gitlab, Github Webhook, Maven

Continuous delivery has been most important aspect of the software development life cycle now and with that Jenkins has been always the first choice of tool for the continuous delivery. Why Jenkins has gained so much of popularity and become one of the first row choice tool for the software community, one of the reason is Jenkins Pipeline. Jenkins pipeline is a continuous delivery pipeline that executes the software workflow as code.

To execute/practise this pipeline below are some pre-requisites.

Pre-Requisites

You should have GitLab installed in your local server with an account or you must be holding account in public Github.

Jenkins should be installed on your local machine or on any VM which you can use.

You should hold the account for Jenkins to login

You should have basic knowledge of Jenkins.

Highlights for this article :

What is Jenkins Pipeline ?

Web-hooks concepts

How to configure GitLab/Github to communicate with Jenkins with code change

Build your first pipeline

What is Jenkins Pipeline ?

A pipeline is a collection of jobs/steps which brings the software from version control to the hands of the end users of that software with the help of certain automation tools.

With every problem, there are always different ways or approaches for the solution. Same way Jenkins provides different ways to build pipelines to release the software solutions e.g. you can create pipeline as a simple job as well as series of job which you can incorporate together using Jenkins pipeline project. In this tutorial, we will use freestyle project and build it as pipeline.

Web-hooks concepts :

A Web-hook is a subscriber to certain events which communicates certain information between applications in real time. Unlike typical APIs where you would need to poll for data/information very frequently in order to get it real-time.

From top view, Web-hooks are one way through which apps can send automated messages to other apps. E.g. when you create a branch, JIRA marks the particular task In Progress state and when you merge that particular branch it marks the task in Done state and same messages gets communicated through Slack to the stack holders.

By design, they have a message — or payload — that are sent to a unique URL — essentially the app’s address. That’s what the mechanism which gets utilised here in case of Continuous Delivery. When you commit for particular change, GitLab/Github will inform that event to Jenkins to consider that change and start building with that change.

Now let’s see how can we configure the same in GitLab/Github.

How to configure GitLab/Github to communicate with Jenkins with code change :

Now, to set up GitLab or Github where it can communicate with Jenkins, we will utilise Web-hooks to implement it as a link between 2 applications. First one would be GitLab and the other one would be Jenkins. To do the same please follow below steps.

For GitLab :

Open & Login GitLab portal

Select your project repository (sample project you can fork)

Open Project Settings

Open Integrations

As per the snap below, please enter the URL of the Jenkins job (in our case it is : http://20.20.1.41:8080/project/MavenPipeline) and secret token (how to generate secret token from Jenkins for Web-hook — see next section Build a Gitlab project), select events on which you want the Web-hook to initiate the Jenkins job.

Maven Project Web-hook configuration

Once you save the above Web-hook configurations, you will find Test expandable button through which you can test particular Web-hook. Below is the snap of saved configurations.

Test Web-hook configurations

For Github :

Select project and open project settings

Open side menu and then Web-hooks

As payload URL — enter the URL of the Jenkins job url

Enter secret token — if available

Github Web-hook configuration

Build your first pipeline :

Step 1 : Login to Jenkins :

Login to Jenkins with specified credentials. If you’re logging into Jenkins for the first time then you will be prompted to install some default set of plugins, it may take a few mins and restart Jenkins.

Step 2 : Create Sample Project/Job :

There will be create new jobs link to get started, once you click on the link, it will prompt a page please enter the project/job name and select the project type. Please see below image.

Create Job

Step 3 : Build a GitLab Project :

You can use simple java + maven project by forking the ready project from here and add the same in your own git repository where you can modify it.

Now open Jenkins → MavenPipeline job and select SCM — Source Code Management section and select Git option to configure the same. As you can see you can define the branch as well for which you want to trigger the build while user commit on that branch.

Code Repository configuration in Jenkins

Please make sure you have configured the Git access credentials in Jenkins’s configure system’s credentials. If you have not yet added the same you can click on ‘Add’ as mentioned above and get it configured.

Next step is to set up authentication between 2 apps through secret token. To do the same, there are 2 steps process:

Generate the secret token, please follow below steps

Open the Jenkins job MavenPipeline

Expand Build Triggers

Select & expand web-hook option - GitLab web-hook URL: http://localhost:8080/project/job_name

Select Advanced options

Generate Secret token

Generate secret token from Jenkins

2. Integrate that secret token in GitLab project repository settings, please follow below steps to do the same

Open GitLab project repository settings

Integrations

Paste the Secret token here as mentioned in below snap

You need to follow the same steps for Github repository settings, but for Github, please select Web-hooks from side menu (as guided above) and paste the same secret token there.

Web-hook integrations — GitLab

Your pipeline is now all configured, what remains is just to test it and further check while you commit and push anything from either GitLab or Github should trigger the build in Jenkins.

Now when you commit some change in your repository, that will automatically kick off the build.

Jenkins job initialisation through Code commit

Cheers!!