STAGING_BRANCH = "master"
STAGING_ACCT = "twitch-clock-dev"
PROD_ACCT = "twitch-clock-prod"
AWS_REGION = "us-west-2"

def make(cmd) {
    sh "make ${cmd}"
}

pipeline {
    agent any
    options {
        disableConcurrentBuilds()
        timeout(time: 1, unit: 'HOURS')
        ansiColor('xterm')
        timestamps()
    }
    stages {
        stage('Build') {
            steps {
                make("build")
            }
        }
        stage('Test') {
            steps {
                echo "Build environment"
                sh "env"
                echo "Running tests with `make test`"
                make("test")
                echo "Linting with `make lint`"
                make("lint")
            }
        }
        stage('Deploy Staging') {
            when {
                branch STAGING_BRANCH
            }
            steps {
                withAWS(credentials: STAGING_ACCT, region: AWS_REGION) {
                    make("deploy_staging")
                }
            }
        }
        stage('Deploy Production') {
            when {
                branch 'master'
            }
            steps {
                input(message: "Deploy to production?", ok: 'Yes')
                withAWS(credentials: PROD_ACCT, region: AWS_REGION) {
                    make("deploy_prod")
                }
            }
        }
    }
}
