groovyCopyEditpipeline {
agent any
options {
// Discard old builds (keep last 5 builds only)
buildDiscarder(logRotator(numToKeepStr: '5', artifactNumToKeepStr: '2'))
// Prevent concurrent builds of the same pipeline
disableConcurrentBuilds()
// Add timestamps to console output
timestamps()
// ANSI color support for readable output
ansiColor('xterm')
// Timeout entire pipeline if it takes longer than 15 minutes
timeout(time: 15, unit: 'MINUTES')
// Allow retry logic for unstable tasks (used inside script block, not here directly)
// Demonstrated below in the script
// Skip default Git/SCM checkout (we'll do manual checkout)
skipDefaultCheckout()
// Set a quiet period of 10 seconds before the build starts
quietPeriod(10)
// Preserve stashes after the build
preserveStashes()
// If any parallel stage fails, stop all
parallelsAlwaysFailFast()
// Check out to a custom subdirectory
checkoutToSubdirectory('source-code')
// Override default index triggers (useful for multibranch)
overrideIndexTriggers(true)
}
environment {
COLOR = "\u001B[32m"
}
stages {
stage('Manual Checkout') {
steps {
dir('source-code') {
checkout scm
}
}
}
stage('Build') {
steps {
echo "${env.COLOR}Running Build"
sh 'echo "Compiling code..."'
stash includes: '**/*.jar', name: 'build-artifacts'
}
}
stage('Test with Retry') {
steps {
retry(3) {
echo "${env.COLOR}Running flaky test..."
sh 'if [ $((RANDOM % 2)) -eq 0 ]; then exit 1; fi'
}
}
}
stage('Parallel Jobs') {
parallel {
stage('Static Analysis') {
steps {
echo "${env.COLOR}Running static code analysis"
sh 'sleep 3'
}
}
stage('Unit Tests') {
steps {
echo "${env.COLOR}Running unit tests"
sh 'sleep 2'
error("Simulated failure") // Will fail fast all parallel
}
}
}
}
}
post {
always {
echo "Cleaning up..."
deleteDir()
}
}
}