Understanding Jenkins Pipeline and Jenkinsfile

Jenkins Pipeline is a collection of plugins that help us to set up and use continuous delivery pipelines in Jenkins. A continuous delivery pipeline is an automated representation of the software delivery process, from version control to users and customers.

✔  Different phases of a job are written in a file and then the Jenkins pipeline runs the entire pipeline as a code.

✔  We can save this file in a Version Control System (VCS) and this file is named Jenkinsfile.

 

What is Jenkinsfile?

✔  It is a text file that stores the pipeline as code.

✔  It can be stored in SCM which makes it accessible for your local system.

✔  This gives advantages to developers, they can access, edit and check the file all the time.

✔  It is written using Groovy scripts.

✔  It has two types of syntaxes in which we can write the Jenkinsfile. Followings are two types:

    ✔  Declarative Pipeline

    ✔  Scripted Pipeline

 

Declarative and Scripted Pipeline:

Declarative Pipeline:

✔  The recent pipeline syntax between the two syntaxes.

✔  Uses simpler Groovy scripts.

✔  Code can be written in Jenkins UI and locally that can be checked into an SCM.

✔  The entire code is defined within a ‘pipeline’ block.

Syntax Example:

pipeline {
     agent any
  
     stages {
         stage('Hello') {
             steps {
                 echo 'Hello World'
             }
         }
     }
 } 

 

Understanding the Components:-

✔  pipeline: entire code will be defined inside this block. It is a main part of the declarative pipeline syntax.

✔  agent: is an executor that will run the entire pipeline or a specific part of the pipeline depending on where it is defined in the script. It has the following parameters:

    ✔  any: runs the pipeline or stage on any available agent.

    ✔  none: this parameter must be applied at the root of the pipeline, and it means that every stage within the pipeline has to define its own agent.

    ✔  label: label is a name given to agents and the labeled agent will run the entire pipeline/ stage.

    ✔  docker: uses docker container as an execution environment for the pipeline/ stage.

 

pipeline {
     agent {
         docker {
             image 'ubuntu'
         }
     }
 }

 

✔  stages: contains several stages that have a specific task.

pipeline-components

✔  steps: has a specific task that completes the execution of a stage.

 

stages{
  stage('Build'){
    steps{
        ...
    }
  }
}

 

Scripted Pipeline:

  • The old and traditional way of writing the code.
  • Uses stricter Groovy Syntax.
  • Code can only be written in Jenkins UI.
  • The entire code is defined within a ‘node’ block.

Syntax Example:

node('master'){   
      stage('Hello'){
          echo 'Hello World'    
     }
 }

Understanding the Components:-

✔  node: entire code will be defined inside this block. It has the name of an agent. It is the main part of the scripted pipeline syntax.

✔  stage: is similar to the stage of the declarative pipeline but it doesn’t have the steps component in it.

 

Conclusion:

We have learned about Jenkins Pipeline, its type, and what is the syntax of those types. We have seen the differences in the syntax and learned about different components in the pipeline.