Creating a Nested Jenkins Pipeline for Advanced Automation

Creating a Nested Jenkins Pipeline for Advanced Automation

Introduction

Jenkins is a formidable automation tool that enables effective development, testing, and deployment of your applications. The ability to build nested pipelines, which let you organize and arrange complicated workflows, is one of its most flexible capabilities. Let's set up a nested Jenkins pipeline that calls a Smoke test child pipeline(running a Selenium Test suite) via the parent pipeline. This approach will help you streamline your Smoke testing process to run after the code is deployed in the dev/qua regions.

Step 1: Install and Configure Jenkins

Installing and setting up Jenkins on your server is the first step. For detailed installation instructions tailored to your environment, refer to the official Jenkins guide. Once installed, set up Jenkins to work with your version control system (e.g., Git, SVN).

Step 2: Create and configure the Parent Pipeline

If the parent pipeline is not already created with stages like checkout, build, unit test, sonar test, liquibase automation, and deploy then you have to create that first. The user interface of Jenkins is really good, you can navigate to the "New Item" page and select "Pipeline" as the project type. Provide a name for your pipeline and click "OK" to proceed. After that add the necessary stages which are required in your parent pipeline.

Here's an example Jenkinsfile snippet for the parent pipeline to deploy any new feature or bug fixes.

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                // Checkout code logic from your version
                // control system
            }
        }
        stage('Build') {
            steps {
                // Build your application via 
                //maven, npm or any other building tools
            }
        }
        stage('Unit Test & Sonar Test') {
            steps {
                // Run unit & tests for your written code for 
                //coverage and code quality
            }
        }
        stage('Deploy') {
            steps {
                // Deploy your application on a particular server
            }
        }

    }
}

Step 3: Create and configure the Child Pipeline

You need to create another Jenkins pipeline that runs jobs for the execution of Selenium Test Suite to test the functionality of a web application.

Here's an example Jenkinsfile snippet for another pipeline(in our case this will be considered a child pipeline) to run automation tests via TestNG/Selenium tests.

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                // checkout code
            }
        }
        stage('Run Test Suite') {
            steps {
                //Execute automation test cases
            }
        }

        stage('Deploy test results on artifactory') {
            steps {
                //Can download the results to check
                //the results of test cases.
            }
        } 
    }
}

Step 4: Call Child Pipeline from the Parent Pipeline

Once you have your BAU pipeline(parent) and Smoke test pipeline(child) ready, you can invoke the child pipeline from within the stages or steps of your parent pipeline. Utilize Jenkins' build or builds step to trigger the child pipeline. Specify the child pipeline's name and any required parameters. This will execute the child pipeline and return control to the parent pipeline once it finishes.

Here's an example Jenkinsfile snippet to call a child pipeline:

pipeline {
    agent any
    stages {
        stage('Checkout') {
            steps {
                // Checkout code logic from your version
                // control system
            }
        }
        stage('Build') {
            steps {
                // Build your application via 
                //maven, npm or any other building tools
            }
        }
        stage('Unit Test & Sonar Test') {
            steps {
                // Run unit & tests for your written code for 
                //coverage and code quality
            }
        }
        stage('Deploy') {
            steps {
                // Deploy your application on a particular server
            }
        }

        //Example of nested pipeline
        stage('Smoke Test') {
            steps {
                // Call child pipeline with parameters
                build job: 'Child Pipeline', parameters: [
                    string(environment: 'Qualif', value: 'qua3')
                ]
            }
        }
    }
}

Conclusion

By implementing nested Jenkins pipelines, you can streamline your Smoke testing process and run Selenium Test suites after deploying your code. This method ensures that your application works fine and reduces the chance of introducing defects into the production environment.

---

Thank you for reading. There are various interesting applications of nested Jenkins automation. Keep exploring. Cheers!