def FAILED_STAGE

pipeline {
    agent any
    tools {
	    maven 'maven3'
	    jdk 'jdk11'
	    nodejs 'node14'
	} 
    //{
       // node {
         //   label 'goofy'
        //}
    //}

    environment {
        //BLUE_OCEAN_URL = "https://jenkins-dev-tools.apps.lab.okd.local/blue/organizations/jenkins/OZG%2Fgoofy/detail/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/pipeline"
        BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/blue/organizations/jenkins/OZG%2Fgoofy/detail/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/pipeline"
    }

    options {
        timeout(time: 1, unit: 'HOURS')
        disableConcurrentBuilds()
        buildDiscarder(logRotator(numToKeepStr: '5'))
    }

    stages {
        stage('Client') {
            steps {
//                container("nodejs"){
                    script {
                        FAILED_STAGE=env.STAGE_NAME

                        sh 'npm --version'
                        dir('goofy-client') {
                            sh 'npm install'
                        
                            if (env.BRANCH_NAME == 'release') {
                                sh 'npm run ci-prodBuild'
                            }
                            else {
                                sh 'npm run ci-build'
                            }

                            sh 'npm run ci-test'

                            if (env.BRANCH_NAME == 'master') {
                                withSonarQubeEnv('sonarqube'){
                                    sh 'npm run ci-sonar'
                                }
                            }
                        }
                    }
                //gi}
            }
            post {
                always{
                    junit testResults: 'goofy-client/test-report.xml', skipPublishingChecks: true
                }
            }
        }

        stage('Server') {
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME
                }
                container("maven-11"){
                    configFileProvider([configFile(fileId: '9aac2fac-7d8d-4128-846b-0a61951df703', variable: 'MAVEN_SETTINGS')]) {
                        sh 'mvn --version'
                        sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client clean install'

                        script {
                            if (env.BRANCH_NAME == 'master') {
        	                    dir('goofy-server'){
                                    withSonarQubeEnv('sonarqube'){
                                        sh 'mvn sonar:sonar'
                                    }
                                }
		                    }
                        }
                    }
                }
            }
            post {
                always{
                    junit testResults: '**/target/surefire-reports/*.xml', skipPublishingChecks: true
                }
            }
        } 

        stage('Openshift build') {
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME
                    def pom = readMavenPom file: 'pom.xml'
                    def version = pom.version

                    openshift.withCluster() {
                        openshift.withProject() {
                            dir('goofy-server') {
                                openshift.startBuild("goofy-builder", "--from-dir=.", "--follow", "--wait")
                            }
                         
                            openshift.tag("goofy-builder:build-latest", "goofy-builder:${env.BRANCH_NAME}-${version}")

                            if (env.BRANCH_NAME == 'release') {
                                openshift.tag("goofy-builder:${env.BRANCH_NAME}-${version}", "goofy-builder:latest")
                            }

                            if (env.BRANCH_NAME == 'master') {
                                def dc = openshift.selector('dc', 'goofy-e2e')

                                openshift.tag("goofy-builder:${env.BRANCH_NAME}-${version}", "goofy-builder:snapshot-latest")
                                openshift.tag("goofy-builder:${env.BRANCH_NAME}-${version}", "goofy-builder:e2e-latest")

                                dc.rollout().status()
                            }
                        }
                    }
                }
            }
        }

        stage('E2E-Test') {
            when {
                branch 'master'
            }
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME
                }
                container("cypress"){
                    sh 'npm --version'
                    dir('goofy-client') {
                        sh 'npm install'
                        sh 'npm run cypress:version'
                        sh 'npm run cypress:ci-run'
                    }
                }
            }
            post {
                always {
                    publishHTML (target: [
                    allowMissing: false,
                    alwaysLinkToLastBuild: false,
                    keepAll: true,
                    reportDir: 'goofy-client/apps/goofy-e2e/reports',
                    reportFiles: 'report.html',
                    reportName: "Goofy E2E-Tests"
                    ])
                }
            }
        }

        stage('nexus deploy'){
            when {
                anyOf {
                    branch 'master'
                    branch 'release'
                }
            }
            steps {
                script {
                    FAILED_STAGE = env.STAGE_NAME
                }
                container('maven-11') {
                    configFileProvider([configFile(fileId: '9aac2fac-7d8d-4128-846b-0a61951df703', variable: 'MAVEN_SETTINGS')]) {
                        sh 'mvn -s $MAVEN_SETTINGS -pl -goofy-client -DskipTests deploy'
                    }
                }
            }
        }

        stage('Openshift deploy') {
            when {
                anyOf {
                    branch 'master';
                    branch 'release'
                }
            }
            steps {
                script {
                    FAILED_STAGE=env.STAGE_NAME
                    def pom = readMavenPom file: 'pom.xml'
                    def version = pom.version

                    openshift.withCluster() {
                        openshift.withProject() {
                            openshift.tag("goofy-builder:${env.BRANCH_NAME}-${version}", "sh-kiel-dev/goofy:${env.BRANCH_NAME}-${version}")
                            
                            if (env.BRANCH_NAME == 'master') {
                                openshift.tag("goofy-builder:snapshot-latest", "sh-kiel-dev/goofy:snapshot-latest")
                            }

                            if (env.BRANCH_NAME == 'release') {
                                openshift.tag("goofy-builder:latest", "sh-kiel-dev/goofy:latest")
                            }
                        }
                        if (env.BRANCH_NAME == 'master') {
                            openshift.withProject('sh-kiel-dev') {
                                def dc = openshift.selector('deployments', 'goofy')

                                dc.rollout().status()
                            }
                        }
                    }
                }
            }
        }
    }
    post {
        failure {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackSend(color: "danger", message: "Goofy: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <${BLUE_OCEAN_URL}|${env.BUILD_NUMBER}>")
                }
            }
        }
    }
}