Developer and Text Tools

Declarative Jenkinsfile Basics: Agents, Stages, Parallel and Post

A practical tour of the declarative Jenkinsfile: choosing an agent, using credentials safely, parallel stages, when conditions and post actions.

Saizul Amin
Saizul Amin7 Nov 2026 · 4 min read
Declarative Jenkinsfile Basics: Agents, Stages, Parallel and Post

Jenkins pipelines have been written as code for years, yet many teams still copy an old Jenkinsfile from another repository and adjust it by trial and error. The declarative syntax is strict about where each section goes, and a misplaced block produces an error that points to the wrong line. This guide explains the parts that matter and how the Jenkinsfile Pipeline Generator writes them for you.

The shape of a declarative pipeline

Everything sits inside one pipeline block. Directly inside it you may use agent, environment, options, parameters, triggers, tools, stages and post, and nothing else. The stages block holds stage blocks, and each stage must contain exactly one of steps, parallel, a nested stages block or matrix. Plain Groovy such as if statements or def variables is not allowed in steps; it has to be wrapped in a script block. The generator follows these rules, and its checker flags files that break them.

Choosing an agent

The agent decides where the work runs. agent any takes the first free executor. A label limits the build to nodes with that label, for example linux or docker. A Docker agent pulls an image and runs the steps inside it, which keeps tools consistent between machines; run arguments can mount a cache directory so dependencies are not downloaded every time. A Dockerfile agent builds the image from your repository first, and a Kubernetes agent starts a pod from a template.

Setting agent none at the top is useful when stages need different machines. Each stage then declares its own agent, and no executor is held while the pipeline waits, for example at an approval step. The generator warns when a stage has steps but no agent in that setup.

Environment and credentials

Environment variables can be set for the whole pipeline or one stage. For secrets, write NAME = credentials('id'), where the id refers to a credential stored in Jenkins. For a username and password credential, Jenkins also defines NAME_USR and NAME_PSW. Inside a step, withCredentials gives the same values for a smaller block, and sshagent loads an SSH key.

One detail causes real leaks. In Groovy, a double-quoted string is interpolated before the shell sees it, so sh "curl -u admin:${API}" puts the secret into the command line Jenkins records. Write the step with single quotes, sh 'curl -u admin:$API', and the shell expands the variable while Jenkins masks it in the log. The checker reports double-quoted steps that use a credential variable.

Options that save trouble later

A timeout stops a stuck build from holding an executor forever. buildDiscarder with logRotator keeps only the last builds and frees disk space. timestamps adds a time to each log line, and disableConcurrentBuilds prevents two builds of the same branch from deploying at once. The generator includes these four by default because almost every pipeline benefits from them.

Parallel stages

A stage can contain a parallel block of stages that run at the same time, such as lint, unit tests and browser tests. Set failFast true and Jenkins stops the other branches as soon as one fails, which saves time. A parallel branch cannot contain another parallel block; the checker catches this. In the generator you press Split into parallel branches on a stage and then edit each branch like a normal stage.

When, input and post

A when block decides whether a stage runs. branch takes a glob such as release/*, buildingTag() matches tag builds, changeset matches changed file paths and expression runs a Groovy condition, such as a boolean parameter. beforeAgent true checks the condition before an agent is allocated, which avoids starting a container just to skip the stage.

An input block pauses the stage until someone approves it, which is common before a production deploy. Post blocks run after a stage or the whole pipeline: always, success, failure, unstable, changed, fixed, regression, aborted, unsuccessful and cleanup. There is no finally condition, a mistake that many people make when coming from other languages.

Previewing the stage view

The stage view in the tool draws the pipeline the way Jenkins shows it, with parallel branches stacked in one column and a final post box. Type a branch name and stages whose when condition does not match are greyed out, with the reason listed below. With the Node.js template, a feature branch skips the Publish stage, while main runs it and waits for approval.

Importing an existing Jenkinsfile

If you already have a declarative Jenkinsfile, paste it into check mode. The checker reports unbalanced braces with the line where the block opened, duplicate stage names, unknown when conditions, sections in the wrong place and secrets written into the file. Import then reads stages, steps, agents, environment, parameters, triggers and post actions back into the form, so you can tidy it up and regenerate a clean file. Scripted pipelines that start with node are recognised and left alone, because they are free-form Groovy.

The generator only writes text in your browser. It cannot know which plugins your controller has, so test the result with a replay or the declarative linter on your Jenkins server before merging it.

Completely freeNo cost, no sign-up
Files stay privateEverything runs in your browser
Easy in your languageAvailable in many languages