def FAILED_STAGE

pipeline {
    agent {
       node {
           label 'jenkins-build-agent'
        }
    }

    environment {
        BLUE_OCEAN_URL = "https://jenkins.ozg-sh.de/job/kop-common/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
        RELEASE_REGEX = /\d+.\d+.\d+/
        SNAPSHOT_REGEX = /\d+.\d+.\d+-SNAPSHOT/
    }

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

    stages {
    	stage('Check Version') {
    	    steps {
    	        script {
    	            FAILED_STAGE = env.STAGE_NAME
    	            def rootPom = readMavenPom file: 'pom.xml'
                    def rootVersion = rootPom.version
                    
                    if(env.BRANCH_NAME == 'release'){
                        if ( !(rootVersion ==~ RELEASE_REGEX)) {
                            error("Keine Release Version für Branch ${env.BRANCH_NAME}.")       
                        }
                    } else {
                        if ( !(rootVersion ==~ SNAPSHOT_REGEX)) {
                               error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")                    
                        }
    	            }
    	        }
 	       }
 	   } //stage check version
 	   stage('Build') {
 	       steps {
 	           script {
                    FAILED_STAGE=env.STAGE_NAME
                }
 	       }
 	   } //stage build
   } //stages
} //pipeline