#!groovy

def agentPod = '''
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
  restartPolicy: Never
'''

pipeline {
    agent any

    environment {
        GOPATH = "/home/jenkins/agent/workspace/go"
        GOBIN = "/usr/local/go/bin"
        GOPRIVATE = "github.com/tidbcloud,github.com/pingcap/dbaas"
        GITHUB_BOT = credentials("github-sre-bot")
    }

    stages {
        stage('Check') {
            when {
                beforeAgent true
                allOf {
                    anyOf {
                        expression {
                            sh(returnStatus: true, script: 'git diff origin/${CHANGE_TARGET}...HEAD --name-only | grep "^.*\\.go$"') == 0
                        }
                        expression {
                            sh(returnStatus: true, script: 'git diff origin/${CHANGE_TARGET}...HEAD --name-only | grep "go.mod"') == 0
                        }
                    }
                }
            }
            agent {
                kubernetes {
                    yaml agentPod
                    defaultContainer "main"
                    customWorkspace "/home/jenkins/agent/workspace/go/src/github.com/tidbcloud/config-service"
                }
            }
            stages {
                stage('Unit test') {
                    steps {
                        sh '''
                        echo "machine github.com login ${GITHUB_BOT_USR} password ${GITHUB_BOT_PSW}" > ~/.netrc
                        make check
                        '''
                    }
                }
            }
        }
    }
}
