jenkinsfile declarative pipeline with code examples

Jenkins is one of the most popular Continuous Integration and Continuous Delivery (CI/CD) tools available in the market. It provides a number of features that can automate the build, test, and deployment phases of software development. Jenkins also allows you to create and configure pipelines, which are used to automate your software delivery process from start to finish.

The Jenkinsfile is a configuration file that defines the steps of your pipeline and allows you to define the entire pipeline process as code. In this article, we will explore the Jenkinsfile declarative pipeline and provide code examples to help you understand it better.

Jenkinsfile Declarative Pipeline Overview

Jenkinsfile declarative pipeline is a more recent addition to Jenkins and provides a simpler and more intuitive way to write pipelines when compared to the traditional Scripted Pipeline. The declarative syntax provides a more structured way of defining your pipeline, making it easier to understand, share, and maintain.

Here's an example of what a Jenkinsfile declarative pipeline might look like:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
        stage('Test') {
            steps {
                sh 'npm run test'
            }
        }
        stage('Deploy') {
            when {
                branch 'master'
            }
            steps {
                sh 'npm run deploy'
            }
        }
    }
}

This code defines a simple three-stage pipeline, consisting of Build, Test, and Deploy stages. The first stage installs the necessary dependencies required for running the application. It's followed by a Test stage that runs the automated tests and checks the code for errors. Finally, the Deploy stage deploys the code to production, but only if the pipeline is run on the master branch.

As you can see, the declarative pipeline syntax provides a clean and structured way to define your pipeline stages, steps, and other variables. Let's take a closer look at some of the key components that make up the Jenkinsfile declarative pipeline.

Agent

The agent is the machine on which all the pipeline stages and steps are executed. You can specify the type of machine or container that you want to run the pipeline on.

For example, you can use 'any' to specify that your pipeline should be executed on any available machine:

pipeline {
    agent any
    ...
}

Alternatively, you can specify a specific machine or container:

pipeline {
    agent {
        label 'my-machine'
    }
    ...
}

In this case, the pipeline will run on the machine labeled 'my-machine.'

Stages

The Stages block defines the different stages of your pipeline, which are made up of one or more steps. In the example pipeline we provided earlier, the stages were Build, Test, and Deploy.

Each stage is composed of a series of steps that execute commands, run scripts, and perform other actions required to complete the stage. The steps can be shell commands, script files, or other Jenkins plugins.

Here's an example of a stage that defines a shell step:

pipeline {
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
    }
}

In the above example, the Build stage has one step, which installs required dependencies using the npm package manager.

Steps and Post

The Steps block defines the different command-line steps that run within each stage of the pipeline and make up the pipeline's work. Each step has an instruction that defines the action to be taken.

pipeline {
    stages {
        stage('Test') {
            steps {
                sh 'npm run test'
            }
        }
    }
}

In this example, the Test stage has only one step, which is to run automated tests using the command npm run test.

If you want to perform an action after all steps in a stage have been completed, you can use the Post block. The Post block accepts three types of actions: always, success, and failure.

pipeline {
    stages {
        stage('Deploy') {
            when {
                branch 'master'
            }
            steps {
                sh 'npm run deploy'
            }
            post {
                always {
                    slackSend channel: '#my-channel',
                              message: "Deployment completed"
                }
                success {
                    sh 'npm run cleanup'
                }
            }
        }
    }
}

In the example above, the Post block contains an Always action that sends a Slack message to a specified channel when the pipeline completes, regardless of whether it was successful or not. The Success action also includes a step to perform cleanup after the pipeline is successful.

Conclusion

Jenkinsfile declarative pipeline makes it easier to create, share and maintain your pipelines. It provides a structured and intuitive way to define the different stages, steps, and other elements required to build, test, and deploy your applications. This article explored the key components of the Jenkinsfile declarative pipeline syntax through several code examples. We hope that it has helped you understand this topic better.

here are some additional details about the topics covered in the previous article:

Agent:

The Agent block in the Jenkinsfile declarative pipeline specifies the machine or container on which the pipeline should be executed. In the example provided in the previous article, the 'any' keyword was used to specify that the pipeline can run on any available machine. Alternatively, a specific machine or container can be specified using the 'label' keyword. For instance, agent { label 'linux' } specifies that the pipeline should run on a machine with the label 'linux'.

Stages:

Stages block is used to define the different stages of the pipeline. Each stage is made up of one or more steps and can be considered as a logical division of the pipeline. Every stage has a name and can be defined with the stage keyword. For example:

pipeline {
    stages {
        stage('Build') {
            steps {
                // Build steps here
            }
        }
        stage('Test') {
            steps {
                // Test steps here
            }
        }
    }
}

In this example, the pipeline has two stages, named 'Build' and 'Test'. The Build stage has steps for building the application, while the Test stage has steps for testing it.

Steps:

The Steps block defines the actual commands that need to be executed in a given stage. These commands can be shell commands, script files, or other Jenkins plugins. Here's an example of a Jenkinsfile declarative pipeline step that uses the sh keyword:

pipeline {
    stages {
        stage('Build') {
            steps {
                sh 'npm install'
            }
        }
    }
}

In this example, the pipeline step installs the dependencies using npm, which is a package manager for Node.js.

Post:

The Post block is a powerful feature in Jenkinsfile declarative pipeline that allows you to perform actions after all the stages and steps have been completed. The post block has actions that can be taken depending on whether the pipeline was successful or not.

There are three types of actions available in the Post block: always, success, and failure. The 'always' action is executed regardless of the pipeline's success or failure. The 'success' action is executed only if the pipeline is successful, while the 'failure' action is executed only if the pipeline fails.

pipeline {
    stages {
        stage('Deploy') {
            steps {
                // Deployment steps here
            }
        }
    }
    post {
        success {
            slackSend channel: '#my-channel', message: "Deployment completed successfully"
        }
        failure {
            slackSend channel: '#my-channel', message: "Deployment failed"
        }
        always {
            sh 'npm run cleanup'
        }
    }
}

In this example, the post block contains three actions: success, failure, and always. The 'always' action runs the command npm run cleanup regardless of the success or failure of the pipeline. The 'success' action sends a message to the Slack channel if the pipeline is successful, while the 'failure' action sends a message to the same channel if the pipeline fails.

Conclusion:

The Jenkinsfile declarative pipeline provides an intuitive and structured way of defining, creating, and maintaining your pipelines. It is a powerful tool that can automate your software development process from start to finish. The key components of the Jenkinsfile declarative pipeline syntax are Agent, Stages, Steps, and Post. The Agent defines the machine or container to run the pipeline; the Stages define the pipeline's different stages; the Steps define the actual commands to be executed; and the Post defines the actions to be taken after all the stages and steps have been completed. With the help of these components, you can create highly customizable pipelines that meet your specific requirements.

Popular questions

  1. What is a Jenkinsfile declarative pipeline?

A Jenkinsfile declarative pipeline is a configuration file used to define a pipeline in Jenkins, which is used to automate the build, test, and deploy phases of software development. The declarative syntax provides a structured and intuitive way of defining pipeline stages, making it easier to understand, share, and maintain.

  1. What is the purpose of the Agent block in the Jenkinsfile declarative pipeline?

The Agent block in the Jenkinsfile declarative pipeline specifies the machine or container on which the pipeline should be executed. It can be used to specify any available machine or container, or a specific machine or container with a designated label.

  1. How are stages defined in the Jenkinsfile declarative pipeline?

The Stages block in the Jenkinsfile declarative pipeline is used to define the different stages of the pipeline. Each stage is made up of one or more steps and can be considered as a logical division of the pipeline. Each stage has a name and can be defined using the stage keyword.

  1. What are the Steps in the Jenkinsfile declarative pipeline?

The Steps block in the Jenkinsfile declarative pipeline defines the specific commands that are executed within each stage of the pipeline. Each step has an instruction that defines the action to be taken, and the steps can be shell commands, script files, or other Jenkins plugins.

  1. What is the purpose of the Post block in the Jenkinsfile declarative pipeline?

The Post block in the Jenkinsfile declarative pipeline allows you to perform actions after all the stages and steps of the pipeline have been completed. The block can be used to specify actions to be taken regardless of whether the pipeline succeeds or fails, or only if the pipeline is successful or fails. This block is useful for cleanup actions or for sending notifications to users.

Tag

DevOps

I am a driven and diligent DevOps Engineer with demonstrated proficiency in automation and deployment tools, including Jenkins, Docker, Kubernetes, and Ansible. With over 2 years of experience in DevOps and Platform engineering, I specialize in Cloud computing and building infrastructures for Big-Data/Data-Analytics solutions and Cloud Migrations. I am eager to utilize my technical expertise and interpersonal skills in a demanding role and work environment. Additionally, I firmly believe that knowledge is an endless pursuit.

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top