
script blocks inside Declarative Pipelines.
At a high level, a Jenkinsfile organizes work into distinct stages such as:
- Checkout: retrieve source code from GitHub, GitLab, Bitbucket, etc.
- Build: compile, build, and package the application.
- Test: run unit, integration, and other tests.
- Deploy: publish artifacts to staging/production environments.
This guide focuses on Declarative Pipelines. Declarative syntax provides clearer structure for CI/CD flows; use
script { ... } blocks when you need Groovy logic for advanced conditions or iteration.Common Declarative blocks and directives
| Directive | Purpose | Example / Notes |
|---|---|---|
pipeline | Root block that defines the pipeline | pipeline { ... } |
agent | Where to run the pipeline or a stage (e.g. any, label, docker) | agent any or agent { docker { image 'maven:3-alpine' } } |
stages / stage | Group and name work stages | stages { stage('Build') { steps { ... } } } |
steps | Actual commands or plugin steps executed inside a stage | steps { sh 'mvn -q package' } |
environment | Define env vars at pipeline or stage scope | environment { VAR = 'value' } |
parameters | Parameterize pipeline runs | parameters { string(name: 'ENV', defaultValue: 'dev') } |
post | Actions to run after pipeline/stage completion | post { success { ... } failure { ... } } |
when | Conditional execution of a stage | when { branch 'main' } or when { expression { env.BRANCH_NAME == 'main' } } |
tools | Declare tool installations (e.g., JDK, Maven) | tools { maven 'M3' } |
options | Pipeline options like timestamps, timeout | options { timeout(time: 1, unit: 'HOURS') } |
stash / unstash | Pass files between stages/nodes | stash name: 'app', includes: 'target/*.jar' |
parallel | Run multiple branches concurrently inside a stage | parallel { stage('A') { ... } stage('B') { ... } } |
Example: Basic Declarative Pipeline
A compact, practical example illustrating agents, stages, a Docker-based build step, test publishing, branch-based deploy condition, and post actions:pipeline {}marks the start of the Declarative pipeline.agent anyat the root means the pipeline can run on any available Jenkins agent by default.- The
Buildstage overrides the agent and runs inside a Docker container (maven:3-alpine) so Maven is available without configuring the node. junitcollects and publishes test results to Jenkins.- The
Deploystage uses awhenexpression to run only on themainbranch. postdefines lifecycle hooks executed after the pipeline finishes (e.g.,success,failure,always).
Environment variables
Use theenvironment directive to declare variables available to all stages (or declare them inside individual stages to limit scope):
Post actions and notifications
post runs after the pipeline or a stage completes. It’s commonly used for notifications, artifact archiving, and cleanup.
Script blocks for advanced logic
Wrap Groovy code inscript { ... } to use loops, conditionals, and pipeline APIs:
When conditions
when controls whether a stage runs. You can use simple built-in conditions or full expressions:
Branch-based condition:
Credentials (secure secrets handling)
UsewithCredentials to inject credentials into the build environment without exposing them in logs.
Never print secrets to logs. Use credentials only in the steps that require them and prefer credential-binding plugins over hard-coding.
Interactive input (manual approvals)
Useinput to pause the pipeline and require a human confirmation for sensitive steps such as production deploys:
Parameters
Define parameters to run pipelines with configurable values. Useparams to access them at runtime:
Stash and unstash (passing files between stages/nodes)
stash stores files temporarily so another stage (possibly on a different agent) can unstash them:
Parallel stages
Run independent tasks concurrently withparallel to reduce overall pipeline time:
Putting it all together
Declarative pipelines support many directives—agent, environment, parameters, stages, post, options, tools, triggers, and more. The examples above cover the most commonly used features. Use script {} for advanced Groovy logic and combine plugins (JUnit, Slack, Docker, Credentials, etc.) to integrate Jenkins with the rest of your toolchain.
This guide gives you a solid foundation to author and maintain Declarative Jenkinsfiles for CI/CD. Explore the official docs and plugin pages to extend pipeline capabilities based on your CI requirements.
Links and references
- Jenkins Pipeline Syntax: https://www.jenkins.io/doc/book/pipeline/syntax/
- Jenkins Declarative Pipeline: https://www.jenkins.io/doc/book/pipeline/development/
- Credentials Binding Plugin: https://plugins.jenkins.io/credentials-binding/
- Jenkins Plugins: https://plugins.jenkins.io/