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 Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Join us tomorrow at 1 PM EST: "3-Step Approach to Comprehensive Runtime Application Security"
Save your seat
  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.

Sachin Slathia user avatar by
Sachin Slathia
·
Oct. 26, 18 · Tutorial
Like (10)
Save
Tweet
Share
12.43K 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.

Popular on DZone

  • A Brief Overview of the Spring Cloud Framework
  • 7 Awesome Libraries for Java Unit and Integration Testing
  • Differences Between Site Reliability Engineer vs. Software Engineer vs. Cloud Engineer vs. DevOps Engineer
  • Top 10 Secure Coding Practices Every Developer Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: