Jenkins pipeline part 2 – stages and steps
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss how to use stages and steps in Jenkins pipeline with examples.
What is a stages is Jenkins pipeline?
Stages contains a sequence of one or more stage directives, the stages section is where the bulk of the “work” described by a Pipeline will be located. At a minimum, it is recommended that stages contain at least one stage directive for each discrete part of the continuous delivery process, such as Build, Test, and Deploy.
What is a stage is Jenkins pipeline?
The stage directive goes in the stages section and should contain a steps section, an optional agent section, or other stage-specific directives. Practically speaking, all of the real work done by a Pipeline will be wrapped in one or more stage directives.
What are steps in Jenkins pipeline?
The steps section defines a series of one or more steps to be executed in a given stage directive. Steps represent the unit of action that you want to perform in your Jenkins pipeline. For each action or set of actions you need to define a step in your Jenkins pipeline.
Note: Declarative Pipelines may use all the available steps documented in the Pipeline Steps reference, which contains a comprehensive list of steps that can be used in your Jenkins pipeline based on the availability of the plugin.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
## -------------- ## Stages & Stage ## -------------- pipeline { agent { ... } stages { stage(' steps { ... } ... } stage(...) { ... } ... } } |
Note: Each stage supports multiple directives like environment, options, triggers, parameters, input, when etc. which are covered separately in coming blog posts in this series.
What are sequential stages in Jenkins pipeline?
Stages in Declarative Pipeline may have a stages section containing a list of nested stages to be run in sequential order. Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
## ----------------- ## Sequential stages ## ----------------- pipeline { agent { ... } stages { stage('normal_stage') { steps { ... } ... } stage('sequestial_stage') { stages { stage('seq_one') { steps { ... } ... } stage('seq_two') { steps { ... } ... } ... } } ... } } |
What are parallel stages in Jenkins pipeline?
Stages in Declarative Pipeline may have a parallel section containing a list of nested stages to be run in parallel. Note that a stage must have one and only one of steps, stages, parallel, or matrix. It is not possible to nest a parallel or matrix block within a stage directive if that stage directive is nested within a parallel or matrix block itself. However, a stage directive within a parallel or matrix block can use all other functionality of a stage, including agent, tools, when, etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## ------------- ## Parallel Stage ## -------------- pipeline { agent { ... } stages { stage('Normal Stage') { steps { } } stage('Parallel Stage') { parallel { stage('parallel stage one') { steps { } } stage('parallel stage two') { steps { } } ... } ... } ... } } |
Enough of theories, now let’s dig down a bit with some examples.
Jenkins pipeline part 2 – stages and steps
Prerequisites
One system with Jenkins installed.
Normal Stages Example:
Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
## ------------ ## Normal Stage ## ------------ pipeline { agent { label 'test' } stages { stage('Build') { steps { sh 'echo "building your code"' } } stage('Test') { steps { sh 'echo "testing your code"' } } stage('Deploy') { steps { sh 'echo "approval required"' sh 'echo "deploying your code"' } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
Sequential Stages Example:
Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## ---------------- ## Sequential Stage ## ---------------- pipeline { agent { label 'test' } stages { stage('Build') { steps { sh 'echo "building your code"' } } stage('Test') { steps { sh 'echo "testing your code"' } } stage('Deploy') { stages { stage('Approval') { steps { sh 'echo "approve deployment"' } } stage('Deployment') { steps { sh 'echo "deploying your code"' } } } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
Parallel Stages Example:`
Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
## -------------- ## Parallel Stage ## -------------- pipeline { agent { label 'test' } stages { stage('Test') { steps { sh 'echo "testing your code"' } } stage('Build') { parallel { stage('Version1') { steps { echo "building version 1" } } stage('Version2') { steps { echo "building version 2" } } } } stage('Deploy') { steps { sh 'echo "approval required"' sh 'echo "deploying your code"' } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
You can also nest different stages as shown below.
Nesting Stages Example:
Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
## -------------- ## Nesting Stages ## -------------- pipeline { agent { label 'test' } stages { stage('Normal') { steps { sh 'echo "stage one"' } } stage('Parallel') { parallel { stage('paraINpara') { steps { sh 'echo "nesting parallel not supported"' } } stage('seqINpara') { stages { stage('one') { steps { echo "stage one" } } stage('two') { steps { echo "stage one" } } } } } } stage('Sequestial') { stages { stage('seqINseq') { stages { stage('one') { steps { echo "stage one" } } stage('two') { steps { echo "stage one" } } } } stage('paraINseq') { parallel { stage('one') { steps { echo "stage one" } } stage('two') { steps { echo "stage one" } } } } } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.
Steps Example:
So far, we have executed shell command (sh block) in the steps. But steps can do more, Jenkins has a ton of steps that are available through plugins that you can use in your pipeline.
Jenkins also provides a Snippet Generator built right into your Jenkins dashboard which can help you generate the steps code for a particular plugin.
Let me give you a demo of using a step provided by plugin and creating the step code using Jenkins snippet generator. I will use the HTTP Request plugin for this demo.
Step 1: Install the HTTPS request plugin in your Jenkins controller and restart your Jenkins controller.
Step 2: Go inside your pipeline that you have created previously and click “Pipeline Syntax”.
Step 3: Select the plugin from the dropdown and provide the required parameters for the plugin and click “Generate Plugin Script”. You can also further customize the step on the “Advanced” section.
Step 4: Copy the code that was generated in the previous step and put inside a step in your pipeline.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
## ----- ## Steps ## ----- pipeline { agent { label 'test' } stages { stage('http') { steps { sh 'echo "using httpRequest"' httpRequest consoleLogResponseBody: true, url: 'https://reqres.in/api/users' sh 'echo "using curl"' sh 'curl https://reqres.in/api/users' } } } } |
Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully. Below is the console output of the pipeline that we have executed.
Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.