Quick-reference tmux cheatsheet: sessions, windows, panes, copy mode, synchronize-panes, .tmux.conf …
Jenkins Cheatsheet Jenkins Cheatsheet

Summary
I write Jenkinsfiles weekly and still forget the exact when syntax every single time, so this page stays open in a pinned tab. Everything below is copy-paste ready and tested against a recent Jenkins LTS. Most of my pipelines end up calling Ansible or building Docker images, so those two sheets pair well with this one.
Declarative Pipeline Skeleton
Docs: Pipeline syntax (Jenkins docs)
Expand your knowledge with Kubernetes Cheatsheet
pipeline {
agent any
options {
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20'))
disableConcurrentBuilds()
timestamps()
}
environment {
APP_ENV = 'staging'
}
stages {
stage('Build') {
steps {
sh 'make build'
}
}
stage('Test') {
steps {
sh 'make test'
}
}
stage('Deploy') {
steps {
sh './deploy.sh'
}
}
}
post {
always { cleanWs() }
success { echo 'Build passed' }
failure { mail to: 'team@example.com', subject: "FAILED: ${env.JOB_NAME} #${env.BUILD_NUMBER}", body: "${env.BUILD_URL}" }
}
}
Scripted vs Declarative
| Declarative | Scripted | |
|---|---|---|
| Syntax | pipeline { ... } | node { ... } |
| Structure | Opinionated, validated | Free-form Groovy |
post / when / options | Built-in | DIY with try/catch |
| Best for | 95% of jobs | Complex dynamic logic |
Rule of thumb: start declarative, drop into a script { } block only when you need loops or dynamic stage generation.
Deepen your understanding in Bash CLI One-Liners Cheatsheet
Environment & Credentials
environment {
// From Jenkins credentials store (ID must exist)
AWS_CREDS = credentials('aws-prod') // sets AWS_CREDS_USR / AWS_CREDS_PSW
API_TOKEN = credentials('api-token-secret') // secret text -> single var
}
// Scoped binding inside steps - preferred for secrets
steps {
withCredentials([
usernamePassword(credentialsId: 'nexus', usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS'),
string(credentialsId: 'slack-token', variable: 'SLACK_TOKEN'),
file(credentialsId: 'kubeconfig-prod', variable: 'KUBECONFIG'),
sshUserPrivateKey(credentialsId: 'deploy-key', keyFileVariable: 'SSH_KEY')
]) {
sh 'curl -u "$NEXUS_USER:$NEXUS_PASS" https://nexus.internal/upload'
}
}
Always use single quotes in sh so secrets are expanded by the shell, not interpolated into the Groovy string (which leaks them to the build log).
Explore this further in tmux Cheatsheet
Parameters
Docs: Pipeline syntax: parameters (Jenkins docs)
Discover related concepts in Ansible Cheatsheet
parameters {
string(name: 'VERSION', defaultValue: '1.0.0', description: 'Release version')
choice(name: 'ENV', choices: ['dev', 'staging', 'prod'], description: 'Target environment')
booleanParam(name: 'SKIP_TESTS', defaultValue: false)
text(name: 'RELEASE_NOTES', defaultValue: '')
}
// Access: params.VERSION, params.ENV, params.SKIP_TESTS
when Conditions
Docs: Pipeline syntax: when (Jenkins docs)
Uncover more details in Bash sed & awk Cheatsheet
stage('Deploy Prod') {
when {
allOf {
branch 'main'
environment name: 'APP_ENV', value: 'prod'
expression { return params.ENV == 'prod' }
not { changeRequest() } // skip on PR builds
}
}
steps { sh './deploy.sh prod' }
}
| Condition | Matches when |
|---|---|
branch 'main' | Branch name matches (multibranch) |
tag 'v*' | Build is for a matching tag |
changeRequest() | Build is a PR |
changeset '**/*.tf' | Commit touched matching files |
buildingTag() | Any tag build |
triggeredBy 'TimerTrigger' | Started by cron |
anyOf { } / allOf { } / not { } | Boolean combinators |
Parallel Stages
Docs: Pipeline syntax: parallel (Jenkins docs)
Journey deeper into this topic with Ansible Cheatsheet
stage('Quality Gates') {
parallel {
stage('Lint') { steps { sh 'make lint' } }
stage('Unit') { steps { sh 'make unit' } }
stage('Integration') {
agent { label 'docker' }
steps { sh 'make integration' }
}
}
}
// failFast true -> abort siblings on first failure (put inside options or parallel)
Shared Libraries
// Jenkinsfile - library configured globally as "mylib"
@Library('mylib@main') _
standardBuild(language: 'go', notify: '#builds') // vars/standardBuild.groovy
Repo layout:
Enrich your learning with Docker Cheatsheet
(root)
├── vars/standardBuild.groovy # def call(Map config) { ... } - callable as a step
├── src/org/acme/Utils.groovy # classes, import as org.acme.Utils
└── resources/templates/pod.yaml # libraryResource 'templates/pod.yaml'
Stash/Unstash & Artifacts
stage('Build') { steps { sh 'make dist'; stash name: 'dist', includes: 'dist/**' } }
stage('Deploy') { agent { label 'deployer' }
steps { unstash 'dist'; sh './push.sh' } }
// Keep artifacts on the build page
archiveArtifacts artifacts: 'dist/*.tar.gz', fingerprint: true, allowEmptyArchive: false
junit 'reports/**/*.xml'
Stash = pass files between stages/agents in the same build (temporary). Artifacts = keep files after the build (permanent, per retention policy).
Gain comprehensive insights from AWS CloudFormation Cheatsheet
Triggers
triggers {
cron('H 2 * * 1-5') // weeknights, H spreads load
pollSCM('H/5 * * * *') // poll every ~5 min
upstream(upstreamProjects: 'lib-build', threshold: hudson.model.Result.SUCCESS)
}
Webhook (preferred over polling): GitHub webhook to https://jenkins.example.com/github-webhook/, GitLab to /project/<job>. Generic jobs: enable “Trigger builds remotely” and hit JENKINS_URL/job/NAME/build?token=TOKEN.
Master this concept through Kubernetes Cheatsheet
CLI & jenkins-cli.jar
curl -O https://jenkins.example.com/jnlpJars/jenkins-cli.jar # from your own controller
alias jcli='java -jar jenkins-cli.jar -s https://jenkins.example.com -auth user:apitoken'
jcli list-jobs
jcli build my-job -p ENV=staging -f -v # -f wait, -v stream console
jcli console my-job 42 # console log of build 42
jcli reload-configuration
jcli safe-restart
jcli install-plugin blueocean -deploy
Blue Ocean lives at /blue. Best for visualising parallel stages and PR pipelines; classic UI is still where all the config lives.
Delve into specifics at Bash CLI One-Liners Cheatsheet
Common Groovy Snippets
Docs: Pipeline Utility Steps reference (Jenkins docs)
0Deepen your understanding in Bash CLI One-Liners Cheatsheet
// Capture command output
def sha = sh(script: 'git rev-parse --short HEAD', returnStdout: true).trim()
// Exit code without failing the stage
def rc = sh(script: './smoke-test.sh', returnStatus: true)
if (rc != 0) { unstable('smoke tests failed') }
// JSON in/out (Pipeline Utility Steps plugin)
def cfg = readJSON file: 'config.json'
echo "replicas: ${cfg.deploy.replicas}"
writeJSON file: 'out.json', json: [version: sha, env: params.ENV]
// YAML too
def vals = readYaml file: 'values.yaml'
// Retry and timeout wrappers
retry(3) { sh './flaky-fetch.sh' }
timeout(time: 5, unit: 'MINUTES') { input message: 'Deploy to prod?' }
Agent Labels & Docker Agents
agent { label 'linux && docker' } // label expression
agent {
docker {
image 'golang:1.22'
args '-v /var/cache/go:/go/pkg'
reuseNode true
}
}
agent {
dockerfile { filename 'ci/Dockerfile'; label 'docker' }
}
agent none at the top + per-stage agents = no idle executor held between stages.
Deepen your understanding in Bash CLI One-Liners Cheatsheet
H instead of fixed minutes in cron triggers (H 2 * * * not 0 2 * * *). Jenkins hashes the job name to pick a consistent minute, so a hundred nightly jobs don’t all fire at 02:00 and flatten your agents.Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
No such DSL method 'x' | Missing plugin or typo | Install plugin (e.g. Pipeline Utility Steps), check spelling |
| Build stuck “Waiting for next available executor” | No agent matches label | Check label expression, agent online status, executor count |
script not permitted to use method | Groovy sandbox block | Approve in Manage Jenkins → In-process Script Approval, or move logic to a shared library |
Secrets appear as **** but wrong value | Groovy string interpolation of creds | Use single-quoted sh strings, let the shell expand $VAR |
stash not found in later stage | Different build or expired | Stash only lives within one build; use archiveArtifacts/copyArtifacts across builds |
| Webhook fires but no build | Branch not discovered | Re-scan multibranch pipeline, check branch filters |
Jenkinsfile not found | Wrong script path or lightweight checkout issue | Fix “Script Path” in job config, verify branch |
| Docker agent: permission denied on socket | Jenkins user not in docker group | Add user to docker group on agent, restart agent |
| Workspace out of disk | No cleanup, fat workspaces | cleanWs() in post { always }, buildDiscarder in options |
Related Cheatsheets
- Docker: image builds, registries, and the run flags behind every
agent { docker { ... } }stage on this page - Ansible: playbooks, inventories, and Vault, which is my usual deploy step invoked from a Jenkins stage
- Python Basics & CLI: argparse, venv, and
json.toolfor the helper scripts your pipelines shell out to
More of these live in the DevOps Tooling group, and the full cheatsheet library has everything else.
Similar Articles
Related Content
More from devops
Docker cheatsheet with the commands I use daily as a DevOps engineer: image builds, run flags, …
Quick-reference Bash basics cheatsheet: variables, parameter expansion, conditionals, loops, arrays, …
You Might Also Like
No related topic suggestions found.
