Interview Prep/CI/CD/Jenkins in Production

What does a production Jenkinsfile for a Java service look like?

IntermediateProductionJenkins in Production
Answer
@Library('[email protected]') _
pipeline {
  agent { kubernetes { inheritFrom 'maven-build' } }
  options {
    timeout(time: 30, unit: 'MINUTES')
    buildDiscarder(logRotator(numToKeepStr: '25'))
    disableConcurrentBuilds(abortPrevious: true)
  }
  stages {
    stage('Build & Test') {
      steps { container('maven') { sh 'mvn -B -ntp verify' } }
      post { always { junit 'target/surefire-reports/*.xml' } }
    }
    stage('Quality Gate') {
      steps {
        withSonarQubeEnv('sonar') { container('maven') { sh 'mvn -B sonar:sonar' } }
        timeout(time: 10, unit: 'MINUTES') { waitForQualityGate abortPipeline: true }
      }
    }
    stage('Image') {
      when { branch 'main' }
      steps { buildAndScanImage(name: 'shop/api', tag: env.GIT_COMMIT.take(8)) }
    }
    stage('Deploy staging') {
      when { branch 'main' }
      steps { deployHelm(env: 'staging', tag: env.GIT_COMMIT.take(8)) }
    }
  }
}

The business logic of scanning and deploying lives in the shared library, so this file stays short.

All CI/CD questions

More on Jenkins in Production