pipeline {
    agent { label 'jenkins-slave' }
    environment {
        DOCKER_REPO = "opengnsys"
        DOCKER_CREDENTIALS = credentials('docker-hub-credentials') 
        DOCKER_IMAGE_NAME = "oggui"
    }
    stages {
        stage ('Checkout') {
            steps {
                checkout scm
            }
        }   
        stage('Build Testing Image') {
            steps {
                sh "printenv"
                echo 'Building....'
                script {
                    def DOCKER_TAG = "${env.BUILD_NUMBER}"
                    
                    dir('ogWebconsole') {
                        IMAGE_ID = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}:${BRANCH_NAME}-${DOCKER_TAG}"
                        IMAGE_ID_TESTING = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}:${BRANCH_NAME}-${DOCKER_TAG}-testing"
                        if (BRANCH_NAME == 'main') {
                            LATEST_ID = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}:latest"
                        }  else {
                            LATEST_ID = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}:${BRANCH_NAME}-latest"
                        }

                        env.IMAGE_ID_TESTING = IMAGE_ID_TESTING
                        env.IMAGE_ID = IMAGE_ID 
                        env.LATEST_ID = LATEST_ID
                        docker.build("${IMAGE_ID_TESTING}", "-f Dockerfile-testing .")
                        if (env.TAG_NAME) {
                            TAG_ID = "${DOCKER_REPO}/${DOCKER_IMAGE_NAME}:${env.TAG_NAME}"
                        }
                    }
                }
            }
        }
        stage('Testing') {
            steps {
                echo 'Running Tests....'
                sh '''
                    cd ogWebconsole
                    docker run -p 4200:4200 --name oggui-testing -e CHROME_BIN=/usr/bin/chromium -v $(pwd)/karma.conf.js:/app/karma.conf.js -v $(pwd)/.env:/app/.env -d $IMAGE_ID_TESTING
                    docker exec oggui-testing ng test --watch=false --source-map=false --karma-config=karma.conf.js
                '''
            }
        }
        stage('Build') {
            steps {
                echo 'Building....'
                script {
                    dir('ogWebconsole') {
                        docker.build("${IMAGE_ID}", "-f Dockerfile .")
                        docker.build("${LATEST_ID}", "-f Dockerfile .")
                        if (env.TAG_NAME) {
                            docker.build("${TAG_ID}", "-f Dockerfile .")
                        }
                    }
                }
            }
        }
        stage('Push') {
            steps {
                echo 'Pushing....'
                script {
                    docker.withRegistry('https://index.docker.io/v1/', 'docker-hub-credentials') {
                        docker.image("${IMAGE_ID}").push()
                        docker.image("${LATEST_ID}").push()
                        if (env.TAG_NAME) {
                            docker.image("${TAG_ID}").push()
                        }
                    }
                }
            }
        }
    }
    post {
        always {
            echo 'Get test results....'
            sh "mkdir -p test-results"
            sh "docker cp oggui-testing:/app/test-results/ogGui-junit-report.xml ./test-results/ogGui-junit-report.xml"
            junit '**/test-results/*.xml'
            echo 'Cleaning up....'
            sh "docker stop oggui-testing"
            sh "docker rm oggui-testing"
            sh "docker rmi ${IMAGE_ID} || true"
            sh "docker rmi ${LATEST_ID} || true"
            sh "docker rmi ${IMAGE_ID_TESTING} || true"
            script {
                def committerEmail = sh (
                    script: "git show -s --pretty=%ae",
                    returnStdout: true
                ).trim()
                def buildResult = currentBuild.currentResult
                mail to: committerEmail, 
                subject: "Opengnsys CI Build ${env.JOB_NAME} - ${env.BRANCH_NAME} - ${buildResult}", 
                body: """
                <h1>Opengnsys CI Build ${JOB_NAME} - ${BRANCH_NAME} - ${buildResult}</h1>
                <p>Build Number: ${BUILD_NUMBER}</p>
                <p>Build URL: ${BUILD_URL}</p>º

                Saludos cordiales,
                Opengnsys CI
                """
            }
        }
    }
}
