pipline options

Common Jenkins Pipeline Options with Use Cases

Option
Description
Example
Real-Time Use Case

buildDiscarder()

Controls how many builds to keep.

buildDiscarder(logRotator(numToKeepStr: '10'))

Keep last 10 builds to save disk space.

disableConcurrentBuilds()

Prevents concurrent builds.

disableConcurrentBuilds()

Avoid conflicts when deploying to same environment.

timeout()

Sets a timeout for the pipeline or stage.

timeout(time: 10, unit: 'MINUTES')

Fail the build if it hangs beyond 10 minutes.

timestamps()

Adds timestamps to console output.

timestamps()

Helpful in debugging when tracking time.

ansiColor()

Enables ANSI color in logs.

ansiColor('xterm')

Use colorized output from shell scripts.

retry()

Retry block of code if it fails.

retry(3) { sh 'curl http://unstable-api' }

Retry flaky HTTP call.

preserveStashes()

Preserves stashes after the build.

preserveStashes()

Use if you stash in one stage and use in post actions.

skipDefaultCheckout()

Skip automatic SCM checkout.

skipDefaultCheckout()

Use when checkout is manually handled.

overrideIndexTriggers()

Override the default triggers from SCM.

overrideIndexTriggers(true)

Needed in multibranch pipelines sometimes.

quietPeriod()

Wait time before starting a build.

quietPeriod(30)

Useful to batch rapid SCM changes.

parallelsAlwaysFailFast()

Stop all parallel stages if one fails.

parallelsAlwaysFailFast()

Optimize parallel build efficiency.

checkoutToSubdirectory()

Clone repo to custom subdirectory.

checkoutToSubdirectory('source')

Useful for mono-repos or special folder structures.


🧪 Sample Declarative Pipeline with Options

Jenkinsfile
pipeline {
    agent any

    options {
        buildDiscarder(logRotator(numToKeepStr: '5'))
        disableConcurrentBuilds()
        timestamps()
        timeout(time: 15, unit: 'MINUTES')
        ansiColor('xterm')
    }

    stages {
        stage('Build') {
            steps {
                echo "Starting Build..."
                sh 'make build'
            }
        }

        stage('Test') {
            steps {
                echo "Running Tests..."
                sh 'make test'
            }
        }
    }

    post {
        always {
            echo "Cleaning up..."
        }
    }
}

Full Jenkins Pipeline with All Options