#!/usr/bin/env groovy
@Library('flexo-libs')_
pipeline {
    agent any
    options {
        timestamps()
        ansiColor('xterm')
    }
    stages {
        stage('Build image') {
            steps {
                checkout scm
                // TODO eventually we should use packer library: https://git.xarth.tv/video-coreservices/package-builder/blob/master/Jenkinsfile#L3-L5
        sshagent(credentials: ['git-aws-read-key']) {
            sh """
                    make
            """
        }
            }
        }
        stage('Upload artifacts') {
            steps {
                script {
                    if (env.BRANCH_NAME == 'master') {
                        withAgentAWS(role: "provisioner-s3-reverse-proxy-jenkins-assume-role", roleAccount: "805315462901", region: "us-west-2") {
                            sh '''
                                ./jenkins-deploy.sh production
                            '''
                        }
                    } else {
                        withAgentAWS(role: "provisioner-s3-reverse-proxy-jenkins-assume-role", roleAccount: "045385815217", region: "us-east-2") {
                            sh '''
                                ./jenkins-deploy.sh staging
                            '''
                        }
                    }
                }
            }
        }
    }
}
