Jenkins Cheat Sheet
Core reference for Jenkins pipelines, Jenkinsfile syntax, common CLI operations, and plugin-based job configuration.
Declarative Pipeline
Basic structure of a Jenkinsfile using declarative syntax.
pipeline { agent any environment { APP_ENV = 'production' } stages { stage('Build') { steps { sh 'mvn clean package' } } stage('Test') { steps { sh 'mvn test' } } stage('Deploy') { when { branch 'main' } steps { sh './deploy.sh' } } } post { always { junit 'target/surefire-reports/*.xml' } failure { mail to: '[email protected]', subject: 'Build failed' } }}
Scripted Pipeline
Groovy-based scripted syntax for advanced control flow.
node { stage('Checkout') { checkout scm } stage('Build') { try { sh 'make build' } catch (err) { currentBuild.result = 'FAILURE' throw err } } parallel( unit: { sh 'make test-unit' }, integration: { sh 'make test-integration' } )}
Key Directives
Common directives used inside a Jenkinsfile.
- agent- Specifies where the pipeline or stage executes (any, none, label, docker)
- environment- Defines key-value environment variables for the pipeline or a stage
- parameters- Declares build parameters (string, booleanParam, choice) requested at trigger time
- triggers- Defines automatic triggers such as cron or pollSCM
- post- Defines actions run after stage/pipeline completion (always, success, failure, unstable)
- when- Conditionally executes a stage based on branch, expression, or environment
- parallel- Runs multiple stages/steps concurrently
Jenkins CLI
Common jenkins-cli.jar operations.
java -jar jenkins-cli.jar -s http://localhost:8080/ list-jobsjava -jar jenkins-cli.jar -s http://localhost:8080/ build my-job -fjava -jar jenkins-cli.jar -s http://localhost:8080/ console my-jobjava -jar jenkins-cli.jar -s http://localhost:8080/ create-job new-job < config.xmljava -jar jenkins-cli.jar -s http://localhost:8080/ restart
Declarative Matrix Builds
Fan out a stage across combinations of axes, with optional exclusions.
pipeline { agent none stages { stage('Test Matrix') { matrix { axes { axis { name 'PLATFORM'; values 'linux', 'windows' } axis { name 'JDK'; values '11', '17', '21' } } excludes { exclude { axis { name 'PLATFORM'; values 'windows' } axis { name 'JDK'; values '11' } } } agent { label "${PLATFORM}" } stages { stage('Test') { steps { sh "./gradlew test -PjdkVersion=${JDK}" } } } } } }}
Manual Approval & Scoped Credentials
Gate a deploy stage behind human input and inject secrets without leaking them to logs.
stage('Deploy to Prod') { when { branch 'main' } steps { timeout(time: 15, unit: 'MINUTES') { input message: 'Deploy to production?', submitter: 'release-team', ok: 'Ship it' } withCredentials([ usernamePassword(credentialsId: 'prod-registry', usernameVariable: 'REG_USER', passwordVariable: 'REG_PASS'), string(credentialsId: 'deploy-token', variable: 'DEPLOY_TOKEN') ]) { sh 'docker login -u "$REG_USER" -p "$REG_PASS" registry.example.com' sh './deploy.sh --token "$DEPLOY_TOKEN"' } }}
Advanced Pipeline Steps
Built-in steps for coordination and resilience beyond basic sh/stage usage.
- retry(n) { }- Re-runs the enclosed block up to n times on failure before propagating the error
- timeout(time, unit) { }- Aborts the enclosed block if it exceeds the given duration
- waitUntil { }- Polls a condition closure repeatedly until it returns true, useful for readiness checks
- lock('resource')- Serializes access to a shared resource across concurrent builds using the Lockable Resources plugin
- milestone(ordinal)- Cancels older, still-running builds once a newer build passes the same milestone
- stash / unstash- Saves files from one stage/agent and restores them in a later stage, even on a different node
- catchError(buildResult, stageResult)- Marks a build/stage result without failing the pipeline, so later post blocks still run
- @NonCPS- Marks a Groovy method to run outside the CPS transform, required for non-serializable operations like sorting with closures
Multibranch Options & Tools
Pipeline-level options and auto-provisioned tools for multibranch/PR builds.
pipeline { agent any tools { jdk 'temurin-17' maven 'maven-3.9' } options { buildDiscarder(logRotator(numToKeepStr: '20')) disableConcurrentBuilds() timestamps() skipDefaultCheckout(false) } triggers { // rebuild PR branches when the GitHub check is re-requested githubPullRequests() } stages { stage('Build') { steps { sh 'mvn -B verify' } } }}
Use the Jenkinsfile 'agent { docker { image "node:18" } }' per-stage instead of a global agent to keep build environments isolated and reproducible without polluting your Jenkins nodes.