The declarative Jenkinsfile
Structure, options, and catching a broken file before it reaches a build.
cd ~/lab && curl -fsSLO https://secopslog.com/lab-files/ci-jenkins/declarative.tar.gz && tar -xzf declarative.tar.gz, which creates ~/lab/declarative/. SHA-256: 538ac9f35c2653d587495bea6c4954fd07af79b087ec03076d709c5d4c3a1eb4You push a Jenkinsfile change and wait for an agent, a checkout and ten minutes of tests, only to find a misplaced brace on the last stage. Now you fix it and wait again. Pipeline files are code, and like code they can be checked before they run. This lesson covers the shape of a declarative Jenkinsfile, the options worth setting on every pipeline, and how to catch a broken file in a second instead of ten minutes.
The shape of a pipeline
Jenkins has two pipeline syntaxes. Declarative, the pipeline { } form used here, has a fixed structure that Jenkins can validate. Scripted is free-form Groovy. Use declarative unless you have a specific reason not to, and drop into a script { } block for the rare step that needs real logic.
pipeline {agent noneoptions {timeout(time: 10, unit: 'MINUTES')disableConcurrentBuilds()buildDiscarder(logRotator(numToKeepStr: '20'))timestamps()}stages {stage('Test') {agent { label 'linux' }steps {sh 'echo testing on $NODE_NAME'}}}}
| Block | What it does here |
|---|---|
agent none | Takes no executor for the pipeline as a whole, so each stage picks its own |
options | Settings for the entire run |
timeout | Aborts a run that takes over 10 minutes, so a hang can't hold an executor forever |
disableConcurrentBuilds | Stops two builds of this job overlapping |
buildDiscarder | Keeps the last 20 builds, so JENKINS_HOME doesn't grow without limit |
timestamps | Prefixes each console line with the time |
stages / stage | The ordered phases shown in the UI |
steps | The actual work; sh runs a shell command |
The agent none plus a per-stage agent pattern matters once you have approvals or long waits: a stage waiting for human input shouldn't occupy an executor. For a simple pipeline, agent { label 'linux' } at the top is fine, as you saw in earlier lessons.
Lint before you push
Jenkins exposes its validator over REST. Send it a file and it answers without running anything:
Now a file someone wrote in a hurry, with sh directly inside the stage:
pipeline {agent { label 'linux' }stages {stage('Test') {sh 'echo no steps block around me'}}}
Both errors name the fix: steps belong in a steps block. Look at the exit status, though: the validator exits 0 even for an invalid file, because the HTTP request itself succeeded. To use this in a pre-commit hook or CI check, test the text for successfully validated and not the exit code. The linter needs a running Jenkins with your plugins, since available steps depend on them.
One more structural rule the linter enforces: a stage does its work in exactly one form. It holds steps, nested stages, parallel branches or a matrix, never two of them. This file puts steps and parallel in the same stage:
pipeline {agent { label 'linux' }stages {stage('Test') {steps {sh 'echo unit'}parallel {stage('Lint') { steps { sh 'echo lint' } }}}}}
To run a step before parallel branches, give it its own stage first.
A good run, then the same mistake without the linter
Timeout set to expire in 10 min is the timeout option taking effect, and each line now has a timestamp from timestamps(). Now push the broken file and skip the lint step:
It fails with the same messages, just wrapped in a stack trace, and before any stage starts: no agent was used and no step ran. A declarative file is compiled as a whole first, which is why a mistake in the last stage still fails at the start of the build. Read the first WorkflowScript: line and ignore the Java frames below it.
Validity isn't correctness, though. The linter checks structure and doesn't run anything, so it can't tell you that a shell command is wrong or a credential ID is misspelled. Those still appear only when the step runs.
A post block with no node
agent none has a consequence for pipeline-level post blocks: they run outside every stage, so no agent is allocated for them. A step that needs a workspace, such as sh, has nowhere to run. This file is valid and its stage passes:
pipeline {agent nonestages {stage('Test') {agent { label 'linux' }steps {sh 'echo testing on $NODE_NAME'}}}post {always {sh 'rm -rf build'}}}
The Test stage ran on lab-agent and passed. Then the always block failed with Attempted to execute a step that requires a node context while ‘agent none’ was specified, and the whole build is FAILURE. With agent none, put cleanup in the stage's own post block, which runs on that stage's agent, or use cleanWs() there.
Hands-on
Start a fresh lab with the original app/Jenkinsfile (git -C app checkout HEAD~2 -- Jenkinsfile brings it back). Delete the closing brace of the options block in app/Jenkinsfile and run the linter, then restore it. Then add a stage named Lint with a misspelled step, shh 'true'. Does the linter catch it? Run the build and find out at which point the misspelled step fails. Finish with ./down.sh.
What you should see: without the brace, the linter reports expecting '}', found '' at line 17, the end of the file, not at line 8 where the brace is missing, so look above the line it names. The misspelled step passes the linter with Jenkinsfile successfully validated. The build fails only when it reaches the step, after an agent was allocated, with No such DSL method 'shh' found among steps, and the Test stage is skipped. The linter checks structure, not step names.
Remember
Put timeout and buildDiscarder in every pipeline, because the default for both is forever. Lint before pushing and check the response text, not the exit code. Only a run proves the commands work.
curl -f and blocks the commit on a non-zero exit. Developers report that invalid Jenkinsfiles get through. Why?-f works with POST. The request is sent and answered.successfully validated.agent none with an agent on each working stage?WorkflowScript: line has the reason.