String GITHUB_REPOSITORY = 'ows-podcast'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#podcast-team'
String QA_ACCOUNT_ID = '437795906767'
String QA_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'

pipeline {
    agent any

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: true, description: 'Whether or not to deploy to prod.')
        booleanParam(name: 'RUN_E2E_TESTS_ONLY', defaultValue: false, description: 'Check this to <b>just</b> run the E2E tests. No other stages will be run, regardless of other parameter values.')
        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') {
            when {
                expression { !params.RUN_E2E_TESTS_ONLY }
            }
            steps {
                complianceChecks()
            }
        }
        stage('Unit Tests and Style Checks') {
            when {
                expression { !params.RUN_E2E_TESTS_ONLY }
            }
            steps {
                sh 'make ci_unit_lint'
            }
            post {
                always {
                    xunit tools: [JUnit(pattern: 'pyunit.xml', deleteOutputFiles: true, failIfNotNew: true, stopProcessingIfError: true)]
                    cobertura coberturaReportFile: 'coverage.xml', failNoReports: true, failUnhealthy: true, failUnstable: true, zoomCoverageChart: true, onlyStable: false
                }
            }
        }
        stage('Static Application Security Tests') {
            when {
                expression { !params.RUN_E2E_TESTS_ONLY }
            }
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                allOf {
                    branch 'master'
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }
        stage('Create a Release') {
            when {
                allOf {
                    branch 'master'
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT, dockerBuildTarget: 'deploy'
            }
        }
        stage('Scan Docker Image') {
            when {
                allOf {
                    branch 'master'
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                dockerScan awsRegion: AWS_REGIONS[0], ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT, failBuild: false
            }
        }
        stage('Deploy to QA') {
            when {
                allOf {
                    branch 'master'
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
            }
        }
        stage('Integration Tests') {
            when {
                allOf {
                    branch 'master'
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                withEnv(['OWS_PODCAST_URL=https://qa-ows-podcast.theorchard.io', 'OWS_ASSET_TRANSCODER_URL=https://qa-ows-asset-transcoder.theorchard.io', 'INPUT_ASSETS_BUCKET_NAME=qa-orcd-asset-transcoder-input', 'OUTPUT_ASSETS_BUCKET_NAME=qa-orcd-podcast-output-assets', 'OUTPUT_PODCAST_CDN=qa-podcast-assets.theorchard.io', 'SKIP_NEGATIVE_TESTS=False']) {
                    sh 'make ci_test_integration'
                }
            }
        }
        // stage('E2E Tests') {
        //     when {
        //         branch 'master'
        //     }
        //     environment {
        //         TAGS = "@ows_podcast"
        //     }
        //     steps {
        //         e2eTests tags: env.TAGS, envVars: ['USE_DYNAMIC_THROTTLING': 'true']
        //     }
        //     post {
        //         regression {
        //             slackNotify(channel: '#cypress-test-e2e-results')
        //         }
        //         fixed {
        //             slackNotify(channel: '#cypress-test-e2e-results')
        //         }
        //     }
        // }
        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD }
                    expression { !params.RUN_E2E_TESTS_ONLY }
                }
            }
            steps {
                fargateDeploy environment: 'prod', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
            }
        }
    }

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