CI/CD pipeline with Jenkins

Hi everyone, in this blog I will be focusing on making a build pipeline in Jenkins.

“There should be two tasks for a human being to perform to deploy software into a development, test, or production environment: to pick the version and environment and to press the “deploy” button.”- David Farley

Prerequisite- Docker, Jenkins basics.

Before starting let us know what Jenkins is?

Jenkins is basically an open source automation server which can help in reducing the efforts of Integration and Deployment in a software development life cycle. It is written in Java. It reduces the gap and establish a link between the developers and operations team by automating the building, testing and deployment of applications.

I hope now you understood what Jenkins is and why we use it. So, now let us move on to making a Jenkins pipeline for building and deploying a Java application. Jenkins pipeline is a group of tasks or event which are interlinked to each other which means if any of the task in a pipeline fails then next task in a pipeline will not be executed and overall build will be failed. For the build to be successful, each event or task needs to be executed successfully. For making a pipeline we can either make use of Jenkins file or can configure it inside projects settings inside Jenkins.

Before staring, I am assuming you have installed Jenkins on your machine. You need following plugins installed in your Jenkins:

  1. Credentials
  2. Credentials Binding Plugin- To get Credentials in you Jenkins file.
  3. Delivery Pipeline Plugin
  4. Pipeline Plugin
  5. Docker Plugin-If you are building docker image.
  6. GitHub Plugin-If you are pushing your code via GitHub

To Install a plugin, follow the below steps:

  1. Click on Manage Jenkins.

2. Select Manage Plugins.

3. Search the require plugin and select that from the list and click on Install without restart.You can also choose Download now and Install after restart.

You also need to set credentials inside Jenkins if you are using GitHub and docker. Steps for the same are given below:

  1. Click on Credentials in Jenkins.

2. Click on Jenkins.

3. Click on Global credentials (unrestricted).

4. Click on Add Credentials.

5. Select Kind as Username with password.You can choose other Kind also depending upon your requirement.

6.Enter ID and note it down as it will be used later on and click on OK.

Now, you have successfully installed all the required plugins and set the credentials. Time to make a Jenkins pipeline.I will not be covering docker in this.

  1. Click on New Item.

2. Select Pipeline from the list and enter item name and click OK.You will get Pipeline as option if you have installed pipeline plugin.

3. After making an item, open the item and click on configure.

4. After that go to Pipeline and select Pipeline Script if you want to write script inside Jenkins otherwise you can select Pipeline script from SCM. In latter case you need to make a Jenkins file inside your project and write your script inside it and then you need to give path of Jenkins file inside Script Path option which you will get when you will select Pipeline script from SCM.

5. Below code is used to make Jenkins pipeline. Paste it inside your script space or Jenkins file. I have used declarative pipeline syntax here.

pipeline {
agent anytools {
// Install the Maven version configured as "M3" and add it to the path.
maven 'Maven'
jdk 'JDK'
}stages {

stage('Pull Code') {
steps {
// Get code from a GitHub repository
echo "Pulling Code"
git 'https://github.com/gdgagan696/Spring-OAuth2.0-Login-With-Facebook.git'
}
} stage('Build Code'){
steps{
echo "Bulding Code"
echo "M2_HOME = ${M2_HOME}"
dir("OAuth2.0"){
bat 'mvn clean package'
}
}
} stage('Junit Test'){
steps{
echo "Test Code"
dir("OAuth2.0"){
bat 'mvn test'
}
}
} stage('Build Docker Image')
{
steps{
dir("OAuth2.0"){
echo "Building Docker Image"
bat 'docker build -t oauth2 -f Dockerfile --no-cache .'
}
}
}
stage('Tag Docker Image'){
steps{
echo "Tagging Docker Image"
bat "docker tag oauth2 registry.heroku.com/onlinecourseportal/web:${env.BUILD_NUMBER}"
}
}

stage('Docker Login')
{
steps{
withCredentials([usernamePassword(credentialsId:'Heroku',usernameVariable:'USR',passwordVariable:'PWD')])
{
echo "Docker Logging In"
bat "docker login registry.heroku.com -u ${env.USR} -p ${env.PWD}"
}

}
}
stage ('Push Docker Image')
{
steps{
echo "Pushing Docker Image"
bat "docker push registry.heroku.com/onlinecourseportal/web:${env.BUILD_NUMBER}"
}
}

stage ('Deploy Docker Image')
{
steps{
withEnv(['HEROKU=C:\\Progra~1\\heroku\\bin']){
withCredentials([usernamePassword(credentialsId:'Heroku',usernameVariable:'USR',passwordVariable:'PWD')])
{
echo "Docker Logging In"
bat "docker login registry.heroku.com -u ${env.USR} -p ${env.PWD}"
}
echo "Deploying Docker Image"
bat "$HEROKU\\heroku container:release web --app=onlinecourseportal"
}
}
}
}post {
success {
echo "BUILD SUCCESS"
}
}
}

Terminologies used in the above code is explained below:

  • tools command is used to mention all the tools you will used inside your Jenkins. As I am using JDK and maven, so I have mentioned it here. You must mention path of the tools also in Global Tool Configuration.
  • All the stages of the pipeline go inside stages commands.
  • Individual stage is made using stage command. Stage here refers to the task or event which we discussed above.
  • Commands inside post will run post Jenkins build.
  • Commands inside success will run only if build is success. There are also other options like like failure and always.
  • steps command basically the process followed by each stage. It is used inside each stage.
  • echo command is used to display something on console.
  • dir command is used to set the directory of the project.
  • git command is used to fetch and pull the code from the GitHub.

Stages used are explained below:

  1. stage (‘Pull Code’) is fetching and pulling the code from GitHub repository as my repo is public.
  2. stage (‘Build Code’) is building & integrating all code. I have used bat as I am on windows. You can use sh command if you are on Linux.
  3. stage (‘Junit Test’) is running all the Junit Test cases.
  4. stage (‘Build Docker Image’) is building the docker Image by using the dockerfile kept inside the project.
  5. stage (‘Tag Docker Image’) is tagging the docker image. ${env.BUILD_NUMBER} is used to get the current build number.
  6. stage (‘Docker Login’) is Logging into docker hub. For that you need to have account on dockerhub. WithCredentials is used to fetch the username and password from the credentials inside Jenkins with the help of Id which you set inside credentials and set inside usernameVariable and passwordVariable. Reminder I told you to note down the ID while setting up credentials. Also, you need to have Credentials plugins installed for this. Here, I am logging into Heroku docker hub.
  7. stage (‘Push Docker Image’) is pushing the image to dockerhub or heroku. Here I am pushing the image to Heroku as I am using Heroku for deployment.
  8. stage (‘Deploy Docker Image’) is deploying the image on Heroku. You can use any other cloud. WithEnv is used to set the path of heroku as I am using heroku command, So, I need to set the path of heroku also.

Hurray!! your Jenkins pipeline is ready to run.

Click on Build after opening the Project/Item. It will run successfully. You can see my output.

Thanks for reading this.

In this blog, we got to know about making a Jenkins pipeline.

Lead Fullstack Software Developer @ Micron Semicon (Asia Pte Ltd) Singapore

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store