Jenkins: only release branches protected from SNAPSHOT

This commit is contained in:
Paul Campbell 2018-06-16 06:43:52 +01:00
parent 27d7bbb6d3
commit 3b89df6589

View file

@ -4,28 +4,22 @@ final dependenciesSupportJDK=9
pipeline { pipeline {
agent any agent any
stages { stages {
stage('master != SNAPSHOT') { stage('release != SNAPSHOT') {
// checks that the pom version is not a snapshot when the current or target branch is master // checks that the pom version is not a snapshot when the current or target branch is a release
when { when {
expression { 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") (readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT")
} }
} }
steps { steps {
error("Build failed because SNAPSHOT version") error("Build failed because SNAPSHOT version: [" + env.GIT_BRANCH + "][" + env.CHANGE_TARGET + "]")
} }
} }
stage('Build & Test') { stage('Build & Test') {
steps { steps {
withMaven(maven: 'maven', jdk: 'JDK LTS') { withMaven(maven: 'maven', jdk: 'JDK LTS') {
sh "${mvn} clean compile checkstyle:checkstyle pmd:pmd test" 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' jacoco exclusionPattern: '**/*{Test|IT|Main|Application|Immutable}.class'
pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: '' pmd canComputeNew: false, defaultEncoding: '', healthy: '', pattern: '', unHealthy: ''
step([$class : 'hudson.plugins.checkstyle.CheckStylePublisher', step([$class : 'hudson.plugins.checkstyle.CheckStylePublisher',
@ -52,14 +46,12 @@ pipeline {
} }
} }
} }
stage('Archiving') { stage('Deploy (release on gitlab)') {
when { expression { findFiles(glob: '**/target/*.jar').length > 0 } } when {
steps { expression {
archiveArtifacts '**/target/*.jar' (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 { steps {
withMaven(maven: 'maven', jdk: 'JDK LTS') { withMaven(maven: 'maven', jdk: 'JDK LTS') {
sh "${mvn} deploy --activate-profiles release -DskipTests=true" 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)
}