Skip to content
Snippets Groups Projects
Jenkinsfile 4.1 KiB
Newer Older
  • Learn to ignore specific revisions
  • OZGCloud's avatar
    OZGCloud committed
    def FAILED_STAGE
    
    pipeline {
        agent {
           node {
    
    OZGCloud's avatar
    OZGCloud committed
               label 'ozgcloud-jenkins-build-agent-jdk21'
    
    OZGCloud's avatar
    OZGCloud committed
            }
        }
    
        environment {
    
    Martin's avatar
    Martin committed
            JENKINS_URL = "https://jenkins.ozg-sh.de/job/codeSH_api-lib/job/${env.BRANCH_NAME}/${env.BUILD_NUMBER}/"
    
    OZGCloud's avatar
    OZGCloud committed
            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
                        if(env.BRANCH_NAME == 'release'){
                            if ( !(rootVersion ==~ RELEASE_REGEX)) {
    
    OZGCloud's avatar
    OZGCloud committed
                                error("Keine Release Version für Branch ${env.BRANCH_NAME}.")
    
    OZGCloud's avatar
    OZGCloud committed
                            }
                        } else {
                            if ( !(rootVersion ==~ SNAPSHOT_REGEX)) {
    
    OZGCloud's avatar
    OZGCloud committed
                                   error("Keine Snapshot Version für Branch ${env.BRANCH_NAME}.")
    
    OZGCloud's avatar
    OZGCloud committed
                            }
        	            }
        	        }
     	       }
     	   } //stage check version
    
            stage('Set Version') {
              when {
                not {
                    anyOf {
    
    Martin's avatar
    Martin committed
                        branch 'main'
    
                        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') {
     	       steps {
     	           script {
                        FAILED_STAGE=env.STAGE_NAME
                    }
    
    OZGCloud's avatar
    OZGCloud committed
                    configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
                         sh 'mvn -s $MAVEN_SETTINGS clean install'
                    }
    
    OZGCloud's avatar
    OZGCloud committed
     	       }
     	   } //stage build
    
    OZGCloud's avatar
    OZGCloud committed
     	   stage('Deploy to nexus') {
     	       steps {
     	           script {
     	               FAILED_STAGE = env.STAGE_NAME
     	           }
    				configFileProvider([configFile(fileId: 'maven-settings', variable: 'MAVEN_SETTINGS')]) {
    
    OZGCloud's avatar
    OZGCloud committed
                        sh 'mvn -s $MAVEN_SETTINGS -Pdeploy -DskipTests deploy'
    
                        sh 'mvn -s $MAVEN_SETTINGS versions:revert'
    
    
    OZGCloud's avatar
    OZGCloud committed
                    }
     	       }
     	   } //stage deploy
    
    OZGCloud's avatar
    OZGCloud committed
       } //stages
       post {
          failure {
              script {
    
    Martin's avatar
    Martin committed
                  if (isMainBranch() || env.BRANCH_NAME == 'release') {
    
    OZGCloud's avatar
    OZGCloud committed
                      sendFailureMessage()
    
    OZGCloud's avatar
    OZGCloud committed
          }
       }
    
    OZGCloud's avatar
    OZGCloud committed
    } //pipeline
    
    OZGCloud's avatar
    OZGCloud committed
    
    Void sendFailureMessage() {
        def room = ''
        def data = """{"msgtype":"m.text", \
    
    Martin's avatar
    Martin committed
                        "body":"api-lib: Build Failed. Stage: ${FAILED_STAGE} Build-ID: ${env.BUILD_NUMBER} Link: ${JENKINS_URL}", \
    
    OZGCloud's avatar
    OZGCloud committed
                        "format": "org.matrix.custom.html", \
    
    Martin's avatar
    Martin committed
                        "formatted_body":"api-lib: Build Failed. Stage: ${FAILED_STAGE} Build-ID: <a href='${JENKINS_URL}'>${env.BUILD_NUMBER}</a>"}"""
    
    Martin's avatar
    Martin committed
        if (isMainBranch()) {
    
    OZGCloud's avatar
    OZGCloud committed
            room = "!GjqhmouBtnDbwUkAjx:matrix.ozg-sh.de"
    
    OZGCloud's avatar
    OZGCloud committed
        }
        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
        }
    
    }
    
    String getPomVersion(String pomFile){
        def pom = readMavenPom file: pomFile
    
        return pom.version
    
    Martin's avatar
    Martin committed
    }
    
    Boolean isMainBranch() {
        return env.BRANCH_NAME == 'main'
    
    OZGCloud's avatar
    OZGCloud committed
    }