Jenkinsfile 4.6 KB
Newer Older
1 2
#!/usr/bin/env groovy

3
/*
4
 * This Jenkinsfile is intended to run on https://ci.jenkins.io and may fail anywhere else.
5
 * It makes assumptions about plugins being installed, labels mapping to nodes that can build what is needed, etc.
A
Andrew Bayer 已提交
6 7 8 9
 *
 * The required labels are "java" and "docker" - "java" would be any node that can run Java builds. It doesn't need
 * to have Java installed, but some setups may have nodes that shouldn't have heavier builds running on them, so we
 * make this explicit. "docker" would be any node with docker installed.
10 11 12 13
 */

// TEST FLAG - to make it easier to turn on/off unit tests for speeding up access to later stuff.
def runTests = true
A
Alex Earl 已提交
14
def failFast = false
15

16
properties([buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '20'))])
17

18
// see https://github.com/jenkins-infra/documentation/blob/master/ci.adoc for information on what node types are available
D
Daniel Beck 已提交
19
def buildTypes = ['Linux', 'Windows']
20

A
Alex Earl 已提交
21 22 23 24 25 26 27 28 29 30
def builds = [:]
for(i = 0; i < buildTypes.size(); i++) {
    def buildType = buildTypes[i]
    builds[buildType] = {
        node(buildType.toLowerCase()) {
            timestamps {
                // First stage is actually checking out the source. Since we're using Multibranch
                // currently, we can use "checkout scm".
                stage('Checkout') {
                    checkout scm
31
                }
32

A
Alex Earl 已提交
33 34 35 36 37
                // Now run the actual build.
                stage("${buildType} Build / Test") {
                    timeout(time: 180, unit: 'MINUTES') {
                        // See below for what this method does - we're passing an arbitrary environment
                        // variable to it so that JAVA_OPTS and MAVEN_OPTS are set correctly.
B
Baptiste Mathus 已提交
38 39
                        withMavenEnv(["JAVA_OPTS=-Xmx1536m -Xms512m",
                                    "MAVEN_OPTS=-Xmx1536m -Xms512m"]) {
A
Alex Earl 已提交
40
                            // Actually run Maven!
41
                            // -Dmaven.repo.local=… tells Maven to create a subdir in the temporary directory for the local Maven repository
42
                            def mvnCmd = "mvn -Pdebug -U javadoc:javadoc clean install ${runTests ? '-Dmaven.test.failure.ignore' : '-DskipTests'} -V -B -Dmaven.repo.local=${pwd tmp: true}/m2repo -s settings-azure.xml -e"
A
Alex Earl 已提交
43 44
                            if(isUnix()) {
                                sh mvnCmd
45
                                sh 'test `git status --short | tee /dev/stderr | wc --bytes` -eq 0'
A
Alex Earl 已提交
46
                            } else {
47
                                bat mvnCmd
A
Alex Earl 已提交
48 49 50 51 52 53
                            }
                        }
                    }
                }

                // Once we've built, archive the artifacts and the test results.
54
                stage("${buildType} Publishing") {
A
Alex Earl 已提交
55 56
                    def files = findFiles(glob: '**/target/*.jar, **/target/*.war, **/target/*.hpi')
                    renameFiles(files, buildType.toLowerCase())
57

A
Alex Earl 已提交
58 59 60
                    archiveArtifacts artifacts: '**/target/*.jar, **/target/*.war, **/target/*.hpi',
                                fingerprint: true
                    if (runTests) {
61
                        junit healthScaleFactor: 20.0, testResults: '*/target/surefire-reports/*.xml'
A
Alex Earl 已提交
62 63
                    }
                }
64
            }
65
        }
66 67 68
    }
}

A
Alex Earl 已提交
69 70 71
builds.failFast = failFast
parallel builds

72 73 74 75 76 77 78 79
// This method sets up the Maven and JDK tools, puts them in the environment along
// with whatever other arbitrary environment variables we passed in, and runs the
// body we passed in within that environment.
void withMavenEnv(List envVars = [], def body) {
    // The names here are currently hardcoded for my test environment. This needs
    // to be made more flexible.
    // Using the "tool" Workflow call automatically installs those tools on the
    // node.
80 81
    String mvntool = tool name: "mvn", type: 'hudson.tasks.Maven$MavenInstallation'
    String jdktool = tool name: "jdk8", type: 'hudson.model.JDK'
82 83 84 85 86 87 88 89 90 91 92 93 94

    // Set JAVA_HOME, MAVEN_HOME and special PATH variables for the tools we're
    // using.
    List mvnEnv = ["PATH+MVN=${mvntool}/bin", "PATH+JDK=${jdktool}/bin", "JAVA_HOME=${jdktool}", "MAVEN_HOME=${mvntool}"]

    // Add any additional environment variables.
    mvnEnv.addAll(envVars)

    // Invoke the body closure we're passed within the environment we've created.
    withEnv(mvnEnv) {
        body.call()
    }
}
A
Alex Earl 已提交
95 96 97 98 99 100 101 102 103 104 105 106

void renameFiles(def files, String prefix) {
    for(i = 0; i < files.length; i++) {
        def newPath = files[i].path.replace(files[i].name, "${prefix}-${files[i].name}")
        def rename = "${files[i].path} ${newPath}"
        if(isUnix()) {
            sh "mv ${rename}"
        } else {
            bat "move ${rename}"
        }
    }
}