String GITHUB_REPOSITORY = 'dx-ui-service'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1', 'eu-central-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#dx-alerts'
String QA_ACCOUNT_ID = '765134955759'
String QA_DEPLOYMENT_ROLE = 'qa-jenkins-pipeline-deploy-role'
String PROD_ACCOUNT_ID = '729038418454'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-pipeline-deploy-role'

pipeline {
    agent any

    environment {
        LANG = 'en_US.UTF-8'
        LC_ALL = 'en_US.UTF-8'
    }

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, 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.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }

        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }

        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }

        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Create a Release') {
            when {
                branch 'master'
            }
            steps {
                withCredentials([
                    string(credentialsId: 'github_packages_token', variable: 'GITHUB_TOKEN'),
                    string(credentialsId: 'github_packages_username', variable: 'GITHUB_USERNAME')
                ]) {
                    dockerToEcr awsRegions: [AWS_REGIONS[0]], ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT,
                        dockerBuildSecrets: [
                            [id: 'GITHUB_USERNAME', env: 'GITHUB_USERNAME'],
                            [id: 'GITHUB_TOKEN', env: 'GITHUB_TOKEN']
                        ]
                }
            }
        }

        stage('Scan Docker Image') {
            when {
                branch 'master'
            }
            steps {
                dockerScan awsRegion: AWS_REGIONS[0], ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT,
                    failBuild: false
            }
        }

        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                fargateDeploy environment: 'qa', awsRegions: [AWS_REGIONS[1]], gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
            }
        }

        /*
        stage('Integration Tests') {
            when {
                branch 'master'
            }
            environment {
                WIP = 'WIP' # This will be provided by DX team
            }
            steps {
                sh 'make docker_test_integration'
            }
            post {
                cleanup {
                    sh 'make docker_down'
                }
            }
        }
        */

        /*
        stage('K6 Load Tests') {
            when {
                branch 'master'
            }
            steps {
                k6Tests testFile: 'dx/digital-exchange-test', waitCompletion: true, propagateFailure: true
            }
        }
        */

        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD }
                }
            }
            steps {
                fargateDeploy environment: 'prod', awsRegions: [AWS_REGIONS[1]], gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
            }
        }

        stage('Publish Software Catalog Definition') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD }
                }
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }

    post {
        always {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        cleanup {
            cleanWs()
        }
    }
}
