ci/cd pipeline inside jenkins

How to Create CI/CD Pipeline Inside Jenkins ?

Jenkins is an automation tool providing continuous integration. It is open source and has been written in the Java Programming Language. It helps in the automation of software development parts that relate to build, test and deployment, and ensures constant delivery. What follows below is one aspect or you say the objective of the Jenkins Certification. Let us see how to create a CI/CD pipeline inside Jenkins.

Continuous Integration/Continuous Deployment is the backbone of DevOps. CI/CD fills the gap between development and operations team by automating the steps like build, test and deployment.

What is Jenkins Pipeline?

A Pipeline is a sequence of executable events or jobs. Jenkins itself does not perform any functionalities, it offers a way for other applications and tools to plug into Jenkins and execute the tasks.

The pipeline contains N number of stages and list of steps to be performed in each stage. A stage can be named as anything. A step is simply says what needs to be executed, and needs to be an executable command through Jenkins environment.

The Jenkins pipeline is typically given as a script called as a Jenkinsfile.

Here is an example of simple Jenkinsfile:
// Example Jenkins Pipeline Script
pipeline {
    stages {
        stage('build') {
            steps {
//Executable command as a step
         echo "This is an example jenkinsfile"
         sh 'mvn --version'
            }
        }
    }
}
//End of Pipeline

How to Build a Jenkins Pipeline?

Let us now see the steps to build Jenkins pipeline. Before starting, make sure you have Jenkins installed.

Open a web browser and navigate to Jenkins URL. In my case, it’s public IP address of amazon EC2 instance on port 8080.

Jenkins Start Page

Click on the “New Item” and define what kind of Jenkins job needs to be created. Select “Pipeline”, give it a name and click on ok.

Create jenkins pipeline Job

On the next page, you will see pipeline configurations steps. There are two possible ways to execute Jenkins pipelines.

  • Writing pipeline script directly on Jenkins
  • Use the Jenkinsfile from SCM (source code management)

Configure a Pipeline Through Direct Script

On the pipeline configuration page, scroll down until the pipeline section. Copy the below code and paste it. I have used 3 stages in this example script, Building, Testing and Deploying. I have just printing some messages in each stage and executable steps.

Sample Jenkins Pipeline:
//Sample script for configuring pipeline through direct script

pipeline {
    agent any
    stages {
       stage('Building') {
         steps {
           echo 'Building Stage Running...'
           echo "Running ${env.BUILD_ID} ${env.BUILD_DISPLAY_NAME} on ${env.NODE_NAME} and JOB ${env.JOB_NAME}"
         }
       }
       stage('Testing') {
         steps {
           echo 'Testing Stage Running...'
         }
       }
       stage('Deploying') {
         steps {
           echo 'Deploying Stage Running...'
         }
       }
   }
}

jenkins direct pipeline

Save the configurations and click on Build Now.

jenkins pipelines build

Click on Logs to view the output of our script.

pipeline logs

That’s it. We have successfully configured a pipeline through direct script.

Configure a Pipeline Through SCM

In this step, we will be using Jenkinsfile from the GitHub repository. Let us create a new job to perform this task.

jenkins pipeline scm

On the pipeline configuration page, select the option “Pipeline Script from SCM” and SCM as “Git” then paste the GitHub repository URL. Type Jenkinsfile in script path and save the changes.

Note: Make sure you have git installed on your system or on the virtual machine where you have installed Jenkins. In my case, I am using EC2 instance with Amazon Linux. I have installed git using the below command.

sudo yum install git

jenkins pipeline demo SCM

Click on save and click “Build Now” to start the pipeline.

Summary

Hope we were able to clear the concept and this article may help you with Jenkins certification preparation. Plus this article was an addition to your already existing and polished knowledge base on Jenkins. If you want to pass the Jenkins certification exam on your first attempt, then spend more time on your learning. Stay tuned. Let’s catch up with another article soon!

About Dharmalingam N

Dharmalingam.N holds a master degree in Business Administration and writes on a wide range of topics ranging from technology to business analysis. He has a background in Relationship Management. Some of the topics he has written about and that have been published include; project management, business analysis and customer engagement.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top