1. Overview

Jenkins is an open-source automation server that facilitates the building, testing, and deployment of software applications. Jenkins provides a scripted pipeline syntax, which permits users to write Jenkins pipelines using the Groovy scripting language. Scripted pipelines in Jenkins are very adaptable and can be customized to meet specific requirements.

In a scripted pipeline, stages are laid out as a series of steps that Jenkins will perform. Each stage corresponds to a distinct phase in the process, like building, testing, and deploying the application. In some cases, it may be required to skip specific stages in the pipeline.

In this tutorial, we’ll discuss different solutions for skipping stages in a Jenkins-scripted pipeline.

2. Importance of Skipping Stages in Pipeline Jobs

Skipping stages in a pipeline job can be important for several reasons. It is very useful to speed up the pipeline execution time. Sometimes certain stages may be unnecessary or take long to run, slowing down the overall pipeline.

Skipping stages in a pipeline job can potentially help speed up the development process by reducing the time it takes for the job to complete. If a pipeline includes a testing stage, developers may want to skip this stage during local development in order to get faster feedback on their changes.

3. Using the when Directive

The when directive in Jenkins scripted pipeline allows developers to define a condition that must be true for a particular stage to execute. We can use the when directive to customize the execution of our pipeline job. We can apply the when directive to a stage, step, or code block:

pipeline {
    agent any
    parameters {
        booleanParam(name: 'skip_test', defaultValue: false, description: 'Set to true to skip the test stage')
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo "Building application"'
                // Add build steps here
            }
        }
        stage('Test') {
            when { expression { params.skip_test != true } }
            steps {
                sh 'echo "Testing application"'
                // Add test steps here
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo "Deploying application"'
                // Add deployment steps here
            }
        }
    }
}

The above Jenkins pipeline automates the Continuous Integration/Continuous Deployment process for an application. It defines three stages – Build, Test, and Deploy.

In the Build stage, we use an echo statement to indicate that we are building the application. The Test stage is contingent and will only be executed if the boolean parameter skip_test is false. If skip_test is true, the test stage will be entirely omitted. The Test stage includes an echo statement that notifies us that the application is being tested. The Deploy stage involves deploying the application.

Overall, the Jenkins pipeline is a productive way to automate CI/CD and conserve time and resources.

4. Using the input step

Jenkins scripted pipeline allows developers to pause pipeline jobs while they await user input. This can be useful in situations where a developer wants to be able to choose whether to skip a particular stage in the pipeline:

pipeline {
    agent any
    stages {
        stage('Build') {
            steps {
                sh 'echo "Building the application"'
                // Add steps to build the application
            }
        }
        stage('Test') {
            steps {
                input message: 'Want to skip the test stage?', ok: 'Yes',
                  parameters: [booleanParam(name: 'skip_test', defaultValue: false)], timeout: time(minutes: 5))
                script {
                    if(params.skip_test) {
                        sh 'echo "Testing the application"'
                        return
                    }
                }
                // Add steps to test the application
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo "Deploying the application"'
                // Add steps to deploy the application
            }
        }
    }
}

The stages block defines three stages of the pipeline. The Build stage contains the directive to build the application, whereas the Deploy stage, includes the directive to deploy the application. The Test stage involves an input step that prompts the user to determine whether to skip the test stage. The script block examines the input parameter skip_test and only executes the command to test the application if skip_test is false.

Each stage can have multiple steps by adding them to the steps block. To illustrate, we can add build or deployment steps to the corresponding stages. Overall, this pipeline defines a workflow that builds, tests, and deploys an application while providing the option to skip the Test stage.

5. Using Function to Control Pipeline Flow

The third method for skipping stages in a Jenkins scripted pipeline necessitates utilizing a function to regulate the pipeline’s flow. This approach is advantageous when we need to skip several stages in the pipeline and we want to avoid copying the same code multiple times.

The fundamental concept is to create a function that accepts the stage name and a boolean parameter to specify whether to skip the stage. The function subsequently contains the steps for the stage inside an if statement, which only carries out the steps if the skip parameter is false:

pipeline {
    agent any
    parameters {
        booleanParam(name: 'skip_test', defaultValue: false, description: 'Set to true to skip the test stage')
    }
    stages {
        stage('Build') {
            steps {
                sh 'echo "Building the application"'
            }
        }
        stage('Test') {
            steps {
                execute_stage('Test', params.skip_test)
            }
        }
        stage('Deploy') {
            steps {
                sh 'echo "Deploying the application"'
            }
        }
    }
}

def execute_stage(stage_name, skip) {
    stage(stage_name) {
        if(skip) {
            echo "Skipping ${stage_name} stage"
            return
        }
        // Add steps to test the application
    }
}

The above pipeline job contains a stages block that establishes three pipeline stages. The Build stage encompasses a sole step that executes a shell command to echo the message “Building application”. The Test stage employs a custom function called execute_stage to execute the test stage by providing the stage name and the value of the skip_test parameter as arguments. The execute_stage function initially generates a new stage with the specified stage name and then evaluates whether skip is true.

By default, this function skips the stage, prints a message, and ends without executing any further steps if the skip is true. If the skip is false, the function proceeds to execute any additional steps integrated into the execute_stage function. With this pipeline, we can skip the Test stage by setting skip_test to true, which will omit the Test stage during execution.

6. Conclusion

In this article, we discussed three different solutions for skipping stages in a Jenkins-scripted pipeline. First, we explored when directive to skip a stage based on a condition. After that, we used the input step to prompt the user to skip a stage. Finally, we looked at a custom function to control the flow of the pipeline.

We can also find scripts to skip a stage in the Jenkins pipeline job over on GitHub.

Comments are open for 30 days after publishing a post. For any issues past this date, use the Contact form on the site.