forked from CMSgov/bluebutton-data-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
136 lines (119 loc) · 4.57 KB
/
Jenkinsfile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#!/usr/bin/env groovy
/**
* <p>
* This is the script that will be run by Jenkins to build and test this
* project. This drives the project's continuous integration and delivery.
* </p>
* <p>
* This script is run by Jenkins' Pipeline feature. A good intro tutorial for
* working with Jenkins Pipelines can be found here:
* <a href="https://jenkins.io/doc/book/pipeline/">Jenkins > Pipeline</a>.
* </p>
* <p>
* The canonical Jenkins server job for this project is located here:
* <a href="https://builds.ls.r53.cmsfhir.systems/jenkins/job/bluebutton-data-model">bluebutton-data-model</a>.
* </p>
*/
properties([
pipelineTriggers([
triggers: [[
$class: 'jenkins.triggers.ReverseBuildTrigger',
upstreamProjects: "bluebutton-parent-pom/master", threshold: hudson.model.Result.SUCCESS
]]
]),
buildDiscarder(logRotator(artifactDaysToKeepStr: '', artifactNumToKeepStr: '', daysToKeepStr: '', numToKeepStr: ''))
])
stage('Checkout') {
node {
// Grab the commit that triggered the build.
checkout scm
// Update the POM version so that the artifacts produced by this build
// are distinguishable from other builds.
setPomVersionUsingBuildId()
}
}
stage('Build') {
node {
milestone(label: 'stage_build_start')
mvn "--update-snapshots -Dmaven.test.failure.ignore clean install"
}
}
stage('Archive') {
node {
// Fingerprint the output artifacts and archive the test results.
// (Archiving the artifacts here would waste space, as the build
// deploys them to the local Maven repository.)
fingerprint '**/target/*.jar'
junit testResults: '**/target/*-reports/TEST-*.xml', keepLongStdio: true
archiveArtifacts artifacts: '**/target/*-reports/*.txt', allowEmptyArchive: true
}
}
/**
* Runs Maven with the specified arguments.
*
* @param args the arguments to pass to <code>mvn</code>
*/
def mvn(args) {
// This tool must be setup and named correctly in the Jenkins config.
def mvnHome = tool 'maven-3'
// Run the build, using Maven, with the appropriate config.
configFileProvider(
[
configFile(fileId: 'bluebutton:settings.xml', variable: 'MAVEN_SETTINGS'),
configFile(fileId: 'bluebutton:toolchains.xml', variable: 'MAVEN_TOOLCHAINS')
]
) {
sh "${mvnHome}/bin/mvn --settings $MAVEN_SETTINGS --toolchains $MAVEN_TOOLCHAINS ${args}"
}
}
/**
* @return the <version /> from the POM of the project in the current working
* directory.
*/
def readPomVersion() {
// Reference: http://stackoverflow.com/a/26514030/1851299
mvn "--quiet --non-recursive -Dexec.executable='echo' -Dexec.args='\${project.version}' org.codehaus.mojo:exec-maven-plugin:1.3.1:exec > pom.project.version.txt"
pomProjectVersion = readFile('pom.project.version.txt').trim()
sh "rm -f pom.project.version.txt"
echo "Current POM version: ${pomProjectVersion}"
return pomProjectVersion
}
/**
* @return an ID for the current build, in the form of
* "<code>${env.BRANCH_NAME}-${env.BUILD_NUMBER}</code>"
*/
def calculateBuildId() {
gitBranchName = "${env.BRANCH_NAME}".toString()
gitCommitId = sh(script: "git rev-parse HEAD", returnStdout: true).trim()
buildId = "${gitBranchName}-${gitCommitId}"
echo "Build ID: ${buildId} (for branch ${gitBranchName})"
return buildId
}
/**
* <p>
* The same artifact repository will store builds produced from all source code
* branches. Unfortunately, Maven's versioning scheme doesn't really account
* for branches: unless the version numbers are manually changed for each
* branch (and there isn't any graceful scheme that would accommodate that),
* the "latest <code>-SNAPSHOT</code>" artifact included in builds could be
* from any branch.
* </p>
* <p>
* This method is used to workaround that problem. For non-<code>master</code>
* branch builds, the "<code>-SNAPSHOT</code> version qualifier is replaced
* with "<code>-<branchName>-<commitSha1></code>, ensuring that
* those builds don't "pollute" the <code>master</code> branch's artifact
* history. Version numbers for <code>master</code> branch builds are left
* alone.
* </p>
*/
def setPomVersionUsingBuildId() {
gitBranchName = "${env.BRANCH_NAME}".toString()
if (!"master".equals(gitBranchName)) {
pomProjectVersionWithBuildId = readPomVersion().replaceAll("SNAPSHOT", calculateBuildId())
// Update the POM version to include the build ID.
// Reference: https://maven.apache.org/maven-release/maven-release-plugin/examples/update-versions.html
mvn "--batch-mode --quiet org.codehaus.mojo:versions-maven-plugin:2.2:set -DnewVersion='${pomProjectVersionWithBuildId}' -DgenerateBackupPoms=false"
echo "Updated POM version: ${pomProjectVersionWithBuildId}"
}
}