From 3b89df65897021db1fdc0c55e4c93b5faecd99f3 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Sat, 16 Jun 2018 06:43:52 +0100 Subject: [PATCH] Jenkins: only release branches protected from SNAPSHOT --- Jenkinsfile.groovy | 40 ++++++++++++++++++---------------------- 1 file changed, 18 insertions(+), 22 deletions(-) diff --git a/Jenkinsfile.groovy b/Jenkinsfile.groovy index 38d1cae..0d3d2b8 100644 --- a/Jenkinsfile.groovy +++ b/Jenkinsfile.groovy @@ -1,37 +1,31 @@ final String mvn = "mvn --batch-mode --update-snapshots --errors" -final dependenciesSupportJDK=9 +final dependenciesSupportJDK = 9 pipeline { agent any stages { - stage('master != SNAPSHOT') { - // checks that the pom version is not a snapshot when the current or target branch is master + stage('release != SNAPSHOT') { + // checks that the pom version is not a snapshot when the current or target branch is a release when { expression { - (env.GIT_BRANCH == 'master' || env.CHANGE_TARGET == 'master') && + (startsWith(env.GIT_BRANCH, 'release') || startsWith(env.CHANGE_TARGET, 'release')) && (readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT") } } steps { - error("Build failed because SNAPSHOT version") + error("Build failed because SNAPSHOT version: [" + env.GIT_BRANCH + "][" + env.CHANGE_TARGET + "]") } } stage('Build & Test') { steps { withMaven(maven: 'maven', jdk: 'JDK LTS') { sh "${mvn} clean compile checkstyle:checkstyle pmd:pmd test" - //junit '**/target/surefire-reports/*.xml' - //sh "${mvn} jacoco:report com.gavinmogan:codacy-maven-plugin:coverage " + - // "-DcoverageReportFile=target/site/jacoco/jacoco.xml " + - // "-DprojectToken=`$JENKINS_HOME/codacy/token` " + - // "-DapiToken=`$JENKINS_HOME/codacy/apitoken` " + - // "-Dcommit=`git rev-parse HEAD`" jacoco exclusionPattern: '**/*{Test|IT|Main|Application|Immutable}.class' pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: '' - step([$class: 'hudson.plugins.checkstyle.CheckStylePublisher', - pattern: '**/target/checkstyle-result.xml', - healthy:'20', - unHealthy:'100']) + step([$class : 'hudson.plugins.checkstyle.CheckStylePublisher', + pattern : '**/target/checkstyle-result.xml', + healthy : '20', + unHealthy: '100']) } } } @@ -52,14 +46,12 @@ pipeline { } } } - stage('Archiving') { - when { expression { findFiles(glob: '**/target/*.jar').length > 0 } } - steps { - archiveArtifacts '**/target/*.jar' + stage('Deploy (release on gitlab)') { + when { + expression { + (env.GIT_BRANCH.startsWith('master') && env.GIT_URL.startsWith('https://gitlab.com')) + } } - } - stage('Deploy (master on gitlab)') { - when { expression { (env.GIT_BRANCH == 'master' && env.GIT_URL.startsWith('https://gitlab.com')) } } steps { withMaven(maven: 'maven', jdk: 'JDK LTS') { sh "${mvn} deploy --activate-profiles release -DskipTests=true" @@ -84,3 +76,7 @@ pipeline { } } } + +private boolean startsWith(String envSetting, String match) { + envSetting != null && envSetting.startsWith(match) +}