Skip to main content
Let’s break down and understand the syntaxes and components used within a Jenkinsfile.
A minimalist slide showing the word "Jenkinsfile" on the left and a white document icon on a teal gradient shape on the right. The slide includes a small "© Copyright KodeKloud" notice at the bottom.
A Jenkinsfile is the cornerstone of your CI/CD automation. It is a text-based script written in a Groovy-like Declarative syntax that defines the stages, agents, environment, and lifecycle of your pipeline. This article covers Declarative Pipelines only. If you need more advanced programmatic control, you can use Scripted Pipelines or 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.
The common Declarative pipeline blocks and directives are described below, with examples.
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

For the full list of Declarative directives, see the official Jenkins Pipeline Syntax documentation: https://www.jenkins.io/doc/book/pipeline/syntax/

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:
Notes on the example above:
  • pipeline {} marks the start of the Declarative pipeline.
  • agent any at the root means the pipeline can run on any available Jenkins agent by default.
  • The Build stage overrides the agent and runs inside a Docker container (maven:3-alpine) so Maven is available without configuring the node.
  • junit collects and publishes test results to Jenkins.
  • The Deploy stage uses a when expression to run only on the main branch.
  • post defines lifecycle hooks executed after the pipeline finishes (e.g., success, failure, always).

Environment variables

Use the environment 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.
Slack notification example using the Pipeline Slack plugin:

Script blocks for advanced logic

Wrap Groovy code in script { ... } to use loops, conditionals, and pipeline APIs:
Script blocks are your gateway to logic that Declarative syntax cannot express natively.

When conditions

when controls whether a stage runs. You can use simple built-in conditions or full expressions: Branch-based condition:
Expression-based condition:

Credentials (secure secrets handling)

Use withCredentials 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)

Use input 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. Use params 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 with parallel 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.

Watch Video