pipeline {
    agent {
        kubernetes {
            defaultContainer "jnlp"
            yaml """
kind: "Pod"
metadata:
  annotations:
    app: "jenkins-agent"
    sidecar.istio.io/inject: "false"
spec:
  containers:
  - image: "407903926827.dkr.ecr.us-west-2.amazonaws.com/liquidata/jnlp:latest"
    imagePullPolicy: "Always"
    name: "jnlp"
    resources:
      limits:
        cpu: "1.8"
        memory: "3Gi"
      requests:
        cpu: "1.8"
        memory: "3Gi"
    securityContext:
      privileged: false
    tty: true
    workingDir: "/home/jenkins/agent"
  restartPolicy: "Never"
  securityContext:
    fsGroup: 1000
    runAsGroup: 1000
    runAsUser: 1000
  serviceAccount: "jenkins-agent-doltci"
"""
        }
    }
    stages {
        stage ("Update Liquidata/dolt-sql-performance:nightly") {
            environment {
                PATH = "${pwd()}/.ci_bin/node_modules/.bin:${env.PATH}"
                DOLT_VERSION = "${env.GIT_COMMIT}"
                TMPDIR = "${pwd()}/tempDir"
                DOLT_ROOT_PATH="${pwd()}/tempRoot"
                DOLT_CREDS = credentials("system-account-dolthub-creds")
            }
            steps {
                sh "rm -rf $TMPDIR && mkdir $TMPDIR"
                sh "rm -rf $DOLT_ROOT_PATH && mkdir $DOLT_ROOT_PATH"
                dir ("sqllogictest") {
                    git url: "https://github.com/dolthub/sqllogictest.git"
                }
                dir ("benchmark/sql_regressions") {
                    script {
                        try {
                            sh "nice ./run_regressions.sh ./nightly.vars"
                        } catch(err) {
                            sh "if [ \"${err.getMessage()}\" = 'script returned exit code 155' ]; then echo 'Result data found in dolt-sql-performance, silently exiting...'; else echo \"${err.getMessage()}\" && exit 1; fi"
                        }
                    }
                }
            }
        }
    }
    post {
        always {
            node ("liquidata-inc-ld-build") {
                cleanWs() // cleanup
            }
        }
        failure {
             emailext body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n More info at: ${env.BUILD_URL}",
                 to: "$SQL_WATCHERS",
                 subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}"
        }
    }
}
