Jenkins Pipeline

From v2 of Jenkins there is a support for jerkin pipeline where we can create jerkin files for our pipe line and store in the SCM. Pipeline are written in groovy language.

Pipeline builds have several major advantages over usual “freestyle” projects:

● Resilience - Pipeline builds can “survive” Jenkins restarts.

● Pausable / Parameterizable - Pipeline builds can be stopped at any stage while waiting for user input. There is also a possibility to control Pipeline jobs execution through Jenkins API.

● Power and Flexibility - Pipeline builds can have complex logic, conditional actions, be executed on multiple agents in parallel, integrate with other jobs, etc. Pipelines are defined using Groovy-based DSL so Jenkins and Java APIs can be used to define the job.

● VCS Friendliness - As a Groovy script, Pipeline definition code can be put under a version control system. It can even be loaded from there on the fly during the Jenkins job execution, which is great for import/export/changes tracking and experimenting.

There are 2 methods in creating jenkinfile

1. Scripting

node{

stage ('Build'){

//...

}

stage ('Test'){

//...

}

}

2. Declarative

pipeline{

agent any

stages {

stage('Build') {

steps {

//...

}

}

stage('Test') {

steps {

//...

}

}

}

}

Sample Piple line looks like this.

Example1:

node {

stage('stage1'){

echo 'Hello World'

}

stage('stage2'){

echo 'Send Atage -Hello World'

}

}

Example2:

node {

stage('echo'){

sh 'echo "hello world"' //This is for single line DSL command

sh '''

echo " Multiple line step"

ls -lah

'''

}

}

To list directories for both Linux and windows

node {

stage('list directory'){

if (isUnix()){

sh "ls -la" //for linux

} else {

bat 'dir' //for windows

}

}

Checkout Git Repository

node {

stage( "checkout') {

git 'https://xxx.git'

}

stage ('Say Hello') {

sh "./say_hello.sh"

}

}

Using Credentials in pipeline.

First create credential from the jenkins and provide the 'ID" field with a name, say my_id_1. This name will be used in the pipeline

node {

stage( "use passwords') {

with Credentials({usernameColonPassword(credentialsId:'my_id_1', variable: ''MY_CREDENTIALS_1')]){

echo "My password is '$MY_CREDENTIALS_1}'!" // echo will pwd as ***

}

}

Error Handling

Since we are using Groovy,we can use try catch block to handle exception

node{

try{

state('echo'){

sh 'echo hello world'

//generate exception

def obj = null

sh "${obj.class}"

}

} catch (NullPointerException e) {

error 'broken pipeline - null pointer exception'

currentBuild.result = 'FAILURE'

//currentBuild.result = 'UNSTABLE'

} finally {

stage('post build'){

echo 'This will always run' //even if the build fails it will run

}

stage('Send notification'){

mail to: 'suresh@knowesis.com',

body: " Something went wrong ${env.BUILD_URL}",

subject: "Failed pipeline: ${currentBuild.fullDisplayName}"

}

}

}

If you want to set the build to unstable. You will need to remove the error. On the build stage page the build will be coloured with yellow. The difference here with error is that the build continues to the next stage marking the stage as Unstable.

If we need a script to be run for every stage add a finally block after the catch. This will run every thing the stage is executed irrespective of the build status.

DECLARATIVE PIPELINE

Simplified and optioned syntax on top of the pipeline sub-systems. Follows the groovy syntax. The top-level of the pipeline must be a pipeline {...} block. Inside the pipeline the blocks consists of sections, directives, steps, or assignment statements.

Common Section;

Agent- defines on which agent the pipeline or the stage will be executed

Stages - defines the stages of the pipeline

Post - defines the post build steps

Example

pipeline{

agent {

label 'linux' // We are instructing the pipeline to run with teh label linux

}

stages{

//we can have multiple stages

stage{

...

}

post { // always run this step after stages

always{

echo "pipeline executed"

}

}

}

}

Common Directives:

Trigges: automatically triggers the pipelines based on definition

Parameters - defines parameter when triggering pipelines

Tools - defines the tools configured on Jenkins

Stage - inside of the stages section; contains steps and agents

When -give control to what should be executed in the pipeline

JENKINS FILE

Example:

pipeline {

agent { label 'linux' }

stages {

stage ('Checkout') {

steps {

git 'https://github.com/effectivejenkins/spring-petclinic.git'

}

}

stage('Build') {

agent { docker 'maven:3.5-alpine' }

steps {

sh 'mvn clean package'

junit '**/target/surefire-reports/TEST-*.xml'

archiveArtifacts artifacts: 'target/*.jar', fingerprint: true //creates fingerprint

}

}

stage('Deploy') {

steps {

input 'Do you approve the deployment?'

sh 'scp target/*.jar jenkins@192.168.50.10:/opt/pet/'

sh "ssh jenkins@192.168.50.10 'nohup java -jar /opt/pet/spring-petclinic-1.5.1.jar &'"

}

}

}

}

Course files:

https://github.com/effectivejenkins