Jenkins Notes

From PeformIQ Upgrade
Revision as of 12:05, 4 August 2020 by PeterHarding (talk | contribs) (Created page with "=Jenkins Scripts= <pre> #!/usr/bin/env groovy pipeline { agent { label 'ci' } environment { REPO_NAME = getRepoName("${JOB_NAME}") // JOB_BASE_NAME is identical to...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Jenkins Scripts

#!/usr/bin/env groovy

pipeline {

  agent { label 'ci' }

  environment {
    REPO_NAME = getRepoName("${JOB_NAME}") // JOB_BASE_NAME is identical to the github repo name
    APPLICATION_REPO_NAME = getRepoName("${JOB_NAME}")
    APPLICATION_REPO = "git@github.aus.thenational.com:${GITHUB_ORGANIZATION}/${APPLICATION_REPO_NAME}.git"
  }

  parameters{
    string(name: 'SCRIPT_BRANCH', defaultValue: 'develop', description: 'performance script branch.')
    string(name: 'TARGET_CLUSTER', defaultValue: 'perf2', description: 'e.g. perf2. leave empty to use the value in application-ppte-load.conf')
    string(name: 'MAX_USERS', defaultValue: '', description: 'leave emtpy to pick this value from application.conf')
    booleanParam(name: 'SHAKEOUT', defaultValue: 'true', description: 'shakeout before running full load and abort if shakeout has 100% failures')
  }

  stages {
    stage('Checkout') {
      steps {
        script {
          echo "${env.REPO_NAME}"
          checkout([$class: 'GitSCM', branches: [[name: "${params.SCRIPT_BRANCH}"]], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: 'GITHUB_PRIVATE_KEY', url: "${APPLICATION_REPO}"]]])
          env.APP_NAME = getAppName()

          env.EXTRA_OPTS = "-Dconfiguration=application-ppte-load.conf"
          if ( "${params.TARGET_CLUSTER}" ) {
            env.EXTRA_OPTS = env.EXTRA_OPTS + " -Dgatling.endpoint=gatling.baseUrl.${params.TARGET_CLUSTER}"
          }
          if ( "${params.MAX_USERS}" ) {
            env.EXTRA_OPTS = env.EXTRA_OPTS + " -Dgatling.users=${params.MAX_USERS}"
          }
          echo "env.EXTRA_OPTS=${env.EXTRA_OPTS}"
        }
      }
    }

    stage('Shakeout') {
      steps {
        script {
          if ( params.SHAKEOUT ) {
            echo "Running shakeout"

            def result = sh script: "./gradlew -Dwarm.up=true performance:runPerformanceTest ${env.EXTRA_OPTS} -Dgatling.users=1 | tee perf_test_warm_up.log", returnStatus: true

            //check to see if there are any failed transactions
            sh script: "if [ `grep -w 'count.*[[:digit:]]*OK=0' perf_test_warm_up.log | wc -l` -gt \"0\" ] || [ ! -f \"perf_test_warm_up.log\" ]; then echo \"Warm up test failed with high number of KOs. Please check the perf_test_warm_up.log for more details.\"; exit 1; else echo \"Successfully completed warm up test.\"; fi"

            if (result != 0) {
              echo "Warm up errors can be ignored."
              zip zipFile: 'perf_test_warm_up.zip', glob: 'perf_test_warm_up.log'
              archiveArtifacts artifacts: "perf_test_warm_up.zip"
            }
          } else {
            echo "Skipping shakeout"
          }
        }
      }
    }

    stage('Performance Test') {
      steps {
        script {
          sh script: "rm -rf performance/results"
          def result = sh script: "./gradlew performance:runLoadTest ${env.EXTRA_OPTS}", returnStatus: true
          sh script: "./gradlew performance:performanceReport"
          sh script: "COMMIT_SHA_TAG=`git rev-parse HEAD`; ./gradlew performance:performanceResultsSummaryForSplunk -Papp_name=${APP_NAME} -Pbranch_name=${params.SCRIPT_BRANCH} -Pcommit_sha_tag=${env.COMMIT_SHA_TAG} -Pbuild_number=${BUILD_NUMBER}"
          def results  = sh ( script: 'cat performance/results/ese_perf_results.csv', returnStdout: true ).trim()
          def payload = groovy.json.JsonOutput.toJson(["event": "${results}", "source": "${env.APP_NAME}-${params.SCRIPT_BRANCH}", "sourcetype": "gatling_perf"])
          def response = sh(returnStdout: true, script: "curl -k -H \"Authorization: Splunk ${env.SPLUNK_TOKEN_APP_LOGS}\" -d '${payload}' ${env.SPLUNK_URL}").trim()
          if (result > 0) {
            error("Performance Tests failed")
          }
        }
      }
      post {
        always {
          sh "ls -larth"
          zip zipFile: 'performance-test-report.zip', dir: 'performance'
          zip zipFile: 'perf-run-log.zip', glob: 'perf-run.log'
          archiveArtifacts artifacts: "perf-run-log.zip"
          archiveArtifacts artifacts: 'performance-test-report.zip'
          junit 'performance/results/**/js/assertions.xml'
        }
      }
    }
  }

}

String getRepoName(String job_name) {
  return job_name.split("/")[1].replaceAll('_', '-')
}


String getAppName() {
  return sh(returnStdout: true, script: "awk -F\\' '/rootProject.name/ {print \$2}' settings.gradle").trim().replaceAll('_', '-')
}