Skip to content

Instantly share code, notes, and snippets.

@stephenc
Last active May 4, 2018 13:36
Show Gist options
  • Save stephenc/67176221096acf9eb06c179feb540281 to your computer and use it in GitHub Desktop.
Save stephenc/67176221096acf9eb06c179feb540281 to your computer and use it in GitHub Desktop.
Watch Me Code, Episode 5 pipeline shared libraries

Watch Me Code - Episode 5

The build is defined in the vars/asfMavenTlpStdBuild.groovy file, that relies on the jenkinsEnv singelton.

Ideally you would put vars/jenkinsEnv.groovy in a separate repository (or at least a separate branch) and define two shared libraries:

  1. The shared library that defines jenkinsEnv
  2. The shared library that defines asfMavenTlpStdBuild

This way, to migrate to a different Jenkins master, we just need a different jenkinsEnv singleton that respects the same API contract.

def call(Map params = [:]) {
def oses = params.containsKey('os') ? params.os : ['linux', 'windows']
def jdks = params.containsKey('jdks') ? params.jdks : ['7','8']
def maven = params.containsKey('maven') ? params.maven : '3.x.x'
def failFast = params.containsKey('failFast') ? params.failFast : true
Map tasks = [failFast: failFast]
boolean first = true
for (String os in oses) {
for (def jdk in jdks) {
String label = jenkinsEnv.labelForOS(os);
String jdkName = jenkinsEnv.jdkFromVersion(os, "${jdk}")
String mvnName = jenkinsEnv.mvnFromVersion(os, "${maven}")
if (label == null || jdkName == null || mvnName == null) {
continue;
}
String stageId = "${os}-jdk${jdk}"
tasks[stageId] = {
node(label) {
stage("Checkout ${stageId}") {
checkout scm
}
stage("Build ${stageId}") {
withMaven(jdk:jdkName, maven:mvnName, mavenLocalRepo:'.repository') {
if (isUnix()) {
sh 'mvn clean verify'
} else {
bat 'mvn clean verify'
}
}
}
}
}
first = false
}
}
return parallel(tasks)
}
#!/usr/bin/env groovy
class jenkinsEnv implements Serializable {
def labelForOS(String os) {
switch (os) {
case 'mac':
return 'mac'
case 'linux':
return 'ubuntu'
default:
return null
}
}
def jdkFromVersion(String os, String version) {
switch (os) {
case 'linux':
switch (version) {
case '7':
return 'java-7'
case '8':
return 'java-8'
default:
return null
}
case 'mac':
switch(version) {
case '8':
return 'openjdk-8 (linux only)'
default:
return null
}
default:
return null
}
}
def mvnFromVersion(String os, String version) {
return '3.5.2'
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment