
def APP_NAME = 'frontend-vector-job-rules'
def SLACK_NOTIFY_CHANNEL = '#distro-build-alerts'
def DEFAULT_AWS_REGION = 'us-east-1'

def QA_CDN_URL = 'https://qa-cdn.theorchard.io'
def PROD_CDN_URL = 'https://cdn.theorchard.io'

pipeline {
    agent {
        label 'aws'
    }

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        choice(name: 'MINIFY', choices: [1, 0], description: 'Whether or not to minify.')
        choice(name: 'DEPLOY_TO_PROD', choices: ['Yes', 'No'], description: 'Whether or not to deploy to prod.')
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag or Git revision.')
    }

    triggers {
        issueCommentTrigger('.*retest this please.*|.*deploy pri.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Unit Tests and Style Checks') {
            when {
                not {
                    environment name: 'GITHUB_COMMENT', value: 'deploy pri'
                }
            }
            steps {
                withCredentials([
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN'),
                ]) {
                    yarnRun([
                        scriptNames: ['test'],
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}"
                        ]
                    ])
                }
            }
        }
        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                // Build
                withCredentials([
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN')
                ]) {
                    yarnRun ([
                        scriptNames: ['deploy'],
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            ENV: 'qa',
                            MINIFY: params.MINIFY,
                            CDN_URL: QA_CDN_URL,
                            DEPLOY_VERSION: env.GIT_COMMIT
                        ]
                    ])
                }
                suiteAppPublish (
                    env: "qa",
                    appName: APP_NAME
                )
            }
        }
        stage('Invalidate QA CDN') {
            when {
                branch 'master'
            }
            steps {
                cdnInvalidate(
                    env: 'qa',
                    appName: APP_NAME
                )
            }
        }
        // stage('E2E Tests') {
        //     when {
        //         branch 'master'
        //     }
        //     steps {
        //         e2eTests tags: '@frontend_vector_job_rules'
        //     }
        //     post {
        //         regression {
        //             slackNotify channel: SLACK_NOTIFY_CHANNEL
        //         }
        //         fixed {
        //             slackNotify channel: SLACK_NOTIFY_CHANNEL
        //         }
        //     }
        // }
        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                // Build
                withCredentials([
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN')
                ]) {
                    yarnRun ([
                        scriptNames: ['deploy'],
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            ENV: 'prod',
                            MINIFY: params.MINIFY,
                            CDN_URL: PROD_CDN_URL,
                            DEPLOY_VERSION: env.GIT_COMMIT
                        ]
                    ])
                }
                suiteAppPublish (
                    env: "prod",
                    appName: APP_NAME
                )
            }
        }
        stage('Invalidate Prod CDN') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                cdnInvalidate(
                    env: 'prod',
                    appName: APP_NAME
                )
            }
        }
    }

    post {
        cleanup {
            cleanWs()
        }
    }
}
