Day26 of #90DaysOfDevOps Challenge

Jenkins Pipeline

Table of contents

Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. This process involves building the software in a reliable and repeatable manner, as well as progressing the built software (called a "build") through multiple stages of testing and deployment.

The continuous delivery pipeline starts when a developer makes any changes and commits it to the source code, the pipeline then builds the artifact, tests the application, and releases it to deliver to the customer.

In Jenkins, we can write the pipeline as a code either in the UI or in a text file called Jenkinsfile which can be committed to the repository with the source code.

There are two ways of defining a pipeline in Jenkins -

  1. Scripted pipeline

  2. Declarative pipeline

In Declarative Pipeline syntax, the pipeline block defines all the work done throughout your entire Pipeline. agent is Declarative Pipeline-specific syntax that instructs Jenkins to allocate an executor (on a node) and workspace for the entire Pipeline.

Jenkinsfile (Declarative Pipeline)
pipeline {
    agent any 
    stages {
        stage('Build') { 
            steps {
                // 
            }
        }
        stage('Test') { 
            steps {
                // 
            }
        }
        stage('Deploy') { 
            steps {
                // 
            }
        }
    }
}

In a Scripted pipeline, one or more 'node' blocks do the core work throughout the entire pipeline, however, this block is not mandatory but having the block does 2 things -

  1. Schedules the steps contained within the block to run by adding an item to the Jenkins queue. As soon as an executor is free on a node, the steps will run.

  2. Creates a workspace (a directory specific to that particular Pipeline) where work can be done on files checked out from the source control.

Jenkinsfile (Scripted Pipeline)
node {  
    stage('Build') { 
        // 
    }
    stage('Test') { 
        // 
    }
    stage('Deploy') { 
        // 
    }
}

Task:

  • Create a New Job, this time select Pipeline instead of Freestyle Project.

No alt text provided for this image

\> Scripted Pipeline to print "Hello world".

No alt text provided for this image

\> Output

No alt text provided for this image

  • Complete the example using the Declarative pipeline.

\> Defined a Jenkinsfile in the repo.

No alt text provided for this image

\> Configure the pipeline accordingly.

No alt text provided for this image

\> Provide the path to Jenkinsfile.

No alt text provided for this image

\> The pipeline builds successfully.

No alt text provided for this image


Thank you for reading! 📘