String GITHUB_REPOSITORY = 'wowza'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#devops'
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'
List<String> EXCLUDED_PACKAGES = ['guarddog', 'emoji']
String EXCLUDED_PACKAGES_STR = EXCLUDED_PACKAGES.join(',')


pipeline {
    agent any

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

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

    parameters {
        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.')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        // stage('Checkout Guarddog code') {
        //     steps {
        //         checkout([$class: 'GitSCM',
        //             branches: [[name: '*/master']],
        //             doGenerateSubmoduleConfigurations: false,
        //             extensions: [
        //                 [$class: 'RelativeTargetDirectory', relativeTargetDir: 'python-deployment-utils'],
        //                 [$class: 'SparseCheckoutPaths', sparseCheckoutPaths: [
        //                     [$class: 'SparseCheckoutPath', path: 'guarddog']
        //                 ]]
        //             ],
        //             submoduleCfg: [],
        //             userRemoteConfigs: [[
        //                 credentialsId: '7c29b204-9e5b-45e0-bd0a-6535917c604f',
        //                 url: "git@github.com:theorchard/python-deployment-utils.git"
        //             ]]
        //         ])
        //     }
        // }
        // stage('Run Guarddog Scan') {
        //     steps {
        //         withEcr {
        //             sh 'docker build --platform linux/amd64 -t guarddog python-deployment-utils/guarddog'
        //             sh "docker run --platform linux/amd64 --rm -v $WORKSPACE:/app/target -e EXCLUDE_PACKAGES=${EXCLUDED_PACKAGES_STR} guarddog /app/target"
        //         }
        //     }
        // }
        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Create a Release') {
            when {
                branch 'master'
            }
            steps {
                dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT
            }
        }
        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, 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 {
                QA_WOWZA_URL = 'http://localhost:1935/ServerVersion'
            }
            steps {
                sh 'make docker_test_integration'
            }
            post {
                cleanup {
                    sh 'make docker_down'
                }
            }
        }*/
        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            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
            }
        }
        stage('Publish Software Catalog Definition') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }

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