#!/usr/bin/env groovy

@Library('apm@current') _

pipeline {
  agent { label 'ubuntu-18 && immutable' }
  environment {
    REPO = 'go-seccomp-bpf'
    BASE_DIR = "src/github.com/elastic/${env.REPO}"
    JOB_GIT_CREDENTIALS = 'f6c7695a-671e-4f4f-a331-acdce44ff9ba'
    PIPELINE_LOG_LEVEL = 'INFO'
    GO111MODULE = 'on'
  }
  options {
    timeout(time: 1, unit: 'HOURS')
    buildDiscarder(logRotator(numToKeepStr: '20', artifactNumToKeepStr: '20', daysToKeepStr: '30'))
    timestamps()
    ansiColor('xterm')
    disableResume()
    durabilityHint('PERFORMANCE_OPTIMIZED')
    rateLimitBuilds(throttle: [count: 60, durationName: 'hour', userBoost: true])
    quietPeriod(10)
  }
  triggers {
    issueCommentTrigger('(?i)(.*(?:jenkins\\W+)?run\\W+(?:the\\W+)?tests(?:\\W+please)?.*|^\\/test$)')
  }
  stages {
    stage('Checkout') {
      steps {
        pipelineManager([ cancelPreviousRunningBuilds: [ when: 'PR' ] ])
        deleteDir()
        gitCheckout(basedir: "${BASE_DIR}")
        stash allowEmpty: true, name: 'source', useDefaultExcludes: false
      }
    }
    stage('verify'){
      steps {
        withGithubNotify(context: "Verify") {
          deleteDir()
          unstash 'source'
          withGoEnv(pkgs: ['github.com/elastic/go-licenser']){
            dir("${BASE_DIR}"){
              sh(label: 'Go build', script: 'go mod verify')
              sh(label: 'Go build', script: 'go-licenser -d')
              sh(label: 'Go build', script: 'go run .ci/scripts/check_format.go')
            }
          }
        }
      }
    }
    stage('Test') {
      failFast false
      matrix {
        agent { label "${PLATFORM}" }
        options { skipDefaultCheckout() }
        axes {
          axis {
            name 'GO_VERSION'
            values '1.12.17', '1.14.13'
          }
          axis {
            name 'PLATFORM'
            values 'ubuntu-18 && immutable'
          }
        }
        stages {
          stage('build'){
            steps {
              withGithubNotify(context: "Build-${GO_VERSION}-${PLATFORM}") {
                deleteDir()
                unstash 'source'
                withGoEnv(version: "${GO_VERSION}"){
                  dir("${BASE_DIR}"){
                    cmd(label: 'Go build', script: 'go build')
                  }
                }
              }
            }
          }
          stage('Test') {
            steps {
              withGithubNotify(context: "Test-${GO_VERSION}-${PLATFORM}") {
                deleteDir()
                unstash 'source'
                withGoEnv(version: "${GO_VERSION}"){
                  dir("${BASE_DIR}"){
                    goTestJUnit(options: '-v ./...', output: 'junit-report.xml')
                  }
                }
              }
            }
            post {
              always {
                junit(allowEmptyResults: true, keepLongStdio: true, testResults: '**/junit-report.xml')
              }
            }
          }
        }
      }
    }
  }
  post {
    cleanup {
      notifyBuildResult(prComment: true)
    }
  }
}
