Skip to content
Snippets Groups Projects
Jenkinsfile 6.04 KiB
Newer Older
  • Learn to ignore specific revisions
  • def FAILED_STAGE
    
    pipeline {
        agent {
    
    OZGCloud's avatar
    OZGCloud committed
           node {
    
    OZGCloud's avatar
    OZGCloud committed
               label 'ozgcloud-jenkins-build-agent-jdk21'
    
            }
        }
    
        environment {
    
            BLUE_OCEAN_URL = "https://jenkins.infra.ozg-cloud.systems/job/ozgcloud-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
                        
    
    OZGCloud's avatar
    OZGCloud committed
                        def dependenciesPom = readMavenPom file: 'ozgcloud-common-dependencies/pom.xml'
    
                        def dependenciesVersion = dependenciesPom.parent.version
                        
    
    OZGCloud's avatar
    OZGCloud committed
                        def parentPom = readMavenPom file: 'ozgcloud-common-parent/pom.xml'
    
                        if(env.BRANCH_NAME == 'release'){
    
                            if ( !(rootVersion ==~ RELEASE_REGEX) || !(dependenciesVersion ==~ RELEASE_REGEX) || !(parentVersion ==~ RELEASE_REGEX)) {
                                error("Keine Release Version für Branch ${env.BRANCH_NAME}.")
                            }
                        } else {
                            if ( !(rootVersion ==~ SNAPSHOT_REGEX) || !(dependenciesVersion ==~ SNAPSHOT_REGEX) || !(parentVersion ==~ SNAPSHOT_REGEX)) {
                                error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")
                            }
                        }
    
                        if( !(rootVersion == dependenciesVersion && rootVersion == parentVersion)){
                            error("Versionen sind nicht identisch")                        
                        }                    
                    }
                }
    
           stage('Set Version') {
              when {
                not {
                    anyOf {
                        branch 'master'
                        branch 'release'
                    }
                }
              }
              steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                        JAR_TAG = getPomVersion('pom.xml').replace("SNAPSHOT", "${env.BRANCH_NAME}-SNAPSHOT")
                    }
                    configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                        sh "mvn -s $MAVEN_SETTINGS versions:set -DnewVersion=${JAR_TAG} -DprocessAllModules=true"
                        
                    }
              }
            }  
    
    OZGCloud's avatar
    OZGCloud committed
            stage('Build OzgCloud-Common Dependencies') {
    
                 steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                    }
    
    OZGCloud's avatar
    OZGCloud committed
               		dir('ozgcloud-common-dependencies') {
    
    OZGCloud's avatar
    OZGCloud committed
                      	configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                           	sh 'mvn --version'
    
    OZGCloud's avatar
    OZGCloud committed
                           	sh 'mvn --no-transfer-progress -s $MAVEN_SETTINGS clean install'
    
    OZGCloud's avatar
    OZGCloud committed
                        }
    
    OZGCloud's avatar
    OZGCloud committed
            stage('Build OzgCloud-Common') {
    
                steps {
                    script {
                        FAILED_STAGE=env.STAGE_NAME
                    }
    
    OZGCloud's avatar
    OZGCloud committed
                    configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                        sh 'mvn --version'
    
    OZGCloud's avatar
    OZGCloud committed
                        sh 'mvn --no-transfer-progress -s $MAVEN_SETTINGS clean install'
    
    OZGCloud's avatar
    OZGCloud committed
    	                script {
    
    OZGCloud's avatar
    OZGCloud committed
    	              	    dir('ozgcloud-common-lib') {
    
    OZGCloud's avatar
    OZGCloud committed
    	                    	try {
    
    OZGCloud's avatar
    OZGCloud committed
    	                            if (env.BRANCH_NAME == 'master') {
    	                                withSonarQubeEnv('sonarqube-ozg-sh'){
        					    			sh 'mvn sonar:sonar'	                                
    	                                }
    	                            }
    	                        } catch (Exception e) {
    	                            unstable("SonarQube failed")
    
    OZGCloud's avatar
    OZGCloud committed
    	                        }
    
    OZGCloud's avatar
    OZGCloud committed
    	                    }
    
    OZGCloud's avatar
    OZGCloud committed
                }
    
    OZGCloud's avatar
    OZGCloud committed
             }
    
    OZGCloud's avatar
    OZGCloud committed
            stage('Deploy OzgCloud-Common to Nexus'){
    
                steps {
                    script {
                        FAILED_STAGE = env.STAGE_NAME
                    }
    
    OZGCloud's avatar
    OZGCloud committed
                    configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
    
    OZGCloud's avatar
    OZGCloud committed
                        sh 'mvn --no-transfer-progress -s $MAVEN_SETTINGS deploy'
    
                        sh "mvn -s $MAVEN_SETTINGS versions:revert"
    
                    }
                }
            }
        }
        post {
            failure {
                script {
    
                    if (env.BRANCH_NAME == 'master' || env.BRANCH_NAME == 'release') {
    
                        sendFailureMessage()
    
    }
    
    Void sendFailureMessage() {
        def room = ''
        def data = """{"msgtype":"m.text", \
    
    OZGCloud's avatar
    OZGCloud committed
                        "body":"ozgcloud-common: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${BLUE_OCEAN_URL}", \
    
                        "format": "org.matrix.custom.html", \
    
    OZGCloud's avatar
    OZGCloud committed
                        "formatted_body":"ozgcloud-common: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <a href='${BLUE_OCEAN_URL}'>${env.BUILD_NUMBER}</a>"}"""
    
           
        if (env.BRANCH_NAME == 'master') {
    
    OZGCloud's avatar
    OZGCloud committed
            room = "!GjqhmouBtnDbwUkAjx:matrix.ozg-sh.de"
    
        else if (env.BRANCH_NAME == 'release') {
    
            room = "!oWZpUGTFsxkJIYNfYg:matrix.ozg-sh.de"
        }
    
        sh "curl -XPOST -H 'authorization: Bearer ${getElementAccessToken()}' -d '${data}' https://matrix.ozg-sh.de/_matrix/client/v3/rooms/$room/send/m.room.message"
    }
    
    String getElementAccessToken() {
        withCredentials([string(credentialsId: 'element-login-json', variable: 'LOGIN_JSON')]) {
            return readJSON ( text: sh (script: '''curl -XPOST -d \"$LOGIN_JSON\" https://matrix.ozg-sh.de/_matrix/client/v3/login''', returnStdout: true)).access_token
        }
    
    OZGCloud's avatar
    OZGCloud committed
    }
    
    
    String getPomVersion(String pomFile){
        def pom = readMavenPom file: pomFile
    
        return pom.version
    }