Simple CI/CD Pipeline Using Jenkins

Reading Time: 5 minutes

In this blog we gone a see creating a CI/CD Pipeline using Jenkins.

Some pre-requisites to create a CI/CD pipeline using Jenkins are.

  • Open-JDK-8
  • Jenkins installed on our system

At the very first we see a little bit introduction of Jenkins and why we need Jenkins Pipeline.

Jenkins

Jenkins is an open-source automation tool to create a continuous integration pipeline. It is used to building and testing your software projects continuously making it easier for developers to integrate changes to the project, and making it easier for users to obtain a fresh build. It integrates all the software development lifecycle stages such as build, test, run and deploy. All that happens by running a single file called “Jenkinsfile”. Jenkinsfile is a file in which we define all the steps or we can say the definition of Jenkins pipeline is defined inside the Jenkinsfile and is checked into source control.

Why Jenkinse Pipeline

  • Jenkins pipeline allows you to define the complex workflow of your application in a single step.
  •  Its functionality helps Jenkins to support continuous delivery (CD). 
  • It can be pausable also. The pipeline can stop and wait for human approval before proceeding further to complete the task defined in the pipeline.
  • It also provides the feature of sending email based on our requirement i.e mail after build state: “stable” or mail after any build state.
  • Whenever the new code is committed to git. A pipeline can automatically integrate that code and start building pipelines again itself.

There are two type of pipeline you can create

  • Scripted-Pipeline
  • Declarative-Pipeline
  • Another option is through the classic-UI

In this blog we gone a see all about declarative pipeline and is syntactically easy than Scripted pipeline.

For your referance you can use my code with Jenkinsfile Here….

Steps to create Pipeline

  1. Go to Jenkins and login with your username and password you provided at the time installing Jenkins.

2. Click on New Item

3. “Enter an item name” and select “Pipeline” -> Click on ok.

4. A page opens. In this you will see the following blocks

General

1. You see a description field, in this field, you can provide a description of your build or whatever you want. You can keep it empty also.

Build Triggers

Select Poll SCM and specify ” * * * * *” in the space which means whenever new commit added to Github from that time exactly after one minute Jenkins pipeline build automatically and Continuously Deliver the code.

Pipeline

  1. Defination : Select pipeline script from SCM as our declarative jenkins pipeline is defined in a jenkinsfile which is present in our repository root.

2. Next in the source code section, you can see a git option, if there is no git option or you are using the bitbucket as an SCM you need to install the plugins first.

3. Go to Jenkins -> Manage Jenkinse ->Manage Plugin -> search for Bitbucket branch source or Github branch source plugin -> Click the checkbox present on the left side of the plugins -> Click on install without restart

4 . From SCM section select git -> Provide repository url

5. In case repo is public it’s fine but if the repo is private, you need to provide the credentials otherwise it gives an error.

5. Add Credentials.

6. If your code is on the master branch. No need to specify the branch name in the branch specifier section to keep master as it is. If you need to build another branch specify it there.

7. Click on to -> apply ->Save

Now your Jenkins pipeline configuration is done. Need to build your job.Click on build now. And you will see the following output as graphically with individuals steps.

When the sanity check is added person need to answer for wether to proceed or abort. If you select proceed then only following stage will get execute. If you select abort it will stop there.

If your pipeline executes successfully you get a blue boll if it fails you will get a red boll.

You can also see the console output as well . Seen in above picture click on the build number -> console output.

Started by user sakshi rajendra gawande
Obtained Jenkinsfile from git https://github.com/sakshigawande12/HelloWorld
Running in Durability level: MAX_SURVIVABILITY
[Pipeline] Start of Pipeline
[Pipeline] node
Running on Jenkins in /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Declarative: Checkout SCM)
[Pipeline] checkout
No credentials specified
Cloning the remote Git repository
Cloning repository https://github.com/sakshigawande12/HelloWorld
 > git init /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline # timeout=10
Fetching upstream changes from https://github.com/sakshigawande12/HelloWorld
 > git --version # timeout=10
 > git fetch --tags --progress -- https://github.com/sakshigawande12/HelloWorld +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/sakshigawande12/HelloWorld # timeout=10
 > git config --add remote.origin.fetch +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git config remote.origin.url https://github.com/sakshigawande12/HelloWorld # timeout=10
Fetching upstream changes from https://github.com/sakshigawande12/HelloWorld
 > git fetch --tags --progress -- https://github.com/sakshigawande12/HelloWorld +refs/heads/*:refs/remotes/origin/* # timeout=10
 > git rev-parse refs/remotes/origin/master^{commit} # timeout=10
 > git rev-parse refs/remotes/origin/origin/master^{commit} # timeout=10
Checking out Revision a99105209859cbdfd610d1ce8c380700e073cfa6 (refs/remotes/origin/master)
 > git config core.sparsecheckout # timeout=10
 > git checkout -f a99105209859cbdfd610d1ce8c380700e073cfa6 # timeout=10
Commit message: "Update Jenkinsfile"
First time build. Skipping changelog.
[Pipeline] }
[Pipeline] // stage
[Pipeline] withEnv
[Pipeline] {
[Pipeline] stage
[Pipeline] { (Compile)
[Pipeline] sh
+ sbt clean compile
[info] Loading project definition from /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/project
[info] Loading settings for project declaratice-ci-cd-pipeline from build.sbt ...
[info] Set current project to HelloWorld (in build file:/var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/)
[info] Executing in batch mode. For better performance use sbt's shell
[success] Total time: 0 s, completed 2 Apr, 2020 12:23:31 AM
[info] Compiling 1 Scala source to /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/target/scala-2.12/classes ...
[info] Done compiling.
[success] Total time: 5 s, completed 2 Apr, 2020 12:23:36 AM
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Run)
[Pipeline] echo
Running...
[Pipeline] sh
+ sbt run
[info] Loading project definition from /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/project
[info] Loading settings for project declaratice-ci-cd-pipeline from build.sbt ...
[info] Set current project to HelloWorld (in build file:/var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/)
[info] running HelloWorld 
hello
[success] Total time: 1 s, completed 2 Apr, 2020 12:23:47 AM
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Test)
[Pipeline] echo
Testing...
[Pipeline] sh
+ sbt test
[info] Loading project definition from /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/project
[info] Loading settings for project declaratice-ci-cd-pipeline from build.sbt ...
[info] Set current project to HelloWorld (in build file:/var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/)
[success] Total time: 1 s, completed 2 Apr, 2020 12:23:58 AM
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Package)
[Pipeline] echo
Packaging...
[Pipeline] sh
+ sbt package
[info] Loading project definition from /var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/project
[info] Loading settings for project declaratice-ci-cd-pipeline from build.sbt ...
[info] Set current project to HelloWorld (in build file:/var/lib/jenkins/workspace/Declaratice-CI-CD-Pipeline/)
[success] Total time: 0 s, completed 2 Apr, 2020 12:24:08 AM
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (sanity check)
[Pipeline] input
does the project  is ready to deploy ?
Proceed or Abort
Approved by sakshi rajendra gawande
[Pipeline] }
[Pipeline] // stage
[Pipeline] stage
[Pipeline] { (Build Status)
[Pipeline] echo
Here your pipeline is get successfully executed
[Pipeline] }
[Pipeline] // stage
[Pipeline] }
[Pipeline] // withEnv
[Pipeline] }
[Pipeline] // node
[Pipeline] End of Pipeline
Finished: SUCCESS

Knoldus-blog-footer-image