This guide explains implementing nested and parallel stages in Jenkins pipelines for improved task organization and execution efficiency.
This guide explains how to implement nested and parallel stages in your Jenkins pipelines. It covers the benefits and configurations of each method, allowing you to organize your tasks for improved readability and faster execution.
Nested stages help group related tasks into a parent stage, making your pipeline more organized and easier to understand. For instance, a “lint and Test” stage can contain separate sub-stages for linting and unit testing. Although these nested stages execute sequentially, grouping them clarifies the pipeline’s logical structure.Consider the following example that demonstrates nested stages:
In this scenario, the “lint and Test” stage comprises two sub-stages: “Lint” and “Unit Tests”. If you then add a separate “Deploy” stage, the pipeline runs the linting and testing stages first, followed by the deployment stage:
Parallel stages enable concurrent execution of multiple stages, which can greatly reduce the total build time. In the previous example, “Lint” and “Unit Tests” executed one after the other. To have them run simultaneously, replace the stages block with a parallel block.Here’s a modified pipeline that runs linting and unit testing concurrently:
When using parallel stages, both “Lint” and “Unit Tests” begin at the same time. The pipeline waits for both to complete before proceeding to the “Deploy” stage.
Below is another example where a nested stage called “lint and format” is introduced. Initially, it contains only a linting stage, but later a formatting stage may be added. Additionally, a “Setup” stage is defined outside the nested stages:
After pushing the changes to Git and building the pipeline, the output shows entry into the “lint and format” stage, where the nested stages handle linting and formatting tasks separately. Later, the formatting stage can be added to run concurrently with linting by modifying the configuration:
Copy
Ask AI
pipeline { agent any stages { stage('lint and format') { parallel { stage('linting') { steps { echo "linting code in nested stage" } } stage('formatting') { steps { echo "formatting code in nested stage" } } } } }}
Running these nested stages in parallel is especially useful in CI/CD pipelines where independent tasks can execute simultaneously to save time.
Demonstrating Parallel Execution with Sleep Commands
To clearly demonstrate the advantage of parallel execution, consider the example below where each nested stage executes a sleep command for 30 seconds. If executed sequentially, the total wait time would be 60 seconds; however, in parallel execution, the overall delay is only 30 seconds.
This output confirms that both stages start simultaneously, enhancing the overall efficiency of the build process.Both nested and parallel staging techniques offer valuable ways to organize and optimize Jenkins pipelines. Nested stages improve the clarity and maintenance of your pipeline, while parallel stages can dramatically reduce execution times by running independent tasks concurrently.