#!groovy

def agentPod = '''
apiVersion: v1
kind: Pod
spec:
  volumes:
  - emptyDir: {}
    name: identity
  - name: cache
    hostPath:
      path: /mnt/stateful_partition/cicache/shared-provider
      type: DirectoryOrCreate
  containers:
  - name: main
    image: pulumi/pulumi:v3.3.1
    command:
    - cat
    tty: true
    resources:
      requests:
        memory: "2000Mi"
        cpu: 1000m
      limits:
        memory: "4000Mi"
        cpu: 1000m
    env:
    - name: AWS_WEB_IDENTITY_TOKEN_FILE
      value: /identity/token
    - name: DOCKER_HOST
      value: tcp://localhost:2375
    volumeMounts:
    - mountPath: /identity
      name: identity
    - name: cache
      mountPath: /cache

  - name: dind
    image: docker:18.09-dind
    securityContext:
      privileged: true
    volumeMounts:
    - name: cache
      mountPath: /cache

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

pipeline {
    agent {
        kubernetes {
            yaml agentPod
            defaultContainer "main"
            customWorkspace "/home/jenkins/agent/workspace/go/src/github.com/tidbcloud/shared-provider"
        }
    }

    environment {
        GOCACHE = "/cache/gocache"
        GOMODCACHE = "/cache/gomodcache"
        GOPATH = "/home/jenkins/agent/workspace/go"
        GOPRIVATE = "github.com/tidbcloud,github.com/pingcap/dbaas"
        GITHUB_BOT = credentials("github-sre-bot")
        VAR_PRIVATE_REPO_DOWNLOAD_TYPE = "netrc"
    }

    stages {
        stage('setup') {
            steps {
                container('main') {
                    sh '''
                    # permission to get private go packages
                    echo "machine github.com login ${GITHUB_BOT_USR} password ${GITHUB_BOT_PSW}" > ~/.netrc
                    '''
                }
            }
        }
    }
    post {
        success {
            echo "run deploy pipeline"
            build job: "shared-provider-cd/${BRANCH_NAME}", parameters: []
        }
    }
}

