Jenkins cheatsheet
Jenkins: Declarative pipeline basics
7 common Jenkins tasks.
A minimal Jenkinsfile
Put a Jenkinsfile in the root of your repository.
pipeline {
agent any
stages {
stage('Build') {
steps { sh 'make build' }
}
stage('Test') {
steps { sh 'make test' }
}
}
}
Run in a Docker container
Run the pipeline inside a container image, so builds have the tools they need.
agent {
docker { image 'node:24' }
}
Set environment variables
Define variables for the whole pipeline or one stage.
environment {
NODE_ENV = 'production'
BUILD_TAG = "${env.JOB_NAME}-${env.BUILD_NUMBER}"
}
Set pipeline options
Add a timeout, keep fewer builds and stop on the first failure.
options {
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '20'))
disableConcurrentBuilds()
}
Ask for parameters
Add build parameters, which appear as a form when you run the job.
parameters {
string(name: 'VERSION', defaultValue: '1.0.0', description: 'Release version')
booleanParam(name: 'DEPLOY', defaultValue: false)
choice(name: 'ENV', choices: ['staging', 'production'])
}
Run on a schedule
Trigger builds by cron or by polling the repository. H spreads the load.
triggers {
cron('H 2 * * 1-5')
pollSCM('H/15 * * * *')
}
Run a stage conditionally
Skip a stage unless a condition holds.
stage('Deploy') {
when { branch 'main' }
steps { sh './deploy.sh' }
}