#!groovy

@Library('infraLib') _

def bumpVersionBranch = 'master'

pipeline {
  agent {
    label 'linux-agents'
  }
  options {
    ansiColor('xterm')
    disableConcurrentBuilds()
    timeout(60)
    timestamps()
    buildDiscarder logRotator(artifactDaysToKeepStr: '',
                              artifactNumToKeepStr: '',
                              daysToKeepStr: '',
                              numToKeepStr: '30')
  }
  environment {
    PROJECT        = 'flask-atlas-auth'
    PROJECT_GROUP  = 'core'

    AWS_PROFILE    = 'gdb-core-dev'
    AWS_ACCOUNT_ID = '582412345909'
    AWS_REGION     = 'us-east-1'

    ECR_REGISTRY        = "${env.AWS_ACCOUNT_ID}.dkr.ecr.${env.AWS_REGION}.amazonaws.com"
    ECR_REPOSITORY_NAME = "${PROJECT_GROUP}/${PROJECT}"
    ECR_REGISTRY_URI    = "${env.ECR_REGISTRY}/${ECR_REPOSITORY_NAME}"

    OCTOPUS_PROJECT     = "${env.PROJECT_GROUP}.${env.PROJECT}"
    OCTOPUS_CLI_SERVER  = 'https://octopus.delphi.zone'
    OCTOPUS_CLI_API_KEY = utility.getSecretValueById('infra/jenkins/octopus_api_key', 'gdb-delphi-dev')
    OCTOPUS_CHANNEL     = octopus.setOctoChannel(env.BRANCH_NAME)
    DEPLOY_ENV          = octopus.setDeploymentEnvV2(params.DEPLOY_ENV_CHOICE, env.PROJECT_GROUP, env.BRANCH_NAME)
    VERSION             = octopus.getVersion(env.BRANCH_NAME,env.WORKSPACE)

    // PYPI_INDEX_URL = 'https://artifacts.apollo.stream/repository/pypi-internal/'

    reporting_CHANNEL = '#core-deployments'
  }

  stages {
    stage('Build') {
      options {
        retry(2)
      }
      steps {
        script { currentBuild.displayName = env.VERSION }
        sh label: 'build', script: '''
            make docker/image/build
        '''
      } /** end: steps */
    } /** end: stage */

    stage('Test') {
      steps {
        sh label: 'test', script: '''
            make docker/test
        '''
      } /** end: steps */
      post {
        always {
          sh "sudo chown \$(whoami):\$(whoami) -R ${env.WORKSPACE}"
          junit allowEmptyResults: false, testResults: "report.xml"
          cobertura autoUpdateHealth: false,
                    autoUpdateStability: false,
                    coberturaReportFile: 'coverage.xml',
                    // conditionalCoverageTargets: '98, 70, 70',
                    failUnhealthy: false,
                    failUnstable: false,
                    // lineCoverageTargets: '98, 70, 70',
                    maxNumberOfBuilds: 0,
                    // methodCoverageTargets: '100, 70, 70',
                    onlyStable: false,
                    zoomCoverageChart: false
        }
      }
    } /** end: stage */
    stage('Style check') {
      steps {
        sh "make docker/checkstyle"
      }
      post {
        always {
          sh "sudo chown \$(whoami):\$(whoami) -R ${env.WORKSPACE}"
        }
      }
    } /** end: stage */
    stage('Security check') {
      steps {
        sh "make docker/security"
      }
      post {
        always {
          sh "sudo chown \$(whoami):\$(whoami) -R ${env.WORKSPACE}"
        }
      }
    } /** end: stage */

    stage('Deploy') {
      when {
        anyOf {
          branch 'master'
        }
      }
      steps {
        sh "make docker/deploy"
      } /** end: steps */
    } /** end: stage */
  } /** end: stages */
  post {
    always {
      sh label: 'fix permissions', script: "sudo chown \$(whoami):\$(whoami) -R ${env.WORKSPACE}"
      script {
        utility.slackNotify(currentBuild.currentResult, env.reporting_CHANNEL)
      }
    }
    cleanup {
      deleteDir()
    }
  } /** end: post */
} /* end: pipeline */
