pipeline {
    agent any
    environment {
        ADX_IMAGE = 'appirio/dx:latest'
        SF_DEPLOY__ENABLED = true
        GIT_USERNAME = credentials('git-username')
        GIT_TOKEN = credentials('git-token')
        SF_ORG__SIT__AUTH_URL = credentials('sit-auth-url')
        SF_ORG__UAT__AUTH_URL = credentials('uat-auth-url')
        SF_ORG__PROD__AUTH_URL = credentials('prod-auth-url')
        SONAR_LOGIN = credentials('sonar-login')
        ADX_REFRESH_TOKEN = credentials('adx-refresh-token')
    }
    stages {
	   stage('feature') {
            when { branch 'feature/*'}
            stages{
                stage('sonarqube_scan'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh 'sonar-scanner-ee -Dsonar.login=$SONAR_LOGIN -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300'
                    }
                }
                stage('validate_against_SIT'){
                    agent {
	                    docker {
	                        image '$ADX_IMAGE'
                            alwaysPull true
	                    }
                    }
                    steps {
                        sh "adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata"
                        sh "adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --checkonly --targetalias SIT"
                    }
                }
            }
        }
        stage('SIT') {
            when { branch 'SIT' }
            stages{
                stage('sonarqube_scan'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh 'sonar-scanner-ee -Dsonar.login=$SONAR_LOGIN -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300'
                    }
                }
                stage('deploy_to_SIT'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh "adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata"
                        sh "adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --targetalias SIT"
                    }
                }
                stage('validate_against_UAT') {
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh "adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata"
                        sh "adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --checkonly --targetalias UAT"
                    }
                }
            }
        }
        stage('master') {
            when { branch 'master' }
            stages{
                stage('sonarqube_scan'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh 'sonar-scanner-ee -Dsonar.login=$SONAR_LOGIN -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300'
                    }
                }
                stage('deploy_to_UAT'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh "adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata"
                        sh "adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --targetalias UAT"
                    }
                }
                stage('validate_against_PROD') {
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh "adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata"
                        sh "adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --checkonly --targetalias PROD"
                    }
                }
            }
        }
        stage('PROD') {
            // Ideally, This job should be triggered manually. Jenkins supports manual approval but it does not work very smoothly.
            // Either there needs to be a timeout for manual approval OR the jenkins server continues to consume resources till the time approval is provided.
            // Because of this reason, this sample triggers 'deploy to PROD' on 'v*' tag push.
            // If required, Jenkins manual approval can be used instead of tag build.
            when {
                allOf{buildingTag() ; branch 'v*'}
            }
            stages{
                stage('deploy_to_PROD'){
                    agent {
                        docker {
                            image '$ADX_IMAGE'
                            alwaysPull true
                        }
                    }
                    steps {
                        sh 'adx metadata:unique --sourcepath force-app/main/default,force-app/sample/metadata'
                        sh 'adx deploy:source --sourcepath force-app/main/default,force-app/sample/metadata --testdiff --targetalias PROD'
                    }
                }
            }
        }
    }
}
