Jenkinsfile · Lesson 8 of 10 · 3 min read
A complete production Jenkinsfile
Enlarge diagram
Swipe sideways to see all of it, or tap to open it full screen.
@Library('platform-lib@v2') _
pipeline {
agent none
options {
timeout(time: 60, unit: 'MINUTES')
buildDiscarder(logRotator(numToKeepStr: '30'))
disableConcurrentBuilds(abortPrevious: true)
timestamps()
}
environment {
AWS_REGION = 'ap-south-1'
REGISTRY = '123456789012.dkr.ecr.ap-south-1.amazonaws.com'
APP = 'orders'
}
stages {
stage('Checks') {
failFast true
parallel {
stage('Unit tests') {
agent { kubernetes { yamlFile 'ci/pod-maven.yaml'; defaultContainer 'maven' } }
steps { sh 'mvn -B -ntp verify' }
post { always { junit '**/surefire-reports/*.xml' } }
}
stage('Static analysis') {
agent { kubernetes { yamlFile 'ci/pod-maven.yaml'; defaultContainer 'maven' } }
steps {
withSonarQubeEnv('sonar') { sh 'mvn -B -ntp sonar:sonar' }
timeout(time: 10, unit: 'MINUTES') { waitForQualityGate abortPipeline: true }
}
}
}
}
stage('Build & push image') {
when { anyOf { branch 'main'; changeRequest() } }
agent { kubernetes { yamlFile 'ci/pod-kaniko.yaml'; defaultContainer 'kaniko' } } // pod SA uses IRSA for ECR push
steps {
script { env.TAG = env.GIT_COMMIT.take(7) }
sh '''/kaniko/executor --context . \
--destination $REGISTRY/$APP:$TAG \
--cache=true --cache-repo $REGISTRY/$APP/cache'''
}
}
stage('Scan image') {
when { anyOf { branch 'main'; changeRequest() } }
agent { kubernetes { yamlFile 'ci/pod-tools.yaml'; defaultContainer 'tools' } }
steps { sh 'trivy image --exit-code 1 --severity CRITICAL --ignore-unfixed $REGISTRY/$APP:$TAG' }
}
stage('Promote to staging') {
when { branch 'main' }
agent { kubernetes { yamlFile 'ci/pod-tools.yaml'; defaultContainer 'tools' } }
steps {
lock('gitops-orders') { gitopsPromote(app: env.APP, env: 'staging', tag: env.TAG) } // library step
smokeTest(url: 'https://orders.staging.acme.dev/health') // library step
}
}
stage('Approve production') {
when { branch 'main'; beforeInput true }
options { timeout(time: 1, unit: 'DAYS') }
input { message 'Promote to production?'; ok 'Promote'; submitter 'release-managers' }
steps { milestone(1) }
}
stage('Promote to production') {
when { branch 'main' }
agent { kubernetes { yamlFile 'ci/pod-tools.yaml'; defaultContainer 'tools' } }
steps {
lock('gitops-orders') { gitopsPromote(app: env.APP, env: 'prod', tag: env.TAG) }
verifySlo(app: env.APP, env: 'prod', minutes: 15) // library step: abort + revert on SLO breach
}
}
}
post {
failure { notify.failure() }
fixed { notify.fixed() }
}
}
What makes it senior-grade:
agent nonewith right-sized per-stage pods; no executor held during approval.- Parallel checks with fail-fast; quality gate and image scan block later stages.
- Image built once, tagged with the commit SHA, promoted unchanged staging → prod.
- No Docker socket — Kaniko — and no stored AWS keys (IRSA on the pod's service account).
- GitOps promotion with a
lockso two builds can't race on the same overlay;milestoneprevents an older approved build overtaking a newer one. abortPreviouscancels superseded PR builds, saving agent capacity.- Post-deploy verification and notifications on failure/fix only.