Nested Stages
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:Grouping related steps into nested stages does not alter the execution order but improves the overall readability of the pipeline.
Parallel Stages
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 thestages 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.
Nested Stages with Additional Examples
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:Demonstrating Parallel Execution with Sleep Commands
To clearly demonstrate the advantage of parallel execution, consider the example below where each nested stage executes asleep 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.
Sequential Execution
Parallel Execution
Pipeline Output Overview
A sample Jenkins pipeline console output might include the following steps:- Checking out code from the Git repository.
- Entering the “lint and format” stage.
- Concurrently executing the “linting” and “formatting” stages.
- Proceeding to further stages such as “Setup” after the parallel stages completion.