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.Promote
152 lines (140 loc) · 6.23 KB
/
JenkinsFile.Promote
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
#!/usr/bin/groovy
node {
def root = pwd()
def mvn = tool 'M3'
def appVersion
def appBaseName = "bf-docs"
def appName
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}/${env.ENVIRONMENT}-config.json ${configFile}"
deleteDir()
}
// read the current configuration
def configJson = readJSON file: "${configFile}"
for (param in configJson.credparams + configJson.jobparams) {
def paramValueString = (param.defaultvalue != null) ? param.defaultvalue.toString() : ""
env."${param.name}" = (param.type == "booleanParam") ? paramValueString.toBoolean() : paramValueString
}
appVersion = "${env.PROMOTE_VERSION}"
}
if(!fileExists('.cf')) {
sh "mkdir -p .cf"
}
withEnv(["CF_HOME=.cf"]) {
def authenticatePcf = { ->
withCredentials([[$class: 'UsernamePasswordMultiBinding', credentialsId: "${env.PCF_CREDS}", usernameVariable: "CFUSER", passwordVariable: "CFPASS"]]) {
sh """
cf api ${env.PCF_API_ENDPOINT}
cf auth ${CFUSER} ${CFPASS}
"""
}
}
stage('Pull Artifact') {
authenticatePcf()
if (appVersion == "latest") {
// Get the latest version from Phase 2
echo "No version specified. Fetching the latest version from ${env.PHASE_TWO_PCF_SPACE}"
sh "cf target -o ${env.PCF_ORG} -s ${env.PHASE_TWO_PCF_SPACE}"
appName = sh(script: "cf apps | grep '${appBaseName}' | cut -f1 -d ' ' ", returnStdout: true)
appVersion = appName.trim().replace("${appBaseName}-", "")
echo "Pulled version ${appVersion} from ${env.PHASE_TWO_PCF_SPACE}"
} else {
echo "Found hardcoded app version: `${appVersion}`"
appName = "${appBaseName}-${appVersion}"
}
appName = appName.trim()
// Get the Artifact from Nexus
def getDependencyStatus = sh(script: """mvn -X --settings ~/.m2/settings.xml dependency:get \
-Dmaven.repo.local="${root}/.m2/repository" \
-DrepositoryId=nexus \
-DartifactId=${appBaseName} \
-Dversion=${appVersion} \
-DgroupId="org.venice.beachfront" \
-Dpackaging=tar.gz \
-Ddest=${root}/${appBaseName}.tar.gz \
-DremoteRepositories="nexus::default::${env.ARTIFACT_STORAGE_DEPLOY_URL}" \
""", returnStatus: true)
echo "dependency status = ${getDependencyStatus}"
if (getDependencyStatus == 0) {
// Unzip
sh "tar -xvzf ${root}/${appBaseName}.tar.gz"
} else {
error("The artifact ${appBaseName} version ${appVersion} could not be found in Nexus.")
}
}
stage ('Deploy') {
authenticatePcf()
sh "cf target -o ${env.PCF_ORG} -s ${env.PROMOTE_SPACE}"
// Generate Piazza API Key
def piazza_api_key = ""
def piazza_url = "https://piazza.${env.PROMOTE_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)
if (keyCurl.contains('Please request a new API')) {
keyCurl = sh(script: """curl -X POST -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
"""
}
withCredentials([
[$class: 'StringBinding', credentialsId: "${env.GEOAXIS_CLIENT_ID}", variable: "GEOAXIS_CLIENT_ID"],
[$class: 'StringBinding', credentialsId: "${env.GEOAXIS_SECRET}", variable: "GEOAXIS_SECRET"],
[$class: 'StringBinding', credentialsId: "${env.JKS_PASSPHRASE}", variable: "JKS_PASSPHRASE"],
[$class: 'StringBinding', credentialsId: "${env.PZ_PASSPHRASE}", variable: "PZ_PASSPHRASE"]
]) {
// Push the app
sh "cf push ${appName} -f manifest.jenkins.yml --hostname ${appName} -d ${env.PROMOTE_DOMAIN} --no-start --no-route"
try {
sh """
cf set-env ${appName} SPACE ${env.PROMOTE_SPACE}
cf set-env ${appName} DOMAIN ${env.PROMOTE_DOMAIN}
cf set-env ${appName} PIAZZA_API_KEY ${piazza_api_key}
cf set-env ${appName} OAUTH_AUTH_DOMAIN ${env.GEOAXIS_AUTH_DOMAIN}
cf set-env ${appName} OAUTH_API_DOMAIN ${env.GEOAXIS_DOMAIN}
cf set-env ${appName} OAUTH_LOGOUT_DOMAIN ${env.GEOAXIS_LOGOUT_DOMAIN}
cf set-env ${appName} OAUTH_CLIENT_ID ${GEOAXIS_CLIENT_ID}
cf set-env ${appName} OAUTH_SECRET ${GEOAXIS_SECRET}
cf set-env ${appName} JKS_FILE piazza.jks
cf set-env ${appName} JKS_PASSPHRASE ${JKS_PASSPHRASE}
cf set-env ${appName} PZ_PASSPHRASE ${PZ_PASSPHRASE}
"""
if (env.USE_GEOSERVER_PKI_AUTH) {
sh "cf set-env ${appName} SPRING_PROFILES_ACTIVE cloud,pki-geoserver-auth"
} else {
sh "cf set-env ${appName} SPRING_PROFILES_ACTIVE cloud,basic-geoserver-auth"
}
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}.")
}
}
// Assign Routes
def legacyAppNames = sh(script: "cf routes | grep \"${appBaseName}\" | awk '{print \$4}'", returnStdout: true)
sh "cf map-route ${appName} ${env.PROMOTE_DOMAIN} --hostname ${appBaseName}"
// Delete old Routes
for (Object legacyApp : legacyAppNames.trim().tokenize(',')) {
def legacyAppName = legacyApp.toString().trim()
if (legacyAppName != appName) {
sh "cf unmap-route ${legacyAppName} ${env.PROMOTE_DOMAIN} --hostname ${appBaseName}"
sh "cf delete -f ${legacyAppName} -r"
}
}
}
}
}