Tuesday, January 3, 2023

Understanding Pipelines- How to create a Jenkins Pipeline from Multiple FreeStyle Jobs

  

Understanding Pipelines- How to create a Jenkins Pipeline from Multiple FreeStyle Jobs

 (Before Starting this Pipeline go to https://github.com/murremway/jackpiro.git to clone the repository )


What is Pipeline?

A pipeline in a Software Engineering team is a set of automated processes that allow Developers and DevOps professionals to reliably and efficiently compile, build and deploy their code to their production compute platforms. There is no hard and fast rule stating what a pipeline should like like and the tools it must utilise, however the most common components of a pipeline are; build automation/continuous integration, test automation, and deployment automation

In Jenkins, a pipeline is a group of events or jobs which are interlinked with one another in a sequence

What is a JenkinsFile?

Jenkins pipelines can be defined using a text file called JenkinsFile. You can implement pipeline as code using JenkinsFile, and this can be defined by using a domain specific language (DSL). With JenkinsFile, you can write the steps needed for running a Jenkins pipeline.

The benefits of using JenkinsFile are:

  • You can create pipelines automatically for all branches and execute pull requests with just one JenkinsFile.
  • You can review your code on the pipeline
  • You can audit your Jenkins pipeline
  • This is the singular source for your pipeline and can be modified by multiple users.

JenkinsFile can be defined by either Web UI or with a JenkinsFile.

Declarative versus Scripted pipeline syntax:

There are two types of syntax used for defining your JenkinsFile.

  1. Declarative
  2. Scripted

Declarative:

Declarative pipeline syntax offers an easy way to create pipelines. It contains a predefined hierarchy to create Jenkins pipelines. It gives you the ability to control all aspects of a pipeline execution in a simple, straight-forward manner.

Scripted:

Scripted Jenkins pipeline runs on the Jenkins master with the help of a lightweight executor. It uses very few resources to translate the pipeline into atomic commands. Both declarative and scripted syntax are different from each other and are defined totally differently.

Why Use Jenkin's Pipeline?

Jenkins is an open continuous integration server which has the ability to support the automation of software development processes. You can create multiple automation jobs with the help of use cases, and run them as a Jenkins pipeline.

Here are the reasons why you use should use Jenkins pipeline:

  • Jenkins pipeline is implemented as a code which allows multiple users to edit and execute the pipeline process.
  • Pipelines are robust. So if your server undergoes an unforeseen restart, the pipeline will be automatically resumed.
  • You can pause the pipeline process and make it wait to resume until there is an input from the user.
  • Jenkins Pipelines support big projects. You can run multiple jobs, and even use pipelines in a loop.


Jenkins Pipeline Concepts

TermDescription
PipelineThe pipeline is a set of instructions given in the form of code for continuous delivery and consists of instructions needed for the entire build process. With pipeline, you can build, test, and deliver the application.
NodeThe machine on which Jenkins runs is called a node. A node block is mainly used in scripted pipeline syntax.
StageA stage block contains a series of steps in a pipeline. That is, the build, test, and deploy processes all come together in a stage. Generally, a stage block is used to visualize the Jenkins pipeline process.
StepA step is nothing but a single task that executes a specific process at a defined time. A pipeline involves a series of steps.
Create a Simple Pipeline 
Step 1. Create new job for code checkout: You can name it : CheckOut
Step 2. Click Advanced:
Click on Use custom workspace 
Select your workSpace. Enter:tmp



Step 3. Configure the SCM checkout by adding your Github Url and credentials 



Click Save.

Step 4.Create Another Job for the build stage:You can call it: Build


Step 5: Repeat Step 2
Step 6: Configure it with maven config as shown below: Then Save
Step 7. Create new Job to Archive artifacts: Name it:Archive_Artifacts 
Step 8: Repeat Step 2 
Step 9 : configure as below:   **/*.war

Step 10: Create a New job: Name it :Publish_To_Artifactory
Step 11: Repeat Step 2
Step 12 Configure as below:




Save
Step13: Create a New job: Code_Quality
Step 14: repeat Step 2
Step 15 Configure as below




Click Save
Step 16: Create a new job: Deploy_To_Container
Step 17: Repeat Step 2
Step 18: Configure As seen below:  
 **/*.war


Click Save
Step 19: Create A New Job: Slack_Notification
Step 20: Repeat Step 2
Step 21 Configure As Seen below
Step 22: Create A Pipeline Job:MyCI_CD_Pipeline
Step 23: Click Advanced Project Option
Enter A Project Name: Continuous Integration-Continuous Build-Continuous Deployment-Continuous Test-Continuous-Notification
Step 24: Copy and Paste Below code in Pipeline Script Box:


node {
 stage ('Checkout')  {

     build job: 'CheckOut' 
 }
stage ('Build') {
     build job: 'Build' 
    }
stage ('Code Quality scan') {
      build job: 'Code_Quality' 
        }
        
stage('Archive Artifacts') {
build job: 'Archive_Artifacts'
}
 stage('Publish to Artifactory') {
build job: 'Publish_To_Artifactory'
}
stage ('DEV Approve')
      {
            echo "Taking approval from DEV"
               
            timeout(time: 7, unit: 'DAYS') {
            input message: 'Do you want to deploy?', submitter: 'admin'
            }
     }
stage ('DEV Deploy')
         {
             build job: 'Deploy_To_Container'
          }

stage ('Slack notification') {
     
build job: 'Slack_Notification'
    }
  
}



Save
Build Now
Your Pipeline will build like below


The Above is an example of a scripted pipeline, it follows the structure below:

Scripted pipeline
node {
  stage('Build') {
       echo 'Building....'
  }
  stage('Test') {
      echo 'Building....'
  }
  stage('Deploy') {
      echo 'Deploying....'
  }
}
on the other hand, Declarative pipeline follows this:
// Declarative pipeline
pipeline {
  agent { label 'slave-node' }
  stages {
    stage('checkout') {
      steps {
        git 'https://bitbucket.org/myrepo''
      }
    }
    stage('build') {
      tools {
        gradle 'Maven3'
      }
      steps {
        sh 'mvn clean test'
      }
    }
  }
}

No comments:

Post a Comment

Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, deploy WARs using Jenkins Pipeline - Build pipelines integrate with github, Sonarqube, Slack, JaCoCo, Nexus, Tomcat

  Jenkins Scripted Pipeline - Create Jenkins Pipeline for Automating Builds, Code quality checks, Deployments to Tomcat - How to build, depl...