Skip to main content
A Jenkinsfile is a Groovy-like, text-based script that defines and automates the stages of your CI/CD pipeline. In this guide, we’ll explore the core declarative directives and components you need to build robust, maintainable pipelines.

Typical Pipeline Structure

A declarative Jenkinsfile usually follows this pattern:
  1. Source: Checkout code from GitHub, Bitbucket, etc.
  2. Build: Compile, build, and package your application.
  3. Test: Execute unit tests, integration tests, and other suites.
  4. Deploy: Push artifacts to staging or production environments.
Here’s a minimal example illustrating these concepts:

Key Declarative Directives

environment Directive

Set environment variables globally or override them per-stage:

post Directive

Define actions to run after the entire pipeline finishes, based on the outcome:

script Directive

Use script blocks to include complex Groovy logic:
Use script sparingly. Most tasks can be handled with declarative steps or existing plugins.

when Directive

Control stage execution based on conditions (branches, environment, tags):

credentials Directive

Inject Jenkins-managed credentials securely:
Never print credentials or secrets to the console. Always use the Jenkins credentials store and the appropriate binding methods.

input Directive

Pause the pipeline for manual approval:

parameters Directive

Allow customization of pipeline runs:

stash / unstash Directives

Share files between stages or nodes:

parallel Directive

Run multiple branches in parallel to speed up the pipeline:

Further Reading and References

Watch Video