Jenkins Pipeline

Jenkins Pipeline

ยท

5 min read

Let's start!

Jenkins Pipeline

According to the official documentation of Jenkins, " Jenkins Pipeline is a suite of plugins that supports implementing and integrating continuous delivery pipelines into Jenkins. "

Pipelines allow for defining a series of steps that will be executed whenever the change of code like building, testing, and deploying takes place.

Pipelines enable modeling simple-to-complex delivery Pipelines as a Code. The definition of a Jenkins Pipeline is written into a text file called Jenkinsfile.

Jenkins Pipelines are written in a domain-specific language (DSL) called Declarative Pipeline. This DSL makes it easy to define complex pipelines without writing a lot of code.

Why Pipelines?

  • They are easy to use.

  • They are scalable.

  • They are flexible.

  • They are secure.

  • Automate the software development process.

  • Improve the quality of your code.

  • Increase the speed of your deployments.

  • Improve collaboration.

Pipeline Concepts

Pipeline

A Pipeline is a user-defined model of a CD pipeline. This is a vital part of Declarative Pipeline Syntax.

Node

A node is a machine part of the Jenkins environment and capable of executing a Pipeline. This is a key part of Scripted Pipeline Syntax.

Stage

A stage in a pipeline is a logical grouping of steps that are executed together. Stages are typically used to group related tasks, such as building, testing, and deploying code.

Step

A step in a pipeline is a single action that is executed as part of a stage. Steps are typically used to perform tasks such as building, testing, and deploying code.

Now let us know the types of Pipelines. There are two types: Declarative Pipelines and Scripted Pipelines.

Declarative Pipeline

Declarative Pipeline is based on a set of declarative statements that define the structure and behavior of your pipeline. These statements are grouped into sections, which are executed in order.
Declarative Pipeline is a new syntax for writing Jenkins Pipelines. It is designed to be more concise and easier to read than the Scripted Pipeline syntax.

Syntax

pipeline {
    agent any #1
    stages {
        stage('Build') { #2
            steps {
                //#3
            }
        }
        stage('Test') { #4
            steps {
                //#5
            }
        }
        stage('Deploy') { #6
            steps {
                //#7
            }
        }
    }
}

#1


Execute this Pipeline or any of its stages, on any available agent.

#2

Defines the "Build" stage.

#3

Perform some steps related to the "Build" stage.

#4

Defines the "Test" stage.

#5

Perform some steps related to the "Test" stage.

#6

Defines the "Deploy" stage.

#7

Perform some steps related to the "Deploy" stage.

Scripted Pipeline

Scripted Pipeline is based on a Groovy script that defines the structure and behavior of the pipeline. The script is executed in order, and it can contain a variety of steps, including building, testing, and deploying code.
Scripted Pipeline is a syntax for writing Jenkins Pipelines. It is the original syntax for Pipelines, and it is still supported in Jenkins.

Syntax

node { #1
    stage('Build') { #2
        //#3
    }
    stage('Test') { #4
        // #5
    }
    stage('Deploy') { #6
        // #7
    }
}

#1


Execute this Pipeline or any of its stages, on any available agent.

#2

Defines the "Build" stage. 'stage' blocks are optional in Scripted Pipeline syntax.

#3

Perform some steps related to the "Build" stage.

#4

Defines the "Test" stage.

#5

Perform some steps related to the "Test" stage.

#6

Defines the "Deploy" stage.

#7

Perform some steps related to the "Deploy" stage.

Difference between Declarative and Scripted Pipelines

Feature

Declarative Pipelines

Scripted Pipelines

Syntax

YAML

Groovy

Ease of use

Easy to learn and use

More complex to learn

Scalability

Scalable

Scalable

Flexibility

Flexible

More flexible

Security

Secure

Less secure

Power

Less powerful

More powerful

Familiarity

Less familiar

More familiar if you know Groovy

Jenkinsfile

A Jenkinsfile is a text file that contains the definition of a Jenkins Pipeline. It is typically stored in the root directory of the project that it is used to automate.

The following is an example of a Jenkinsfile:

pipeline {
  agent any
  stages {
    stage('Build') {
      steps {
        sh 'mvn clean install'
      }
    }
    stage('Deploy') {
      steps {
        sh 'scp -r target/*.jar localhost:/opt/app'
      }
    }
  }
}

This is the foundation of "Pipeline-as-code"; treating the CD pipeline as a part of the application to be versioned and reviewed like any other code.

Now let us complete tasks as we now know the basics of the Pipelines.

Task 1: Create a Jenkins Pipeline Project using declarative pipeline syntax to print "Hello World"

1) Create a Jenkins Pipeline named "HelloWorld Example1"

2) Go to the Dashboard > Configure > Pipeline > Select Definition as "Pipeline Script" > And enter the following command.

 pipeline {
    agent any

    stages {
        stage('Hello') {
            steps {
                echo 'Hello World'
            }
        }
    }
}

3) Click on Save and Build the Pipeline.

4) In the Pipeline Dashboard, you can see the Stage View as well. Our job is successful.

5) This is the Console Output. It says SUCCESS. Also, you can observe that the "HELLO WORLD" is printed.

In this blog, I have discussed Jenkins Pipelines, Jenkinsfile, types of Jenkins pipelines,If you have any questions or would like to share your experiences, feel free to leave a comment below. Don't forget to read my blogs.

Thanks for Reading!! ๐Ÿ˜Š๐Ÿ˜Š

ย