pipeline {
    agent any
    environment {
        SF_DEPLOY__ENABLED = true
        GIT_USERNAME = credentials('git-username')
        GIT_TOKEN = credentials('git-token')
        SF_ORG__SIT__AUTH_URL = credentials('package-sit-auth-url')
        SF_ORG__UAT__AUTH_URL = credentials('package-uat-auth-url')
        SF_ORG__PROD__AUTH_URL = credentials('package-prod-auth-url')
        SF_ORG__DEVHUB__AUTH_URL = credentials('package-devhub-auth-url')
        SONAR_LOGIN = credentials('sonar-login')
        ADX_IMAGE = 'appirio/dx:latest'
        ADX_REFRESH_TOKEN = credentials('adx-refresh-token')
    }
    stages {

        stage('sonarqube_scan'){
            agent {
                docker {
                    image '$ADX_IMAGE'
                    alwaysPull true
                }
            }
            steps {
                script{
                    tagName=sh(script: 'git tag -l --points-at HEAD | tail -1',returnStdout: true)
                    if(!tagName)
                        sh 'sonar-scanner-ee -Dsonar.login=$SONAR_LOGIN -Dsonar.qualitygate.wait=true -Dsonar.qualitygate.timeout=300'
                }
            }
        }
        stage('version_package'){
            when { branch 'master' }
            agent {
                docker {
                    image '$ADX_IMAGE'
                    alwaysPull true
                }
            }
            steps {
                script{
                    uatProdGitTag=sh(script: 'git tag -l --points-at HEAD | grep -e prod-* -e uat-* | tail -1',returnStdout: true)
                    if(!uatProdGitTag){
                        sh 'adx sfdx:package:publish --installationkeybypass'
                    }
                }
            }
        }
        stage('install_to_SIT'){
            when { branch 'master' }
            agent {
                docker {
                    image '$ADX_IMAGE'
                    alwaysPull true
                }
            }
            steps {
                script{
                    uatProdGitTag=sh(script: 'git tag -l --points-at HEAD | grep -e prod-* -e uat-* | tail -1',returnStdout: true)
                    if(!uatProdGitTag){
                        sh 'adx sfdx:package:install --destination SIT'
                    }
                }
            }
        }
        // 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 'install to UAT' when there is 'uat-*' tag on master along with manually triggering the build on master using 'Build Now' button.
        // If required, Jenkins manual approval can be used instead of tag build.
        stage('install_to_UAT'){
            when { branch 'master' }
            agent {
                docker {
                    image '$ADX_IMAGE'
                    alwaysPull true
                }
            }
            steps {
                script{
                    prodTagName=sh(script: 'git tag -l --points-at HEAD | grep prod-* | tail -1',returnStdout: true)
                    if(!prodTagName){
                        uatTagName=sh(script: 'git tag -l --points-at HEAD | grep uat-* | tail -1',returnStdout: true)
                        if(uatTagName){
                            sh 'adx sfdx:package:install --destination UAT'
                        }
                    }
                }
            }
        }
        // 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 'install to PROD' when there is 'prod-*' tag on master along with manually triggering the build on master using 'Build Now' button.
        // If required, Jenkins manual approval can be used instead of tag build.
        stage('install_to_PROD'){
            when { branch 'master' }
            agent {
                docker {
                    image '$ADX_IMAGE'
                    alwaysPull true
                }
            }
            steps {
                script{
                    prodTagName=sh(script: 'git tag -l --points-at HEAD | grep prod-* | tail -1',returnStdout: true)
                    if(prodTagName){
                        sh 'adx sfdx:package:install --destination PROD --release'
                    }
                }
            }
        }
    }
}
