#!groovy

def result

@Library('infraLib') _

pipeline {
  agent {
    label 'linux-agents'
  }
  options {
    ansiColor('xterm')
    disableConcurrentBuilds()
    disableResume()
    timeout(60)
    timestamps()
    buildDiscarder logRotator(artifactDaysToKeepStr: '',
      artifactNumToKeepStr: '',
      daysToKeepStr: '',
      numToKeepStr: '15')
  }

  environment {
    NOTIFICATIONS_CHANNEL = '#core-deployments'
  }

  stages {
    stage('Upload documentation') {
      steps {
        script {
          result = sh (label: "upload documentations", script: "aws --profile gdb-infra-dev s3 sync --exclude 'src/*' --size-only ${env.WORKSPACE}/docs/ s3://dev-infra-cross-products-documentation/${env.BRANCH_NAME}/", returnStdout: true)
          echo result
          if (result.trim() != "") {
            sh (label: "invalidate cache", script: "aws --profile gdb-infra-dev cloudfront create-invalidation --distribution-id E1TNIP6DFGVIY --paths '/${branch}/*.svg'")
          }
        }
      }
    }
  }
  post {
    always {
      sh "sudo chown \$(whoami):\$(whoami) -R ${env.WORKSPACE}"
      script {
        utility.slackNotify(currentBuild.currentResult, env.NOTIFICATIONS_CHANNEL)
      }
    }
    cleanup {
      deleteDir()
    }
  }
}
