github.com/drone/go-convert@v0.0.0-20240307072510-6bd371c65e61/convert/jenkins/testdata/Jenkinsfile.12 (about)

     1  #!groovy
     2  
     3  @Library('github.com/triologygmbh/jenkinsfile@ad12c8a9') _
     4  
     5  node('docker') { // Require a build executor with docker (label)
     6  
     7      catchError {
     8  
     9          properties([
    10                  pipelineTriggers(createPipelineTriggers()),
    11                  disableConcurrentBuilds(),
    12                  buildDiscarder(logRotator(numToKeepStr: '10'))
    13          ])
    14  
    15          kubectlImage = 'lachlanevenson/k8s-kubectl:v1.19.3'
    16          helmImage = 'lachlanevenson/k8s-helm:v3.4.1'
    17          
    18          stage('Checkout') {
    19              checkout scm
    20          }
    21  
    22          String versionName = createVersion()
    23  
    24          stage('Build') {
    25              mvn "clean install -DskipTests -Parq-wildfly-swarm -Drevision=${versionName}"
    26              archiveArtifacts '**/target/*.*ar'
    27          }
    28  
    29          parallel(
    30                  unitTest: {
    31                      stage('Unit Test') {
    32                          mvn "test -Drevision=${versionName}"
    33                      }
    34                  },
    35                  integrationTest: {
    36                      stage('Integration Test') {
    37                          if (isTimeTriggeredBuild()) {
    38                              mvn "verify -DskipUnitTests -Parq-wildfly-swarm -Drevision=${versionName}"
    39                          }
    40                      }
    41                  }
    42          )
    43  
    44          stage('Statical Code Analysis') {
    45              analyzeWithSonarQubeAndWaitForQualityGoal()
    46          }
    47  
    48          stage('Deploy') {
    49              if (currentBuild.currentResult == 'SUCCESS') {
    50  
    51                  // Comment in and out some things so this deploys branch 12-x for this demo.
    52                  // In real world projects its good practice to deploy only develop and main branches
    53                  if (env.BRANCH_NAME == "main") {
    54                      //deployToKubernetes('production', versionName, 'kubeconfig-prod')
    55                  } else { //if (env.BRANCH_NAME == 'develop') {
    56                      deployToKubernetes('staging', versionName, 'kubeconfig-oss-deployer')
    57                  }
    58              }
    59          }
    60      }
    61  
    62      // Archive Unit and integration test results, if any
    63      junit allowEmptyResults: true,
    64              testResults: '**/target/surefire-reports/TEST-*.xml, **/target/failsafe-reports/*.xml'
    65  
    66      mailIfStatusChanged env.EMAIL_RECIPIENTS
    67  }
    68  
    69  def createPipelineTriggers() {
    70      if (env.BRANCH_NAME == 'master') {
    71          // Run a nightly only for master
    72          return [cron('H H(0-3) * * 1-5')]
    73      }
    74      return []
    75  }
    76  
    77  String createVersion() {
    78      // E.g. "201708140933"
    79      String versionName = "${new Date().format('yyyyMMddHHmm')}"
    80  
    81      if (env.BRANCH_NAME != "master") {
    82          versionName += '-SNAPSHOT'
    83      }
    84      echo "Building version ${versionName} on branch ${env.BRANCH_NAME}"
    85      currentBuild.description = versionName
    86      return versionName
    87  }
    88  
    89  void deployToKubernetes(String stage, String versionName, String credentialsId) {
    90      String imageRepo = "cloudogu/kitchensink"
    91      String imageName = "${imageRepo}:${versionName}"
    92  
    93      docker.withRegistry('', 'hub.docker.com-cesmarvin') {
    94          docker.build(imageName, '.').push()
    95      }
    96  
    97      String helmReleaseName = 'kitchensink'
    98      String chartFolder = "chart"
    99      String helmFlags = "--values=chart/values-${stage}.yaml --set image.repository=${imageRepo} --set image.tag=${versionName}"
   100      String helmResourceType = 'Deployment'
   101      String helmResourceName = ''
   102  
   103      docker.image(helmImage).inside("--entrypoint=''") {
   104          withCredentials([file(credentialsId: credentialsId, variable: 'KUBECONFIG')]) {
   105              sh "helm upgrade --install ${helmFlags} ${helmReleaseName} ${chartFolder}"
   106              helmResourceName = getHelmChartResourceName(helmResourceType, helmReleaseName, helmFlags, chartFolder)
   107          }
   108      }
   109  
   110      waitForRolloutToComplete(credentialsId, helmResourceType, helmResourceName)
   111  }
   112  
   113  void analyzeWithSonarQubeAndWaitForQualityGoal() {
   114      withSonarQubeEnv('sonarcloud.io-cloudogu') {
   115          mvn "${SONAR_MAVEN_GOAL} -Dsonar.host.url=${SONAR_HOST_URL} -Dsonar.login=${SONAR_AUTH_TOKEN} " +
   116                  // Here, we could define e.g. sonar.organization, needed for sonarcloud.io
   117                  "${SONAR_EXTRA_PROPS} " +
   118                  // Addionally needed when using the branch plugin (e.g. on sonarcloud.io)
   119                  "-Dsonar.branch.name=${BRANCH_NAME} -Dsonar.branch.target=master"
   120      }
   121      timeout(time: 10, unit: 'MINUTES') { // Normally, this takes only some ms. sonarcloud.io might take minutes, though :-(
   122          def qg = waitForQualityGate()
   123          if (qg.status != 'OK') {
   124              unstable("Pipeline unstable due to quality gate failure: ${qg.status}")
   125          }
   126      }
   127  }
   128  
   129  String getHelmChartResourceName(String resource, String releaseName, String helmFlags, String chartLocation) {
   130      sh(returnStdout: true, script:
   131              "helm template ${releaseName} ${helmFlags} ${chartLocation} | " +
   132                      // Find deployment amongst all stuff rendered by helm
   133                      "awk '/${resource}/,/--/' | " +
   134                      // Find metadata and name
   135                      "awk '/metadata/,/  name:/' | " +
   136                      // grep only the "pure" name lines
   137                      "grep -e '^[ ]*name:' | " +
   138                      // keep only the first bellow metadata
   139                      "head -1 | " +
   140                      // keep only the value, drop "name:"
   141                      "sed 's@name:@@'")
   142              // Get rid of whitespaces
   143              .trim()
   144  }
   145  
   146  void waitForRolloutToComplete(String credentialsId, String resourceType, String resourceName, String timeout = '2m') {
   147      withKubectl(credentialsId) {
   148          sh "kubectl rollout status ${resourceType} ${resourceName} --timeout=${timeout}"
   149      }
   150  }
   151  
   152  void withKubectl(String credentialsId, Closure body) {
   153  
   154      // Namespace is set via KUBECONFIG!
   155  
   156      withCredentials([file(credentialsId: credentialsId, variable: 'KUBECONFIG')]) {
   157           docker.image(kubectlImage).inside("--entrypoint=''") {
   158              body()
   159          }
   160      }
   161  }
   162  
   163  String kubectlImage
   164  String helmImage