Jenkins

CI/CD using Jenkins Pipeline as code

Jenkins Pipeline

Pipeline annexes a strong set of automation tools onto Jenkins. It assists use cases covering from simple to comprehensive continuous integration/delivery pipelines. This blog will provide easy steps to implement CI/CD using Jenkins Pipeline as code. Furthermore it will integrate Jenkins, Github, SonarQube and JFrog Artifactory.

Job –> Pipeline

As Job is a defined process. Build is outcome of that process. Pipeline can be thought of as a series of jobs. It orchestrates various phases (e.g. build, test, deploy) involved in the life-cycle of a software. Above all, using Jenkins pipeline as code, entire process can be automated by writing scripts for each module.

Types of DSL syntax

Two types of DSL syntax are available: Scripted Pipeline and Declarative Pipeline. Groovy DSL is used to code scripted pipelines, while Declarative Pipeline provides predefined structures and models. It allows fast, stable & compact pipelines creation by users with or without learning Groovy.

3 ways to create pipeline

As enumerated by their website , there exists 3 ways to create pipeline:

  • Through Blue Ocean — after setting up a Pipeline project in Blue Ocean, the Blue Ocean UI helps to write Pipeline’s Jenkinsfile and commit it to source control.
  • Through the classic UI —  a basic Pipeline can be entred directly in Jenkins through the classic UI.
  • In SCM —  Jenkins can be written manually and then can be committed to project’s source control repository.

*Note : It is the best practice to define pipeline in Jenkinsfile and store it in source control (Github) along with the other code check-in. Hence allowing Jenkins to load it directly from SCM and execute the scripted stages.

Using Jenkinsfile has the following advantages (as stated by them):

a). Firstly, code review/iteration on the Pipeline

b). Secondly, audit trail for the Pipeline

c). Lastly, it act as a single source of truth for the Pipeline, therefore can be viewed and edited by multiple members of the project.

Enough with the background theory, let’s get start with the procedure step-by-step :

CI/CD using Jenkins Pipeline as code

Following are the steps employing declarative pipeline script to automate the Jenkins job:

Step 1. Start Jenkins :

a). If downloaded as a .zip file & installed by running jenkins.msi :

b). However, if downloaded as a jenkins.war file:

Go to command prompt. Browse to the directory containing jenkins.war. Run the following command:

java -jar jenkins.war

After Jenkins is fully up & running. Go to the default browser. Log in with the credentials

Step 2. Create & configure Jenkins Pipeline Project/Job:

a). Install the plugin from Manage Jenkins

Go to Jenkins Dashboard. Manage Jenkins. Manage Plugins. Available. search for ‘Build Pipeline’. Click on ‘Install without restart’

Jenkins plugin management
Plugin Installation : Jenkins

b). Go to Jenkins Dashboard. Click on “New Item”. Type the name of the project/job. Select pipeline. Click OK.

Jenkins pipeline job
Job creation : Jenkins

c). Edit the Project Configuration Window. You can provide the Description (though optional).

Jenkins project configuration page
Job configuration Window : Jenkins

Go to the Pipeline section :

  • Select “Pipeline script from SCM” from definition.
  • Select Git as SCM
  • Provide the URL of the github repository, where you have checked-in your source code, pom.xml and Jenkinsfile (Wait. Don’t panic!! we will create one in a bit).

Jenkins pipeline configuration
Configuration Window : Jenkins
  • Provide the script path, i.e. name of your Jenkinsfile (by default its name is Jenkinsfile, without any extension).
  • Click Apply & Save.
Step 3. Create a basic maven project (maven-archetype-webapp or maven-archetype-quickstart).
Step 4. Create a Jenkinsfile:

Go to the root directory of your maven project. Create a new text file with name: Jenkinsfile  and edit it with the following code snippet:

a). Create Artifactory Server Instance :

Make sure Artifactory Server is installed and configured (see here) in Jenkins under Manage Jenkins → Configure System

Configure System : Jenkins

*Provide the server-id same as set in the Jenkins Configuration System.

b). Create Artifactory Maven Build instance.

*Maven builds can resolve dependencies, deploy artifacts and publish build-info to Artifactory.

*Let’s define buildInfo Object too.

c). Start Declarative pipeline script (get basics here) with different stages.

Stage (i). ‘Clone sources’ : to tell Jenkins to checkout source code from a particular repository (github in this case).

 

Stage (ii). ‘SonarQube analysis’ :

Line no. 28 : The dedicated stage to run SonarQube analysis.

Line no. 31 : Since version 2.5 of the SonarQube scanner for Jenkins, there is an official support of Jenkins pipeline. They provide ‘withSonarQubeEnv’ block that allow to select the SonarQube server instance you want to interact with.

Line no. 32 : Running & configuring scanner — triggering SonarQube analysis on maven projects, with the help of sonar-maven-plugin, available in maven central repository.

Stage (iii). ‘Artifactory configuration’ : this is the stage responsible for uploading the artifacts to artifactory.

Maven Builds with Artifactory:

Maven builds can resolve dependencies, deploy artifacts  and can also publish build-info to Artifactory. To run Maven builds with artifactory from your pipeline script,

(1). Create Artifactory server instance (done in the beginning).

stage('Artifactory configuration'){

steps {

script {
rtMaven.tool ='Maven-3.5.3'

rtMaven.deployer releaseRepo: 'libs-release-local', 'libs-snapshot-local', server: server

rtMaven.resolver releaseRepo:'libs-release', snapshotRepo: 'libs-snapshot', server: server

rtMaven deployer.artifactDeploymentPatterns.addExclude("pom.xml")

buildInfo = Artifactory.newBuildInfo()

buildInfo.retention maxBuilds: 10, maxDays: 7, deleteBuildArtifacts: true

buildInfo.env.capture = true
}
}

(2) Create an Artifactory Maven Build instance, as well as define the location to deploy Maven build artifacts (jar, war, ear) into. Also, the location to download dependencies from.

* For instance, release dependencies to be resolved from the ‘libs-release’ repository & the snapshot dependencies from the ‘libs-snapshot’ repository.

*Though by default all the build artifacts are deployed to Artifactory, filter can also be applied based on their names, using the ‘addInclude’ & ‘addExclude’ method.

(3) We can also capture the enviornment variables and publish the build information in artifactory. Set build-Info object to automatically capture enviornment variables while downloading & uploadng files.

*By default enviornment variables, like ‘password’, ‘secret’, or ‘key’ are excluded & will not be published to Artifactory.

Stage (iv). Executing Maven goals

Stage (v). Publishing Build Information:

Step 5. Commit & push the Jenkinsfile in the source code repository specified in Stage (i), with the git (Use git add, git commit & git push command).
Step 6. Build Project/job:

Go to Jenkins dashboard. Select your project (“CD-pipeline”). Click on “Build Now”.

Pipeline Execution : Jenkins

*Note 1: Each stage defined in the script can be visualized in the stage view, along with the details (time spent & logs).

*Note 2: Moreover it is easy to visualize and track the build process. It helps to detect the point/stage where & which build failed.

*Note 3: Further, we can notify the developers regarding the failed build.

Congratulations !! You have finally automated your pipeline in Jenkins.

Please follow and like us: