pipeline {
  agent any

  options {
      disableConcurrentBuilds()
      timeout(time: 30, unit: 'MINUTES')
      ansiColor('xterm')
      timestamps()
  }

  stages {
    stage("Git fetch master") {
      steps {
        sshagent(['git-aws-read-key']) {
          sh "git config --add remote.origin.fetch +refs/heads/master:refs/remotes/origin/master"
          sh "git fetch --no-tags"
          sh "git checkout master"
          sh "git checkout ${GIT_COMMIT}"
        }
      }
    }
    stage("Lint") {
      steps {
        echo "Check that all linters pass"
        sh "make lint"
      }
    }
    stage("Test") {
      steps {
        echo "Run tests"
        sh "make test"
      }
    }
    stage("Build") {
      steps {
        echo "Check that code builds successfully"
        sh "make jenkins-check-gen"
      }
    }
    stage("Codegen sync test") {
      steps {
        echo "Check that code was generated with make build"
        sh "make jenkins-generated-code-check"
      }
    }
    stage("Update ControlPlane: Staging") {
      when {
        branch 'master'
      }
      steps {
        echo "Fire the lambda that sends up-to-date schema information to staging controlplane"
        sh "make jenkins-metadata-generation"
        sh "AWS_ACCT_ID=297385687169 ./cmd/jenkins/invoke-controlplane-updater.sh"
      }
    }
    stage("Update ControlPlane: Production") {
      when {
        branch 'master'
      }
      steps {
        echo "Fire the lambda that sends up-to-date schema information to production controlplane"
        sh "AWS_ACCT_ID=859517684765 ./cmd/jenkins/invoke-controlplane-updater.sh"
      }
    }
  }
}
