Pages

Thursday, August 10, 2017

Understanding Jenkins Pipeline

One of the added features of Jenkins 2 is the introduction of Jenkins Pipeline. A Jenkins Pipeline allows you to define an entire application life cycle as code. This enable users to implement a project entire build/test/deploy pipeline in a Jenkinsfile which is stored alongside with code.

The default interaction model with Jenkins is by using the Web UI to manually create jobs, configure plugins and fill the necessary details. A manual effort is needed to create and manage jobs. Not just this but this model will keep the configuration of the Job to build/test/deploy separate from the actual code being build/test and deploy.

With the introduction of the “Pipeline” plugin, users now configure their entire project build/test/deploy phases in a Pipeline file called “Jenkinfile” and can be stored along side their original code. The pipeline file will be treated as a code committed into the Source control. Jenkins can read for the Jenkins file in the repositories and use that code to build/test and deploy code.

The Pipeline plugin was inspired by the Build flow plugin that is used to create build pipeline in previous versions of Jenkins. Some of the features of Jenkins pipeline are
1. Job can be suspended and resumed in between
2. Pipeline file can be committed to repository which can be checkout and executed as build flow

One of the Huge benefit of using a Pipeline is that the job itself is durable which will survive planned or even unplanned restarts of the Jenkins master.

Pipeline Vocabulary
Each Pipeline generally consists of 3 things: Stages, steps and Nodes

A Step can be taught as a “build Step” which is a single task that we want Jenkins to execute

A Node is a special step that schedules the contained steps to run by adding them to Jenkins Build Queue. Even better a node leverages Jenkins build system


Stages are for setting up logical divisions with in pipelines. The Pipeline Visualization plugin available in Jenkins will display each stage as a separate segment. By using stages we can define each phase with a name as “dev” ,”test” etc . A Stage will look like this

A sample Jenkinsfile looks as,

pipeline {
   agent {
        node {
                    label 'linux-slave' }
                    }
    }
         
   stages
     {
                stage("check-out")                     {
                                 steps {
                                  git 'git@github-isl-01.ca.com:manja17/funniest.git'                           
                                 }
            }
                                       
                stage('lint'){
                         steps {
                               echo "linting Success, Generating Report"
                                  }
                        }                                 
       }                
}

In the above snippet , we create a Pipeline script which is included in the Jenkinfile. The script performs the below steps
1.    Run the Job on the slave node by the name “linux-slave” thus using the Jenkins distributed system
2.    It has 2 stages “check-out” and “lint”. In the “check-out” we are checking out the code from the git hub repository. In the “lint” stage we are just running the echo command
A more extended version of Pipeline scripts can be written with doing all build/test and deploy phases
Using Scripting – Jenkins Pipeline supports executing shell or batch scripts just like free style jobs
stage('lint') {
                   steps {
                               sh 'sleep 10' # for Nix
                              bat 'timeout /t 10' # for Windows
                                  }
                        }   

Tool Installation – Jenkins Pipeline has the core capability of adding tools or installing them when ever necessary. We need to define the pipeline script as
def mvnHome = tool 'M3'
sh "${mvnHome}/bin/mvn -B verify"

Variables – The Env global variable allows accessing environment variables available on the node using
echo env.PATH

Integration with Plugins- The Pipeline file provides support to write plugin details in a declarative way.
steps {
                /* `make check` returns non-zero on test failures,
                * using `true` to allow the Pipeline to continue nonetheless
                */
                sh 'make check || true'
                junit '**/target/*.xml'
            }

In the above snippet we are trying to run the tests and use junit plugin to display the results
As another example we can run the test in python and publish the respot in HTML using the publishHTML plugin as

steps {
        sh "pytest --cov ./ --cov-report html --verbose"
        publishHTML(target:
                              [allowMissing: false,
                              alwaysLinkToLastBuild: false,
                              keepAll: false,
                              reportDir: 'htmlcov',
                              reportFiles: 'index.html',
                              reportName: 'Test Report',
                              reportTitles: ''])  
                             
                    echo "Testing Success"      
          }  

An Email can be send using the email plugin as
stage('mail'){
        steps{
            emailext attachLog: true, body: 'Jenkins Build - Status Report', subject: 'Build Report', to: 'jagadesh.manchala@gmail.com'
        }
    }  
Parallel execution – Jenkin Pipeline does have a built-in functionality for executing portions of Scripted Pipeline in parallel.

parallel 'task1': {}, 'task2': {}

Handling Approvals – Jenkins Workflow supports approvals, manual or automated through the input step as,

Input “Are you Sure ?”

Once ran the job, we can see a question during the checkout the code as
Waiting – Waiting feature is also added by using the “waitUntil” as
waitUntil {
    sh “input.sh”
   }

This will wait until the script input.sh is completed. This can be used during execution of the Automated test cases which generally would take some time

Since Jenkins Pipeline is based on Groovy scripting , most of its features are supported in Jenkins file like Exception handling, timing ,stashing etc. 

More  to Come,Happy Learning :-)

No comments :

Post a Comment