AWS_REGION = "us-west-2"
DEV_CREDENTIALS_ID = "twitch-secretshop-dev"
PROD_CREDENTIALS_ID = "twitch-secretshop-prod"
DEV_ACCOUNT_ID = "454314487337"
PROD_ACCOUNT_ID = "774733389815"
JENKINS_ROLE = "secretshop-jenkins"

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

properties([
    pipelineTriggers([
        [$class: "tv.justin.jenkins.twitchgithub.GitHubPushTrigger"]
    ])
])

pipeline {
    agent any

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

    stages {
        stage('Build') {
            steps {
                sh 'make jenkins-build'
            }
        }

        stage('Lint and test') {
            steps {
                sh 'make jenkins'
            }
        }
        stage('Build dev docker image') {
            steps {
                withAWS (role: JENKINS_ROLE, roleAccount: DEV_ACCOUNT_ID) {
                    make('publish-docker-dev')
                }
            }
        }
        
        stage('Build prod docker image') {
            steps {
                withAWS (role: JENKINS_ROLE, roleAccount: PROD_ACCOUNT_ID) {
                    make('publish-docker-prod')
                }
            }
        }

        stage('Deploy canary docker image') {
            when {
                branch 'master'
            }
            steps {
                withAWS (role: JENKINS_ROLE, roleAccount: PROD_ACCOUNT_ID) {
                    make('deploy-current-docker-canary')
                }
            }
        }
    }
}
