#!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
    env:
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    resources:
      requests:
        memory: "2000Mi"
        cpu: 2000m
      limits:
        memory: "4000Mi"
        cpu: 4000m
  - name: dind
    image: docker:18.09-dind
    securityContext:
      privileged: true
  restartPolicy: Never
'''

pipeline {
    agent any

    environment {
        GOPATH = "/home/jenkins/agent/workspace/lint/go"
        GITHUB_BOT = credentials("github-sre-bot")
        GOPRIVATE="github.com/tidbcloud"
    }

    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 "^.*\\.ts$"') == 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/lint/go/src/github.com/pingcap/dbaas"
                }
            }
            stages {
                stage('Prepare') {
                    steps {
                        sh 'echo "machine github.com login ${GITHUB_BOT_USR} password ${GITHUB_BOT_PSW}" > ~/.netrc'
                    }
                }
                stage('Lint') {
                    steps {
                        sh 'make check'
                    }
                }
                stage('Unit test') {

                    steps {
                        sh 'make test'
                    }
                }
            }
        }
    }
}
