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
/
JenkinsFile
233 lines (224 loc) · 9.59 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
#!/usr/bin/groovy
node {
def root = pwd()
def nodejs = tool 'NodeJS_6'
def mvn = tool 'M3'
stage("Config") {
// clone the configuration repository and copy the current configuration
def configDir = "${root}/configuration"
def configFile = "${root}/config.json"
dir(configDir) {
git url: "${env.CONFIGURATION_URL}", credentialsId: "${env.CONFIGURATION_CREDS}"
sh "mv ${configDir}/${ENVIRONMENT}-config.json ${configFile}"
deleteDir()
}
// read the current configuration
def configJson = readJSON file: "${configFile}"
for (param in configJson.credparams + configJson.jobparams) {
env."${param.name}" = (param.type == "booleanParam") ? "${param.defaultvalue}".toBoolean() : "${param.defaultvalue}"
}
}
stage("Setup") {
deleteDir()
if(env.USE_GIT_CREDS.toBoolean()) {
git url: "${env.GIT_URL}", branch: "${env.GIT_BRANCH}", credentialsId: "${env.GITLAB_CREDS}"
} else {
git url: "${env.GIT_URL}", branch: "${env.GIT_BRANCH}"
}
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.ARTIFACT_READ_ONLY_CREDS}", usernameVariable: "USER", passwordVariable: "PASS"]]) {
sh 'sed -i "s*CONDA_CHANNEL*`echo "${CONDA_CHANNEL_URL}" | sed "s#://#://${USER}:${PASS}@#g"`*g" environment.yml'
}
def branch = "${env.GIT_BRANCH}"
sh """sed -i "s/REPLACE_VERSION/${branch}/g" environment.yml"""
}
def appvers = sh(script: """git describe --long --tags --always | sed 's/\\./-/'g""", returnStdout: true).trim()
def appName = "pzsvc-ndwi-${appvers}"
stage("Archive") {
// Build Tarball
def targetFiles = "environment.yml Procfile manifest.jenkins.yml pzsvc-exec.conf"
def tarball = "pzsvc-ndwi-py.tar.gz"
sh "tar cvvzf ${tarball} ${targetFiles}"
// Check if Exists
def getDependencyStatus = sh(script: """mvn -X --settings ~/.m2/settings.xml dependency:get \
-Dmaven.repo.local="${root}/.m2/repository" \
-DrepositoryId=nexus \
-DartifactId=pzsvc-ndwi \
-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}/${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=pzsvc-ndwi \
-Dversion=${appvers}
"""
}
}
stage("Phase One Deploy") {
if(!fileExists('.cf')) {
sh "mkdir -p .cf"
}
def piazza_api_key = "empty"
def piazza_url = "https://piazza.${env.PHASE_ONE_PCF_DOMAIN}"
withCredentials([[$class: 'StringBinding', credentialsId: "${env.BEACHFRONT_PIAZZA_AUTH_TEXT}", variable: 'TOKEN']]) {
def keyCurl = sh(script: """curl -s ${piazza_url}/v2/key -u \"${TOKEN}:\"""", returnStdout: true)
piazza_api_key = sh(script: """echo \"${keyCurl}\"|grep -oE '\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}'""", returnStdout: true).trim()
sh """
if [ -z $piazza_api_key ]; then
echo "No Piazza API key found"
exit 1
fi
"""
}
withEnv([
"CF_HOME=.cf"
]) {
withCredentials([
[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.PCF_CREDS}", usernameVariable: "CFUSER", passwordVariable: "CFPASS"]
]) {
sh "cf api ${env.PCF_API_ENDPOINT}"
sh "cf auth ${CFUSER} ${CFPASS}"
sh "cf target -o ${env.PCF_ORG} -s ${env.PHASE_ONE_PCF_SPACE}"
sh "cf push ${appName} -f manifest.jenkins.yml --hostname ${appName} -d ${env.PHASE_ONE_PCF_DOMAIN} -b ${env.PYTHON_BUILDPACK_NAME} --no-start --no-route"
sh "cf set-env ${appName} SPACE ${env.PHASE_ONE_PCF_SPACE}"
sh "cf set-env ${appName} DOMAIN ${env.PHASE_ONE_PCF_DOMAIN}"
sh "cf set-env ${appName} HTTP_TIMEOUT ${env.BF_ALG_HTTP_TIMEOUT}"
sh "cf set-env ${appName} PZ_API_KEY ${piazza_api_key}"
sh "cf set-env ${appName} PZ_ADDR ${piazza_url}"
sh "cf set-env ${appName} CF_USER ${CFUSER}"
sh "cf set-env ${appName} CF_PASS ${CFPASS}"
sh "cf set-env ${appName} CF_API https://${env.PCF_API_ENDPOINT}"
sh "cf set-env ${appName} GDAL_CACHEMAX 64"
if(env.USE_GIT_CREDS.toBoolean()) {
sh "cf set-env ${appName} REQUESTS_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt"
}
try {
sh "cf start ${appName}"
} catch (Exception e) {
sh "cf logs --recent ${appName}"
sh "cf delete ${appName} -f -r"
error("Error during application start. Deleting ${appName} and failing the build.")
}
sh """
cf api ${env.PCF_API_ENDPOINT}
cf auth ${CFUSER} ${CFPASS}
cf target -o ${env.PCF_ORG} -s ${env.PHASE_ONE_PCF_SPACE}
"""
def legacyAppNames = sh(script: """cf routes | grep \"pzsvc-ndwi\" | awk '{print \$4}'""", returnStdout: true)
sh "cf map-route ${appName} apps.internal --hostname pzsvc-ndwi-${env.PHASE_ONE_PCF_SPACE}"
// Remove legacy applications
for (Object legacyApp : legacyAppNames.trim().tokenize(',')) {
def legacyAppName = legacyApp.toString().trim()
if(legacyAppName != appName) {
sh "cf unmap-route ${legacyAppName} apps.internal --hostname pzsvc-ndwi-${env.PHASE_ONE_PCF_SPACE}"
sh "cf delete -f ${legacyAppName} -r"
}
}
}
}
}
if(!env.SKIP_INTEGRATION_TESTS.toBoolean()) {
stage("Phase One Integration Tests") {
if(!fileExists("integration-tests")) {
sh "mkdir -p integration-tests"
}
withCredentials([file(credentialsId: "${POSTMAN_SECRET_FILE}", variable: "POSTMAN_FILE")]) {
withEnv([
"PATH+=${root}/integration-tests/node_modules/newman/bin:${nodejs}/bin",
"PCF_SPACE=${env.PHASE_ONE_PCF_SPACE}",
"NPM_CONFIG_CACHE=${root}/.npmcache",
"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 """
mkdir -p ${root}/.npmcache
npm install newman@3
"""
sh "sh ci/beachfront.sh"
}
}
}
}
}
if(env.DEPLOY_PHASE_TWO.toBoolean()) {
stage("Phase Two Deploy") {
def piazza_api_key = "empty"
def piazza_url = "https://piazza.${env.PHASE_TWO_PCF_DOMAIN}"
withCredentials([[$class: 'StringBinding', credentialsId: "${env.BEACHFRONT_PIAZZA_AUTH_TEXT}", variable: 'TOKEN']]) {
def keyCurl = sh(script: """curl -s ${piazza_url}/v2/key -u \"${TOKEN}:\"""", returnStdout: true)
piazza_api_key = sh(script: """echo \"${keyCurl}\"|grep -oE '\\w{8}-\\w{4}-\\w{4}-\\w{4}-\\w{12}'""", returnStdout: true).trim()
sh """
if [ -z $piazza_api_key ]; then
echo "No Piazza API key found"
exit 1
fi
"""
}
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 ${env.PHASE_TWO_PCF_SPACE}
cf push ${appName} -f manifest.jenkins.yml --hostname ${appName} -d ${env.PHASE_TWO_PCF_DOMAIN} -b ${env.PYTHON_BUILDPACK_NAME} --no-start -u none --no-route
cf set-env ${appName} SPACE ${env.PHASE_TWO_PCF_SPACE}
cf set-env ${appName} DOMAIN ${env.PHASE_TWO_PCF_DOMAIN}
cf set-env ${appName} HTTP_TIMEOUT ${env.BF_ALG_HTTP_TIMEOUT}
cf set-env ${appName} PZ_API_KEY ${piazza_api_key}
cf set-env ${appName} PZ_ADDR ${piazza_url}
cf set-env ${appName} CF_USER ${CFUSER}
cf set-env ${appName} CF_PASS ${CFPASS}
cf set-env ${appName} CF_API https://${env.PCF_API_ENDPOINT}
cf set-env ${appName} GDAL_CACHEMAX 64
"""
if(env.USE_GIT_CREDS.toBoolean()) {
sh "cf set-env ${appName} REQUESTS_CA_BUNDLE /etc/ssl/certs/ca-certificates.crt"
}
try {
sh "cf start ${appName}"
} catch (Exception e) {
try {
sh "cf logs --recent ${appName}"
} catch (Exception ex) {
echo "Printing logs failed: ${ex}"
}
sh "cf delete ${appName} -f -r"
error("Error during application start. Deleting ${appName} and failing the build.")
}
sh """
cf api ${env.PCF_API_ENDPOINT}
cf auth ${CFUSER} ${CFPASS}
cf target -o ${env.PCF_ORG} -s ${env.PHASE_TWO_PCF_SPACE}
"""
def legacyAppNames = sh(script: """cf routes | grep \"pzsvc-ndwi\" | awk '{print \$4}'""", returnStdout: true)
sh "cf map-route ${appName} apps.internal --hostname pzsvc-ndwi-${env.PHASE_TWO_PCF_SPACE}"
// Remove legacy applications
for (Object legacyApp : legacyAppNames.trim().tokenize(',')) {
def legacyAppName = legacyApp.toString().trim()
if(legacyAppName != appName) {
sh "cf unmap-route ${legacyAppName} apps.internal --hostname pzsvc-ndwi-${env.PHASE_TWO_PCF_SPACE}"
sh "cf delete -f ${legacyAppName} -r"
}
}
}
}
}
}
}