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
148 lines (135 loc) · 5.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
#!/usr/bin/groovy
node {
def appName = "vzutil-versioning"
def root = pwd()
def mvn = tool 'M3'
def golangTool = tool 'golang_1.7'
def gopath = "${root}/gopath"
def fullAppName = "" // Fill during setup phase
def appVersion = "" // Fill during setup phase
stage("Config") {
if(!fileExists('.cf')) {
sh "mkdir -p .cf"
}
// 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}"
}
}
def printLogsFailsafe = {String logAppName ->
try {
echo "Printing recent logs for ${logAppName}"
sh "cf logs --recent ${logAppName}"
} catch (Exception e) {
echo "Printing logs failed: ${e}"
}
}
def deployPhase = { String pcfSpace, String pcfDomain, String deployAppName ->
if(!fileExists('.cf')) {
sh "mkdir -p .cf"
}
withEnv(["CF_HOME=.cf"]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.PCF_CREDS}", usernameVariable: "CFUSER", passwordVariable: "CFPASS"]]) {
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.JENKINS_CREDS}", usernameVariable: 'JUSER', passwordVariable: 'JPASS']]) {
sh "pwd && ls"
sh "cf api ${env.PCF_API_ENDPOINT}"
sh "cf auth ${CFUSER} ${CFPASS}"
sh "cf target -o ${env.PCF_ORG} -s ${pcfSpace}"
//def jauth = (JUSER+":"+JPASS).bytes.encodeBase64().toString()
try {
sh "cf push ${deployAppName} -f manifest.jenkins.yml --hostname ${deployAppName} -d ${pcfDomain} -u none --no-start"
sh "cf set-env ${deployAppName} SPACE ${pcfSpace}"
sh "cf set-env ${deployAppName} DOMAIN ${pcfDomain}"
sh "cf set-env ${deployAppName} ARTIFACT_STORAGE_URL ${env.ARTIFACT_STORAGE_URL}"
sh "cf set-env ${deployAppName} ES_VCAP ${env.ES_VCAP}"
sh "cf set-env ${deployAppName} VZUTIL_AUTH ${env.VZUTIL_USER}"
//sh "cf set-env ${appName} JENKINS ${jauth}"
sh "cf bind-service ${deployAppName} ${env.ES_VCAP}"
sh "cf start ${deployAppName}"
} catch (Exception e) {
error(e.toString())
error("Error during application start. Deleting ${deployAppName} and failing the build.")
printLogsFailsafe(deployAppName)
sh "cf delete ${deployAppName} -f -r"
return
}
}
def legacyAppNames = sh(script: "cf routes | grep \"${appName}\" | while read -r line; do echo \$line | awk '{print \$4}'; done", returnStdout: true)
sh "cf map-route ${deployAppName} ${pcfDomain} --hostname ${appName}"
// Remove Legacy applications
for (Object legacyApp : legacyAppNames.trim().tokenize('\n,')) {
def legacyAppName = legacyApp.toString().trim()
if (legacyAppName != deployAppName) {
sh "cf unmap-route ${legacyAppName} ${pcfDomain} --hostname ${appName} || true"
sh "cf delete -f ${legacyAppName} -r || true"
}
}
}
}
}
stage("Setup") {
deleteDir()
withEnv([
"PATH+=${golangTool}/bin:${gopath}/bin",
"GOROOT=${golangTool}",
"GOPATH=${gopath}"
]) {
sh """
mkdir -p ${gopath}/bin ${gopath}/pkg ${gopath}/src/github.com/venicegeo/vzutil-versioning
go version
"""
}
dir("${gopath}/src/github.com/venicegeo/vzutil-versioning") {
git url: "${env.GIT_URL}", branch: "${env.GIT_BRANCH}"
appVersion = (sh(script: "git describe --long --tags --always | sed 's/\\./-/'g", returnStdout: true)).trim()
fullAppName = "${appName}-${appVersion}-${env.BUILD_NUMBER}"
}
}
stage("Build") {
withEnv([
"PATH+=${golangTool}/bin:${gopath}/bin",
"GOROOT=${golangTool}",
"GOPATH=${gopath}",
"GOBIN=${gopath}/bin"
]) {
sh """
go build -v github.com/venicegeo/vzutil-versioning/single
go build -v github.com/venicegeo/vzutil-versioning/web
go build -v github.com/venicegeo/vzutil-versioning/compare
cp "${gopath}/src/github.com/venicegeo/vzutil-versioning/manifest.jenkins.yml" .
cp "${gopath}/src/github.com/venicegeo/vzutil-versioning/environment.yml" .
cp "${gopath}/src/github.com/venicegeo/vzutil-versioning/Procfile" .
cp "${gopath}/src/github.com/venicegeo/vzutil-versioning/write_settings.sh" .
cp -r "${gopath}/src/github.com/venicegeo/vzutil-versioning/web/templates" .
"""
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.ARTIFACT_STORAGE_CREDS}", usernameVariable: 'NEXUS_USER', passwordVariable: 'NEXUS_PASS']]) {
sh """
./write_settings.sh
rm write_settings.sh
"""
}
sh """
zip -r9 vzutil-versioning.zip single web compare manifest.jenkins.yml environment.yml Procfile templates settings.xml
"""
}
}
stage("Deploy (Stage 1)") {
withEnv([
"PATH+=${golangTool}/bin:${gopath}/bin",
"GOROOT=${golangTool}",
"GOPATH=${gopath}",
'CF_HOME=.cf'
]) {
deployPhase(env.PHASE_ONE_PCF_SPACE, env.PHASE_ONE_PCF_DOMAIN, fullAppName)
}
}
}