Constructing CI/CD pipelines with Jenkins

In my article A newbie’s information to constructing DevOps pipelines with open supply instruments, I shared a narrative about constructing a DevOps pipeline from scratch. The core expertise driving that initiative was Jenkins, an open supply software to construct steady integration and steady supply (CI/CD) pipelines.

At Citi, there was a separate staff that supplied devoted Jenkins pipelines with a secure master-slave node setup, however the atmosphere was solely used for high quality assurance (QA), staging, and manufacturing environments. The event atmosphere was nonetheless very guide, and our staff wanted to automate it to achieve as a lot flexibility as attainable whereas accelerating the event effort. That is the explanation we determined to construct a CI/CD pipeline for DevOps. And the open supply model of Jenkins was the plain alternative attributable to its flexibility, openness, highly effective plugin-capabilities, and ease of use.

On this article, I’ll share a step-by-step walkthrough on how one can construct a CI/CD pipeline utilizing Jenkins.

What’s a pipeline?

Earlier than leaping into the tutorial, it is useful to know one thing about CI/CD pipelines.

To start out, it’s useful to know that Jenkins itself shouldn’t be a pipeline. Simply creating a brand new Jenkins job doesn’t assemble a pipeline. Take into consideration Jenkins like a distant management—it is the place you click on a button. What occurs while you do click on a button depends upon what the distant is constructed to manage. Jenkins presents a method for different utility APIs, software program libraries, construct instruments, and so on. to plug into Jenkins, and it executes and automates the duties. By itself, Jenkins doesn’t carry out any performance however will get increasingly highly effective as different instruments are plugged into it.

A pipeline is a separate idea that refers back to the teams of occasions or jobs which can be linked collectively in a sequence:

A pipeline is a sequence of occasions or jobs that may be executed.

The best method to perceive a pipeline is to visualise a sequence of levels, like this:

Right here, it is best to see two acquainted ideas: Stage and Step.

  • Stage: A block that incorporates a sequence of steps. A stage block will be named something; it’s used to visualise the pipeline course of.
  • Step: A process that claims what to do. Steps are outlined inside a stage block.

Within the instance diagram above, Stage 1 will be named “Construct,” “Collect Data,” or no matter, and the same thought is utilized for the opposite stage blocks. “Step” merely says what to execute, and this could be a easy print command (e.g., echo “Hey, World”), a program-execution command (e.g., java HelloWorld), a shell-execution command (e.g., chmod 755 Hey), or another command—so long as it’s acknowledged as an executable command by the Jenkins atmosphere.

The Jenkins pipeline is supplied as a codified script usually referred to as a Jenkinsfile, though the file title will be completely different. Right here is an instance of a easy Jenkins pipeline file.

// Instance of Jenkins pipeline script

pipeline {
levels {
stage(“Construct”) {
steps {
// Simply print a Hey, Pipeline to the console
echo “Hey, Pipeline!”
// Compile a Java file. This requires JDKconfiguration from Jenkins
// Execute the compiled Java binary referred to as HelloWorld. This requires JDK configuration from Jenkins
java HelloWorld
// Executes the Apache Maven instructions, clear then bundle. This requires Apache Maven configuration from Jenkins
mvn clear bundle ./HelloPackage
// Checklist the recordsdata in present listing path by executing a default shell command
sh “ls -ltr”
// And subsequent levels if you wish to outline additional…
} // Finish of levels
} // Finish of pipeline

It is easy to see the construction of a Jenkins pipeline from this pattern script. Notice that some instructions, like java, javac, and mvn, are usually not accessible by default, and so they have to be put in and configured by Jenkins. Due to this fact:

A Jenkins pipeline is the best way to execute a Jenkins job sequentially in an outlined method by codifying it and structuring it inside a number of blocks that may embrace a number of steps containing duties.

OK. Now that you simply perceive what a Jenkins pipeline is, I will present you the way to create and execute a Jenkins pipeline. On the finish of the tutorial, you should have constructed a Jenkins pipeline like this:

Easy methods to construct a Jenkins pipeline

To make this tutorial simpler to observe, I created a pattern GitHub repository and a video tutorial.

Earlier than beginning this tutorial, you will want:

  • Java Growth Package: Should you do not have already got it, set up a JDK and add it to the atmosphere path so a Java command (like java jar) will be executed by a terminal. That is essential to leverage the Java Net Archive (WAR) model of Jenkins that’s used on this tutorial (though you need to use another distribution).
  • Fundamental pc operations: It’s best to know the way to kind some code, execute primary Linux instructions by the shell, and open a browser.

Let’s get began.

Step 1: Obtain Jenkins

Navigate to the Jenkins obtain web page. Scroll all the way down to Generic Java bundle (.conflict) and click on on it to obtain the file; reserve it someplace the place you possibly can find it simply. (Should you select one other Jenkins distribution, the remainder of tutorial steps must be just about the identical, apart from Step 2.) The rationale to make use of the WAR file is that it’s a one-time executable file that’s simply executable and detachable.

Step 2: Execute Jenkins as a Java binary

Open a terminal window and enter the listing the place you downloaded Jenkins with cd <your path>. (Earlier than you proceed, ensure JDK is put in and added to the atmosphere path.) Execute the next command, which can run the WAR file as an executable binary:

java -jar ./jenkins.conflict

If all the things goes easily, Jenkins must be up and operating on the default port 8080.

Step 3: Create a brand new Jenkins job

Open an online browser and navigate to localhost:8080. Except you may have a earlier Jenkins set up, it ought to go straight to the Jenkins dashboard. Click on Create New Jobs. You can too click on New Merchandise on the left.

Step 4: Create a pipeline job

On this step, you possibly can choose and outline what kind of Jenkins job you need to create. Choose Pipeline and provides it a reputation (e.g., TestPipeline). Click on OK to create a pipeline job.

You will notice a Jenkins job configuration web page. Scroll down to seek out Pipeline part. There are two methods to execute a Jenkins pipeline. A technique is by straight writing a pipeline script on Jenkins, and the opposite method is by retrieving the Jenkins file from SCM (supply management administration). We are going to undergo each methods within the subsequent two steps.

Step 5: Configure and execute a pipeline job by a direct script

To execute the pipeline with a direct script, start by copying the contents of the pattern Jenkinsfile from GitHub. Select Pipeline script because the Vacation spot and paste the Jenkinsfile contents in Script. Spend a little bit time finding out how the Jenkins file is structured. Discover that there are three Phases: Construct, Take a look at, and Deploy, that are arbitrary and will be something. Inside every Stage, there are Steps; on this instance, they only print some random messages.

Click on Save to maintain the modifications, and it ought to routinely take you again to the Job Overview.

To start out the method to construct the pipeline, click on Construct Now. If all the things works, you will notice your first pipeline (just like the one under).

To see the output from the pipeline script construct, click on any of the Phases and click on Log. You will notice a message like this.

Step 6: Configure and execute a pipeline job with SCM

Now, swap gears: On this step, you’ll Deploy the identical Jenkins job by copying the Jenkinsfile from a source-controlled GitHub. In the identical GitHub repository, choose up the repository URL by clicking Clone or obtain and copying its URL.

Click on Configure to switch the prevailing job. Scroll to the Superior Undertaking Choices setting, however this time, choose the Pipeline script from SCM choice within the Vacation spot dropdown. Paste the GitHub repo’s URL within the Repository URL, and sort Jenkinsfile within the Script Path. Save by clicking the Save button.

To construct the pipeline, as soon as you might be again to the Process Overview web page, click on Construct Now to execute the job once more. The outcome would be the identical as earlier than, besides you may have one extra stage referred to as Declaration: Checkout SCM.

To see the pipeline’s output from the SCM construct, click on the Stage and examine the Log to examine how the supply management cloning course of went.

Do greater than print messages

Congratulations! You have constructed your first Jenkins pipeline!

“However wait,” you say, “that is very restricted. I can’t actually do something with it besides print dummy messages.” That’s OK. Thus far, this tutorial supplied only a glimpse of what a Jenkins pipeline can do, however you possibly can lengthen its capabilities by integrating it with different instruments. Listed here are a couple of concepts to your subsequent challenge:

  • Construct a multi-staged Java construct pipeline that takes from the phases of pulling dependencies from JAR repositories like Nexus or Artifactory, compiling Java codes, operating the unit assessments, packaging right into a JAR/WAR file, and deploying to a cloud server.
  • Implement the superior code testing dashboard that may report again the well being of the challenge based mostly on the unit take a look at, load take a look at, and automatic person interface take a look at with Selenium.
  • Assemble a multi-pipeline or multi-user pipeline automating the duties of executing Ansible playbooks whereas permitting for approved customers to reply to process in progress.
  • Design an entire end-to-end DevOps pipeline that pulls the infrastructure useful resource recordsdata and configuration recordsdata saved in SCM like GitHub and executing the scripts by numerous runtime packages.

Observe any of the tutorials on the finish of this text to get into these extra superior circumstances.

Handle Jenkins

From the primary Jenkins dashboard, click on Handle Jenkins.

World software configuration

There are a lot of choices accessible, together with managing plugins, viewing the system log, and so on. Click on World Instrument Configuration.

Add extra capabilities

Right here, you possibly can add the JDK path, Git, Gradle, and a lot extra. After you configure a software, it’s only a matter of including the command into your Jenkinsfile or executing it by your Jenkins script.

The place to go from right here?

This text put you in your method to making a CI/CD pipeline utilizing Jenkins, a cool open supply software. To seek out out about lots of the different issues you are able to do with Jenkins, try these different articles on

You could be thinking about a number of the different articles I’ve written to complement your open supply journey:


Germany Devoted Server

Leave a Reply