jenkins: more refactor environment checking and switch deploy branch

This commit is contained in:
Paul Campbell 2018-06-20 19:00:48 +01:00
parent e395b54db4
commit 32354fe8ea

View file

@ -5,15 +5,15 @@ pipeline {
agent any agent any
stages { stages {
stage('release != SNAPSHOT') { stage('release != SNAPSHOT') {
// checks that the pom version is not a snapshot when the current or target branch is a release // checks that the pom version is not a SNAPSHOT when the current branch is a release
when { when {
expression { expression {
(startsWith(env.GIT_BRANCH, 'release') || startsWith(env.CHANGE_TARGET, 'release')) && (branchStartsWith('release/')) &&
(readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT") (readMavenPom(file: 'pom.xml').version).contains("SNAPSHOT")
} }
} }
steps { steps {
error("Build failed because SNAPSHOT version: [" + env.GIT_BRANCH + "][" + env.CHANGE_TARGET + "]") error("Build failed because SNAPSHOT version: [" + env.GIT_BRANCH + "]")
} }
} }
stage('Build & Test') { stage('Build & Test') {
@ -37,7 +37,7 @@ pipeline {
} }
} }
stage('SonarQube (gitlab only)') { stage('SonarQube (gitlab only)') {
when { expression { env.GIT_URL.startsWith('https://gitlab.com') } } when { expression { isPublished() } }
steps { steps {
withSonarQubeEnv('sonarqube') { withSonarQubeEnv('sonarqube') {
withMaven(maven: 'maven', jdk: 'JDK LTS') { withMaven(maven: 'maven', jdk: 'JDK LTS') {
@ -49,7 +49,7 @@ pipeline {
stage('Deploy (release on gitlab)') { stage('Deploy (release on gitlab)') {
when { when {
expression { expression {
(env.GIT_BRANCH.startsWith('master') && env.GIT_URL.startsWith('https://gitlab.com')) (branchStartsWith('release/') && isPublished())
} }
} }
steps { steps {
@ -77,6 +77,14 @@ pipeline {
} }
} }
private boolean startsWith(String envSetting, String match) { private boolean branchStartsWith(String branchName) {
envSetting != null && envSetting.startsWith(match) startsWith(env.GIT_BRANCH, branchName)
}
private boolean isPublished() {
startsWith(env.GIT_URL, 'https://')
}
private boolean startsWith(String value, String match) {
value != null && value.startsWith(match)
} }