Jenkinsfile — Pipeline as Code

Jenkinsfile defines CI/CD pipeline as code (pipeline { agent any; stages { stage("Build") { steps {...} } } post { success/failure } }). Declarative pipeline = easy, structured, uses predefined syntax. Scripted pipeline = Groovy-based, flexible, handles complex logic. 7 main keywords: pipeline, agent, stages, stage, steps, post, environment.

Overview

Pipeline as code

A Jenkinsfile is a text file checked into the source repository that defines the entire CI/CD pipeline as code. Jenkins reads it automatically when a build triggers. Keeping the pipeline in the repository gives version control, peer review and reproducibility.

Declarative vs Scripted

  • Syntax — Structured, opinionated, predefined blocks — Groovy-based, free-form
  • Readability — Easier to read and write — Harder to read
  • Validation — Jenkins validates the structure before running — No validation before run
  • Tooling — Supports a visual pipeline editor — None
  • Strength — Most teams should use it — More power for loops, conditionals and error handling

The seven main keywords

  • pipeline — Top-level wrapper block (Declarative only)
  • agent — Where stages run: any, none, a specific node label, a Kubernetes pod
  • stages — Contains one or more stage blocks
  • stage — A named phase (Build, Test, Deploy); appears in the Jenkins UI
  • steps — The actual commands: sh, echo, script, bat, withCredentials, stash, unstash
  • post — Actions after the pipeline completes: success, failure, always, unstable, changed
  • environment — Key-value env vars available to all stages

Additional blocks

  • when — Conditionally run a stage by branch name, environment variable or expression
  • tools — Install JDK, Maven, Node.js through Jenkins Tool config
  • parallel — Run multiple stages concurrently to speed up pipelines
  • withCredentials — Inject secrets from the Jenkins Credentials store without exposing them in logs

> [!TIP] Put the Deploy stage behind when { branch "main" } so feature branches build and test but never deploy.

Key concepts

Declarative vs Scripted
Declarative: structured, validated, recommended for most pipelines. Uses predefined blocks. Scripted: Groovy, flexible, handles complex logic (dynamic stage generation, loops). Choose Declarative unless you need scripted's power.
agent directive
Defines where the pipeline or a stage runs. agent any: any available agent. agent none: no default (each stage must declare its own). agent { label "docker" }: agents labeled "docker". agent { kubernetes { ... } }: Kubernetes pod spec.
post block
Runs after all stages complete. Conditions: always (always runs), success (only on success), failure (only on failure), unstable (test failures), changed (result changed vs previous build). Good for notifications, cleanup, artifact archiving.
withCredentials
Injects secrets from Jenkins Credentials store into the build environment without logging them. withCredentials([usernamePassword(credentialsId: "docker-hub", usernameVariable: "DOCKER_USER", passwordVariable: "DOCKER_PASS")]) { sh "docker login -u $DOCKER_USER -p $DOCKER_PASS" }
parallel stages
Run multiple stages concurrently. stages { stage("Parallel Tests") { parallel { stage("Unit") { steps { sh "mvn test" } } stage("Lint") { steps { sh "eslint ." } } } } }. Reduces total pipeline time.
when directive
Conditionally execute a stage. when { branch "main" } — only on main branch. when { environment name: "DEPLOY_ENV", value: "prod" }. when { expression { return params.DEPLOY == true } }. Prevents deploy stages from running on feature branches.

When to use it

  • Every Jenkins CI/CD pipeline — Jenkinsfile should always be in the repository, never configured only in the UI
  • Writing pipeline logic for build, test, scan, package, deploy stages
  • Implementing conditional deployments (only deploy from main branch)
  • Running stages in parallel (test + lint + security scan simultaneously)
  • Injecting secrets from Jenkins Credentials store safely into build steps

Interview questions

  • Write a Jenkinsfile with Build, Test, Docker Build, and Deploy stages.
  • What is the difference between declarative and scripted Jenkins pipelines?

Sign up free to read the full topic

More DevOps topics

Start free on Cariara · Coding problems · System design studio