This repository has been archived by the owner on Jul 15, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsFile
266 lines (238 loc) · 10.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/groovy
node {
def artifactId = "geojsongpkgconverter"
def appName = "bf-geojson-geopkg-converter"
def root = pwd()
def mvn = tool 'M3'
def zapHome = tool 'ZAProxy_v2_5_0'
def nodejs = tool 'NodeJS_6'
// Deploy and integration test function abstractions
def deployPhase = { String pcfSpace, String pcfDomain, String fullAppName ->
if(!fileExists('.cf')) {
sh "mkdir -p .cf"
}
withEnv(["CF_HOME=.cf"]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.PCF_CREDS}", usernameVariable: "CFUSER", passwordVariable: "CFPASS"]]) {
sh """
cf api ${env.PCF_API_ENDPOINT}
cf auth ${CFUSER} ${CFPASS}
cf target -o ${env.PCF_ORG} -s ${pcfSpace}
cf push ${fullAppName} -f manifest.jenkins.yml \
-p ${root}/${fullAppName}.jar \
-d ${pcfDomain} \
-b ${env.JAVA_BUILDPACK_NAME} \
--hostname ${fullAppName} \
--no-start
cf set-env ${fullAppName} PIAZZA_URL ${env.PIAZZA_URL}
"""
try {
sh "cf start ${fullAppName}"
} catch (Exception e) {
sh "cf logs --recent ${fullAppName}"
sh "cf delete ${fullAppName} -f"
error("Error during application start. Deleting ${fullAppName} and failing the build.")
}
}
}
if(!params.SKIP_SCANS) {
withCredentials([[$class: 'StringBinding', credentialsId: "${env.THREADFIX_API_KEY}", variable: "THREADFIX_KEY"]]) {
sh """
mkdir -p ${root}/zap-out
${zapHome}/zap.sh -cmd -quickout ${root}/zap-out/zap.xml -quickurl https://${fullAppName}.${pcfDomain}
cat ${root}/zap-out/zap.xml
/bin/curl -v --insecure -H 'Accept: application/json' -X POST --form file=@${root}/zap-out/zap.xml ${env.THREADFIX_URL}/rest/latest/applications/${THREADFIX_ID}/upload?apiKey=${THREADFIX_KEY}
"""
}
}
withEnv(["CF_HOME=.cf"]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.PCF_CREDS}", usernameVariable: "CFUSER", passwordVariable: "CFPASS"]]) {
sh """
cf api ${env.PCF_API_ENDPOINT}
cf auth ${CFUSER} ${CFPASS}
cf target -o ${env.PCF_ORG} -s ${pcfSpace}
"""
def legacyAppNames = sh(script: """cf routes | grep \"${appName} \" | awk '{print \$4}'""", returnStdout: true)
sh "cf map-route ${fullAppName} ${pcfDomain} --hostname bf-geojson-geopkg-converter"
// Remove legacy applications
for (Object legacyApp : legacyAppNames.trim().tokenize(',')) {
def legacyAppName = legacyApp.toString().trim()
if(legacyAppName != appName && legacyAppName != fullAppName) {
sh "cf unmap-route ${legacyAppName} ${pcfDomain} --hostname ${appName}"
sh "cf delete -f ${legacyAppName}"
}
}
}
}
}
def integrationTestPhase = { String pcfSpace, String pcfDomain ->
if(!fileExists("integration-tests")) {
sh "mkdir -p integration-tests"
}
// Clone Test Repository
withCredentials([file(credentialsId: "${POSTMAN_SECRET_FILE}", variable: 'POSTMAN_FILE')]) {
withEnv([
"PATH+=${root}/integration-tests/node_modules/newman/bin:${nodejs}/bin",
"PCF_SPACE=${pcfSpace}", "HOME=${WORKSPACE}"
]) {
dir ("integration-tests") {
if(env.INTEGRATION_GITLAB_CREDS) {
git url: "${env.INTEGRATION_GIT_URL}", branch: "${env.INTEGRATION_GIT_BRANCH}", credentialsId: "${env.INTEGRATION_GITLAB_CREDS}"
} else {
git url: "${env.INTEGRATION_GIT_URL}", branch: "${env.INTEGRATION_GIT_BRANCH}"
}
sh "npm install newman@3"
sh "/bin/bash ci/beachfront.sh"
}
}
}
}
// Actual pipeline stages
stage("Setup") {
deleteDir()
if(env.GITLAB_CREDS) {
git url: "${env.GIT_URL}", branch: "${env.GIT_BRANCH}", credentialsId: "${env.GITLAB_CREDS}"
} else {
git url: "${env.GIT_URL}", branch: "${env.GIT_BRANCH}"
}
}
def appvers = sh(script: """git describe --long --tags --always | sed 's/\\./-/'g""", returnStdout: true)
appvers = appvers.trim()
def fullAppName = "${appName}-${appvers}"
// TODO: Dependencies should probably have their own pipeline rather than being built as part of this one
stage("Build/Push Dependencies") {
sh """
mkdir -p ${root}/deps
mkdir -p ${root}/deps/geopackage-wkb-java
mkdir -p ${root}/deps/geopackage-core-java
mkdir -p ${root}/deps/geopackage-java
"""
dir("${root}/deps/geopackage-wkb-java") {
if(env.GITLAB_CREDS) {
git url: "${env.GIT_BASE_URL}geopackage-wkb-java.git", branch: "${env.GIT_BRANCH}", credentialsId: "${env.GITLAB_CREDS}"
} else {
git url: "${env.GIT_BASE_URL}geopackage-wkb-java.git", branch: "${env.GIT_BRANCH}"
}
}
dir("${root}/deps/geopackage-core-java") {
if(env.GITLAB_CREDS) {
git url: "${env.GIT_BASE_URL}geopackage-core-java.git", branch: "${env.GIT_BRANCH}", credentialsId: "${env.GITLAB_CREDS}"
} else {
git url: "${env.GIT_BASE_URL}geopackage-core-java.git", branch: "${env.GIT_BRANCH}"
}
}
dir("${root}/deps/geopackage-java") {
if(env.GITLAB_CREDS) {
git url: "${env.GIT_BASE_URL}geopackage-java.git", branch: "${env.GIT_BRANCH}", credentialsId: "${env.GITLAB_CREDS}"
} else {
git url: "${env.GIT_BASE_URL}geopackage-java.git", branch: "${env.GIT_BRANCH}"
}
}
def buildAndPushDep = { String dep, String depGroupId, String depArtifactId ->
dir("${root}/deps/${dep}") {
// Use the real version from pom.xml
def version = sh(script: """xmllint --xpath "//*[local-name()='project']/*[local-name()='version']/text()" pom.xml""", returnStdout: true)
version = version.trim()
echo "Building and pushing ${dep}@${version}"
if(!params.SKIP_SCANS) {
stage("${dep} Scan") {
withCredentials([[$class: "StringBinding", credentialsId: "${env.THREADFIX_API_KEY}", variable: "THREADFIX_KEY"]]) {
// Dependency Checker
sh "/jslave/dependency-check/dependency-check/bin/dependency-check.sh --version"
sh '/jslave/dependency-check/dependency-check/bin/dependency-check.sh --project "GeoPackage ${dep}" --scan "." --format "XML" --enableExperimental --disableBundleAudit'
sh "/bin/curl -v --insecure -H 'Accept: application/json' -X POST --form file=@dependency-check-report.xml ${env.THREADFIX_URL}/rest/latest/applications/${env.THREADFIX_ID}/upload?apiKey=${THREADFIX_KEY}"
// Fortify
sh "/opt/hp_fortify_sca/bin/sourceanalyzer -b ${env.BUILD_NUMBER} src/main/java/{*.java,**/*.java}"
sh "/opt/hp_fortify_sca/bin/sourceanalyzer -b ${env.BUILD_NUMBER} -scan -Xmx1G -f fortifyResults-${env.BUILD_NUMBER}.fpr"
sh "/bin/curl -v --insecure -H 'Accept: application/json' -X POST --form file=@fortifyResults-${env.BUILD_NUMBER}.fpr ${env.THREADFIX_URL}/rest/latest/applications/${env.THREADFIX_ID}/upload?apiKey=${THREADFIX_KEY}"
// TODO: Sonar targets must be added to Maven POM in order to enable Sonar scans.
}
}
}
def getDependencyStatus = sh(script: """mvn -X --settings ~/.m2/settings.xml dependency:get \
-DrepositoryId=nexus \
-DremoteRepositories="nexus::default::${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
-Durl="${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
-Dmaven.repo.local="${root}/.m2/repository" \
-Dpackaging=jar \
-DgroupId=${depGroupId} \
-DartifactId=${depArtifactId} \
-Dversion=${version} \
""", returnStatus: true)
echo "dependency status = ${getDependencyStatus}"
if(getDependencyStatus == 0) {
echo "Artifact version ${appvers} exists in Nexus, nothing to do"
return
}
sh """
${mvn}/bin/mvn clean install -U -Dmaven.repo.local=${root}/.m2/repository
DEPENDENCY_JAR=\$(ls target/ | awk '!/(sources|javadoc).jar/ && /.jar\$/ {print \$1; exit}')
mvn -X --settings ~/.m2/settings.xml deploy:deploy-file -Dfile=target/\$DEPENDENCY_JAR \
-DrepositoryId=nexus \
-Durl="${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
-Dmaven.repo.local="${root}/.m2/repository" \
-DpomFile=pom.xml \
-Dpackaging=jar \
-DgroupId=${depGroupId} \
-DartifactId=${depArtifactId} \
-Dversion=${version} \
"""
}
}
buildAndPushDep("geopackage-wkb-java", "mil.nga", "wkb")
buildAndPushDep("geopackage-core-java", "mil.nga.geopackage", "geopackage-core")
buildAndPushDep("geopackage-java", "mil.nga.geopackage", "geopackage")
}
stage("Archive") {
// Build Tarball
def targetFiles = "${fullAppName}.jar manifest.jenkins.yml"
def artifactDirectory = "maven-artifact"
sh "mkdir -p ${root}/${artifactDirectory}"
def tarball = "bf-geojson-geopkg-converter.tar.gz"
sh """
sed -i 's,\${env.ARTIFACT_STORAGE_URL},${env.ARTIFACT_STORAGE_URL},g' pom.xml
${mvn}/bin/mvn clean package -U -Dmaven.repo.local=${root}
cp ${root}/target/geojsongpkgconverter-1.0.0.jar ${root}/${fullAppName}.jar
tar cvvzf ${tarball} ${targetFiles}
mv ${tarball} ${root}/${artifactDirectory}/${tarball}
"""
dir("${root}/${artifactDirectory}") {
def getDependencyStatus = sh(script: """mvn -X --settings ~/.m2/settings.xml dependency:get \
-Dmaven.repo.local="${root}/.m2/repository" \
-DrepositoryId=nexus \
-DartifactId=${artifactId} \
-Dversion=${appvers} \
-DgroupId="org.venice.beachfront" \
-Dpackaging=tar.gz \
-DremoteRepositories="nexus::default::${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
""", returnStatus: true)
echo "dependency status = ${getDependencyStatus}"
if(getDependencyStatus == 0) {
echo "Artifact version ${appvers} exists in Nexus, nothing to do"
} else {
sh """mvn -X --settings ~/.m2/settings.xml deploy:deploy-file -Dfile=${root}/${artifactDirectory}/${tarball} \
-DrepositoryId=nexus \
-Durl="${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
-DgroupId="org.venice.beachfront" \
-DgeneratePom=false \
-Dpackaging=tar.gz \
-Dmaven.repo.local="${root}/.m2/repository" \
-DartifactId=${artifactId} \
-Dversion=${appvers}
"""
}
}
}
stage("Phase One Deploy") {
deployPhase(env.PHASE_ONE_PCF_SPACE, env.PHASE_ONE_PCF_DOMAIN, fullAppName)
}
if(!params.SKIP_INTEGRATION_TESTS) {
stage("Phase One Integration Tests") {
integrationTestPhase(env.PHASE_ONE_PCF_SPACE, env.PHASE_ONE_PCF_DOMAIN)
}
}
if(params.DEPLOY_PHASE_TWO) {
stage("Phase Two Deploy") {
deployPhase(env.PHASE_TWO_PCF_SPACE, env.PHASE_TWO_PCF_DOMAIN)
}
}
}