Jenkins pipeline part 2 – stages and steps

Jenkins pipeline part 2 – stages and steps

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.

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.

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.

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.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 2 – stages and steps

Sequential Stages Example:

Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 2 – stages and steps

Parallel Stages Example:`

Create a new pipeline in your Jenkins controller server using below Jenkinsfile. Replace the label as per your Jenkins configuration.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 2 – stages and steps

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.

Trigger the pipeline depending on you pipeline setup. If everything goes right your pipeline will get executed successfully.

Jenkins pipeline part 2 – stages and steps

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.

Jenkins pipeline part 2 – stages and steps

Step 2: Go inside your pipeline that you have created previously and click “Pipeline Syntax”.

Jenkins pipeline part 2 – stages and steps

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.

Jenkins pipeline part 2 – stages and steps

Step 4: Copy the code that was generated in the previous step and put inside a step in your pipeline.

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.

Jenkins pipeline part 2 – stages and steps

Hope you have enjoyed this article, to get more details on Jenkins, please refer below Jenkins official documentation.

https://www.jenkins.io/doc/

Leave a Reply

Close Menu