These directives turn a basic pipeline into one that is safe to run unattended and adaptable per build.
options configure the whole pipeline (some also work per stage):
timeout(time: 30, unit: 'MINUTES'): abort hung builds.buildDiscarder(logRotator(numToKeepStr: '30')): keep only recent builds and artifacts.disableConcurrentBuilds(): queue instead of running two builds of the job at once.retry(2),skipDefaultCheckout(),timestamps()(Timestamper plugin).
parameters define inputs shown on Build with Parameters: string, booleanParam, choice, text, password. Read them as params.NAME. Note the first build after adding parameters runs before Jenkins knows about them.
triggers start builds automatically: cron('H 2 * * 1-5') or pollSCM(...), plus upstream jobs. The H symbol hashes the job name into a stable value, spreading jobs across the period instead of all starting at minute 0.
when decides whether a stage runs: branch 'main', tag 'v*', changeRequest(), environment name: 'DEPLOY', value: 'true', expression { params.RUN_E2E }, combined with allOf, anyOf and not. Add beforeAgent true so the condition is evaluated before allocating an agent.
The input step pauses for a human decision, such as approving a production deploy; run it outside any agent so it holds no executor.
Example
pipeline {
agent any
options {
timeout(time: 30, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '30'))
disableConcurrentBuilds()
}
parameters {
choice(name: 'TARGET', choices: ['staging', 'production'])
booleanParam(name: 'RUN_E2E', defaultValue: false)
}
triggers { cron('H 2 * * 1-5') } // nightly on weekdays
stages {
stage('Build') { steps { sh 'make' } }
stage('E2E') {
when { expression { params.RUN_E2E } }
steps { sh 'make e2e' }
}
stage('Deploy') {
when { allOf { branch 'main'; expression { params.TARGET == 'production' } } }
steps { sh "./deploy.sh ${params.TARGET}" }
}
}
}Remember
- options: timeout, buildDiscarder and disableConcurrentBuilds belong in most pipelines
- params.NAME reads build parameters
- when { } skips stages; H spreads cron schedules
Watch out for
- Writing cron('0 * * * *') on many jobs so they all start at the same minute; use H.