String SLACK_NOTIFICATIONS_CHANNEL = '#graphql, #sound-recording-alerts'

pipeline {
    agent any
    parameters {
        string(
            name: 'SHARED_LIBRARIES_VERSION',
            defaultValue: 'master',
            description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag, Git revision or PR ref (e.g. pull/PR_NUMBER/merge).'
        )
    }
    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Get Git Commit') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '577cbc72-7d9e-4eba-924c-ecbbe6de9805', url: 'git@github.com:theorchard/graphql-sr-delivery.git']]])
                    GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                }
            }
        }
        stage('Unit tests and Style Checks') {
            steps {
                build job: 'graphql-sr-delivery-unit-lint', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT]]
            }
        }
        stage('Create a release') {
            steps {
                build job: 'graphql-sr-delivery-docker-to-ecr', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT]]
            }
        }
        stage('Validate QA schema') {
            when {
                expression {
                    params.VALIDATE_SCHEMA == true
                }
            }
            steps {
                build job: 'graphql-sr-delivery-validate-schema', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "qa"]]
            }
        }
        stage('Deploying to QA') {
            steps {
                build job: 'graphql-sr-delivery-docker-fargate-deploy', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "qa"]]
            }
        }
        stage('Push QA schema') {
            steps {
                build job: 'graphql-sr-delivery-push-schema', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "qa"]]
            }
        }

        stage('Integration Tests') {
            steps {
                retry(2) {
                    echo "Testing the version against integration tests... ${GIT_COMMIT}"
                    sleep(10)
                    build job: 'graphql-sr-delivery-integration-tests',  parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT]]
                }
            }
        }
        stage('E2E Tests') {
            steps {
                echo "Testing the version against end-to-end tests... ${GIT_COMMIT}"
                build job: 'graphql-sr-delivery-e2e-cucumber-tests'
            }
        }
        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Validate Prod schema') {
            when {
                expression {
                    params.VALIDATE_SCHEMA == true
                }
            }
            steps {
                build job: 'graphql-sr-delivery-validate-schema', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "prod"]]
            }
        }
        stage('Deploy to Prod') {
            steps {
                echo "Deploying to prod... ${GIT_COMMIT}"
                build job: 'graphql-sr-delivery-docker-fargate-deploy', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "prod"]]
                datadogSoftwareCatalogPublish()
            }
        }
        stage('Push Prod schema') {
            steps {
                build job: 'graphql-sr-delivery-push-schema', parameters: [[$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'ENV', value: "prod"]]
            }
        }
    }
    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()
        }
    }
}
