Opscraft
Jenkinsfile · Lesson 8 of 10 · 3 min read

A complete production Jenkinsfile

Enlarge diagramChecks (parallel)unit tests · static analysisBuild & push imageKaniko to ECR, tag = commitScan imagefail on criticalPromote to stagingGitOps commitApprove productioninput, agent none, timeoutPromote to productionsame tag, prod overlay123456
Figure 1. The stages of the complete production Jenkinsfile in this lesson.
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 none with 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 lock so two builds can't race on the same overlay; milestone prevents an older approved build overtaking a newer one.
  • abortPrevious cancels superseded PR builds, saving agent capacity.
  • Post-deploy verification and notifications on failure/fix only.