Rewrite Jenkinsfile for resource only project with trunk-style branching model

This commit is contained in:
Paul Campbell 2018-07-03 06:35:04 +00:00
parent 707207df44
commit e91f600d8d

View file

@ -1,39 +1,67 @@
final String mvn = "mvn --batch-mode --update-snapshots" final String mvn = "mvn --batch-mode --update-snapshots --errors"
final Integer dependenciesSupportJDK = 10
pipeline { pipeline {
agent any agent any
stages { stages {
stage('no SNAPSHOT in master') { stage('release != SNAPSHOT') {
// checks that the pom version is not a snapshot when the current branch is master // checks that the pom version is not a SNAPSHOT when the current branch is a release
// TODO: also check for SNAPSHOT when is a pull request with master as the target branch
when { when {
expression { expression {
(env.GIT_BRANCH == 'master') && (branchStartsWith('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 + "]")
} }
} }
stage('Build') { stage('Install') {
steps { steps {
withMaven(maven: 'maven 3.5.2', jdk: 'JDK 9') { withMaven(maven: 'maven', jdk: 'JDK 1.8') {
sh 'mvn clean install' sh "${mvn} -DskipTests install"
} }
} }
} }
stage('Archiving') { stage('Deploy (release on gitlab)') {
when {
expression {
(branchStartsWith('release/') && isPublished())
}
}
steps { steps {
archiveArtifacts '**/classes/**/*.xml' withMaven(maven: 'maven', jdk: 'JDK 1.8') {
sh "${mvn} --activate-profiles release deploy"
}
} }
} }
stage('Deploy') { stage('Build Java 9') {
when { expression { (env.GIT_BRANCH == 'master') } } when { expression { dependenciesSupportJDK >= 9 } }
steps { steps {
withMaven(maven: 'maven 3.5.2', jdk: 'JDK 1.8') { withMaven(maven: 'maven', jdk: 'JDK 9') {
sh "${mvn} deploy --activate-profiles release -DskipTests=true" sh "${mvn} clean verify -Djava.version=9"
}
}
}
stage('Build Java 10') {
when { expression { dependenciesSupportJDK >= 10 } }
steps {
withMaven(maven: 'maven', jdk: 'JDK 10') {
sh "${mvn} clean verify -Djava.version=10"
} }
} }
} }
} }
} }
private boolean branchStartsWith(String branchName) {
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)
}