#!groovy

import groovy.transform.Field

@Field
def pulumiPod = '''
apiVersion: v1
kind: Pod
spec:
  containers:
  - name: main
    image: gcr.io/pingcap-public/third-party/pulumi:v2.15.3-go-v1.17.6
    command:
    - cat
    tty: true
    resources:
      requests:
        memory: "2000Mi"
        cpu: 2000m
      limits:
        memory: "4000Mi"
        cpu: 4000m
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    - name: AWS_WEB_IDENTITY_TOKEN_FILE
      value: /identity/token
    volumeMounts:
    - mountPath: /identity
      name: identity
  - name: dind
    image: docker:18.09-dind
    securityContext:
      privileged: true
  - name: auth
    image: gcr.io/pingcap-gardener/jenkins/gcp-web-identity:20201201
    env:
    - name: AWS_WEB_IDENTITY_TOKEN_FILE
      value: /identity/token
    volumeMounts:
    - mountPath: /identity
      name: identity
  volumes:
  - emptyDir: {}
    name: identity
  restartPolicy: Never
'''

def IMAGE_TAG = 'unknown'

pipeline {
    agent {
        kubernetes {
            yaml pulumiPod
            defaultContainer "main"
            customWorkspace "/home/jenkins/agent/workspace/go/src/github.com/tidbcloud/config-service"
        }
    }

    options {
        disableConcurrentBuilds()
    }

    environment {
        GOPATH = "/home/jenkins/agent/workspace/go"
        REGION = "us-west-2"
        AWS_DEFAULT_REGION  = "us-west-2"
        GOPRIVATE = "github.com/tidbcloud,github.com/pingcap/dbaas"
        GITHUB_BOT = credentials("github-sre-bot")
        BASE_IMAGE = 'gcr.io/pingcap-public/config-service'
    }

    stages {
        stage('Unit test') {
            steps {
                sh '''
                    echo "machine github.com login ${GITHUB_BOT_USR} password ${GITHUB_BOT_PSW}" > ~/.netrc
                    make test
                '''
            }
        }
        stage('Build Release') {
            when { branch 'master' }
            steps {
                sh "gcloud auth configure-docker"
                sh "make docker-build"
                sh "make docker-build-overlay"
                sh "make docker-push"
            }
            post {
                unsuccessful {
                    script {
                        sendNotification("#gitops-ci", 'FAILURE')
                    }
                }
                success {
                    script {
                        sendNotification("#gitops-ci", 'SUCCESS')
                    }
                }
            }
        }
    }
}

def sendNotification(CHANNEL="#gitops-ci", STATUS="SUCCESS") {
    def GIT_AUTHOR_NAME = sh(returnStdout: true, script: "git log -1 --pretty=format:'%an'").trim()
    def GIT_COMMIT_MSG = sh(returnStdout: true, script: "git log --oneline -n 1 --pretty=%B HEAD | head -n 1").trim()
    def slackmsg = "[<${env.RUN_DISPLAY_URL}|Build Config-Service Image>] `${STATUS}`" + "\n" +
            "Commit: <https://github.com/tidbcloud/config-service/commit/${env.GIT_COMMIT}|${GIT_COMMIT_MSG}>" + '\n' +
            "Authored by: `${GIT_AUTHOR_NAME}`"
    def COLOR = 'good'
    if (STATUS != "SUCCESS") {
        COLOR = 'danger'
    }
    slackSend channel: CHANNEL, color: COLOR, teamDomain: 'pingcap', tokenCredentialId: 'slack-pingcap-token', message: "${slackmsg}"
}
