String SLACK_NOTIFICATIONS_CHANNEL = '#data,#distro-build-alerts'
String PROD_AWS_CREDS_ID = 'prod-swf-feed-sender-exec-creds'
String QA_AWS_CREDS_ID = 'swf-proper-new-releases-exec-creds'

pipeline {
    agent any

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

    parameters {
        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.')
        choice(name: 'Environment', choices: ['prod', 'qa'], description: 'The environment to run against.')

        //Boolean values are not used - if parameter is an empty string it is omitted.
        string(name: 'reload', defaultValue: 'True', description: 'sets context "reload"')
        string(name: 'proper_in_vector', defaultValue: 'True', description: 'sets context "proper_in_vector": "True"')
        string(name: 'cutoff', defaultValue: '', description: 'sets custom cutoff time, by default - current time in EST time zone')
        string(name: 's3_only', defaultValue: '', description: 'sets context "s3_only"')
    }

    environment {
        reload = "${params.reload}"
        proper_in_vector = "${params.proper_in_vector}"
        cutoff = "${params.cutoff}"
        s3_only = "${params.s3_only}"
        FEED_SENDER_TABLE = "${params.Environment == 'prod' ? 'prod_feed_sender_status' : 'dev_feed_sender_status'}"
        // No QA creds were presented for some reason
        CREDENTIALS_ID = "${PROD_AWS_CREDS_ID}"
        AWS_DEFAULT_REGION = "us-east-1"
    }

    triggers {
        parameterizedCron('H */2 * * * % Environment=prod')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Run exec job') {
            steps {
                withEcr {
                    withEnv(["Environment=${params.Environment}"]) {
                        withCredentials([[
                            $class: 'AmazonWebServicesCredentialsBinding',
                            credentialsId: env.CREDENTIALS_ID,
                            accessKeyVariable: 'AWS_ACCESS_KEY_ID',
                            secretKeyVariable: 'AWS_SECRET_ACCESS_KEY'
                            ]]) {
                            sh "make docker_proper_changed_releases_exec"
                        }
                    }
                }
            }
            post {
                cleanup {
                    sh 'make down_proper_changed_releases_exec'
                }
            }
        }
    }
    post {
        regression {
            script {
                if (params.Environment == 'prod') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        fixed {
            script {
                if (params.Environment == 'prod') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        cleanup {
            cleanWs()
        }
    }
}
