DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Mastering Shift-Left: The Ultimate Guide to Input Validation in Jenkins Pipelines
  • Jenkins Pipelines With Centralized Error Codes and Fail-Fast
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Building Jenkins Infrastructure Pipelines

Trending

  • Why DDoS Protection Is an Architectural Decision for Developers
  • Why Your Test Automation Is Always Behind the Code And the Architecture That Fixes It
  • Architecting Zero-Trust AI Agents: How to Handle Data Safely
  • Why Round-Robin Won't Save You: Load Balancing Challenges in Data Streaming Services With Heterogeneous Traffic
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. DevOps and CI/CD
  4. Structure of a Jenkins Pipeline

Structure of a Jenkins Pipeline

Learn about the basic structure of a Jenkins Declarative Pipeline for CI/CD and DevOps.

By 
Sachin Slathia user avatar
Sachin Slathia
·
Oct. 26, 18 · Tutorial
Likes (10)
Comment
Save
Tweet
Share
14.6K Views

Join the DZone community and get the full member experience.

Join For Free

In this blog, I will be discussing the basic structure of a Jenkins Pipeline, what a pipeline consists of, and the components used in the pipeline.

You can see the basic overview of a pipeline here, on GitHub.

Let's see the components used in this pipeline in detail:

1. pipeline: A block in which we write the main pipeline we want to create.

2. agent: The agent is where we want our whole pipeline or stages to run.

An agent can have parameters like

  • any: If we write agent any then Jenkins will run the whole pipeline or stage on any available agent.
  • none: If we write agent none on top of the pipeline then our global agent is not defined and for every stage, we have to define our agent where we want to run our stage.
  • label: This means running the pipeline or stage in any Jenkins environment with a defined label.
  • docker: To run in a Docker environment.

There are many more agents. If you want to discover all agents, click here. Let's continue talking about pipeline components:

3. stages: This is a block where we write the stage we are writing steps for. A Stages block contains stages like Build, Test, and Deploy.

In this stage, we are authorizing the git repository where my play project is.

4. stage: This is a sub-block inside stages where we define a single stage like Deploy or Build and steps required in each of these stages.

5. steps: In this block, we write all the steps that are required in a stage.

6. post block: A post block is where we write what we want to do when our pipeline has executed.

The post block takes parameters like

  • always: This step will always execute whether our pipeline fails or not.
  • failed: This step will execute only when the current build of our pipeline has failed.
  • changed: This step will execute only when the current state of our pipeline is different from the previous state.

To discover all the steps in the post module, you can refer to the Jenkins documentation here. 

7. triggers: This is used when we want to specify after how much time our pipeline will be triggered. It has parameters like

  • cron: It takes a cron type expression to see after how much time a pipeline should trigger itself. To learn more about cron expressions, see here. 

This will automatically trigger my pipeline every five minutes.

  • poll SCM: This defines a regular interval after which Jenkins will poll our git environment to see whether there is a change in our source code. If yes, then it will trigger our build.

This pipeline will tell Jenkins to continuously poll our GitHub every five minutes to see if there is a change in our source code. If yes then it will start to execute stages in our pipeline.

8. when: To run a particular stage on the given condition, we use the when block.

The compile stage will only run when the branch on GitHub is the master. If it is a branch other than the master, it will skip this step. To learn more about the when clause and what parameters it takes, click here.

9. sequential stages: A Jenkins pipeline can become quite complex sometimes. We can declare stages inside stages; this structure of writing pipelines is known as sequential stages. Nested stages will run in a sequential order.

10. parallel stage: We can even run our stages in parallel on different machines. Just add a parallel {} block in our stages and our stages will run in parallel on different agents.

I hope this blog was helpful in understanding the basic structure of a Jenkins Declarative Pipeline. If you want to explore more about Jenkins pipelines, you can refer to the Jenkins documentation here.

Pipeline (software) Jenkins (software)

Opinions expressed by DZone contributors are their own.

Related

  • Mastering Shift-Left: The Ultimate Guide to Input Validation in Jenkins Pipelines
  • Jenkins Pipelines With Centralized Error Codes and Fail-Fast
  • Optimizing CI/CD Pipeline With Kubernetes, Jenkins, Docker, and Feature Flags
  • Building Jenkins Infrastructure Pipelines

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook