-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathJenkinsfile
More file actions
274 lines (240 loc) · 10 KB
/
Jenkinsfile
File metadata and controls
274 lines (240 loc) · 10 KB
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
267
268
269
270
271
272
273
274
#!groovy
// Important: What is BRANCH_NAME?
// It is branch name for builds triggered from branches.
// It is PR-<pr-id> for builds triggered from pull requests.
def tag
if (BRANCH_NAME ==~ /feature\/AMP-\d+.*/) {
def jiraId = (BRANCH_NAME =~ /feature\/AMP-(\d+).*/)[0][1]
tag = "feature-${jiraId}"
} else {
tag = BRANCH_NAME.replaceAll(/[^a-zA-Z0-9_-]/, "-").toLowerCase()
}
// Record original branch or pull request for cleanup jobs
def branch = env.CHANGE_ID == null ? BRANCH_NAME : null
def pr = env.CHANGE_ID
def registryKey = env.AMP_REGISTRY_PRIVATE_KEY
def changePretty = (pr != null) ? "pull request ${pr}" : "branch ${branch}"
println "Branch: ${branch}"
println "Pull request: ${pr}"
println "Tag: ${tag}"
def dbVersion
def pgVersion = 14
def country
def ampUrl
def dockerRepo = "798366298150.dkr.ecr.us-east-1.amazonaws.com/"
def DEPLOY_CRED_ID = 'amp-deploy-ssh'
def deployUser() { return env.jenkinsUser?.trim() ? env.jenkinsUser.trim() : 'jenkins' }
def setupKnownHosts = { host ->
sh """
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts
ssh-keyscan -H ${host} >> ~/.ssh/known_hosts
"""
}
def updateGitHubCommitStatus(context, message, state) {
repoUrl = sh(returnStdout: true, script: "git config --get remote.origin.url").trim()
lastAuthor = sh(returnStdout: true, script: "git log --pretty=%an -n 1").trim()
ref = lastAuthor.equals("Jenkins") ? "HEAD~1" : "HEAD"
commitSha = sh(returnStdout: true, script: "git rev-parse ${ref}").trim()
step([
$class: 'GitHubCommitStatusSetter',
reposSource: [$class: "ManuallyEnteredRepositorySource", url: repoUrl],
commitShaSource: [$class: "ManuallyEnteredShaSource", sha: commitSha],
contextSource: [$class: "ManuallyEnteredCommitContextSource", context: context],
statusBackrefSource: [$class: "ManuallyEnteredBackrefSource", backref: "${BUILD_URL}"],
errorHandlers: [[$class: 'ShallowAnyErrorHandler']],
statusResultSource: [
$class: "ConditionalStatusResultSource",
results: [[$class: "AnyBuildResult", message: message, state: state]]
]
])
}
def codeVersion
def countries
def environment
stage('Build') {
timeout(15) {
milestone()
environment = input(
message: "Server to deploy",
parameters: [choice(choices: ["${env.AMP_STAGING_HOSTNAME}", "${env.AMP_DE_HOSTNAME}"], name: 'environment')]
)
milestone()
}
println "Using environment: ${environment}"
node('ansible') {
checkout scm
// Find AMP version
codeVersion = readMavenPom(file: 'amp/pom.xml').version
println "AMP Version: ${codeVersion}"
sh """
mkdir -p ~/.ssh
chmod 700 ~/.ssh
touch ~/.ssh/known_hosts
chmod 600 ~/.ssh/known_hosts
ssh-keyscan -H ${environment} >> ~/.ssh/known_hosts
"""
sshagent(credentials: [DEPLOY_CRED_ID]) {
setupKnownHosts(environment)
countries = sh(returnStdout: true,
script: "ssh ${deployUser()}@${environment} 'cd /opt/amp_dbs && amp-db ls ${codeVersion} | sort'"
).trim()
}
if (countries == "") {
println "There are no database backups compatible with ${codeVersion}"
currentBuild.result = 'FAILURE'
}
}
timeout(15) {
milestone()
country = input(
message: "Proceed with build and deploy?",
parameters: [choice(choices: countries, name: 'country')]
)
milestone()
}
println "Let set amp url based on ${environment}"
if ("${environment}".toLowerCase().contains("ampdevde")) {
ampUrl = "http://amp-${country}-${tag}.de.ampsite.net/"
} else {
ampUrl = "http://amp-${country}-${tag}.stg.ampsite.net/"
}
println "amp url is ${ampUrl}"
node('docker') {
checkout scm
def image = "${dockerRepo}amp/webapp:${tag}"
def hash = sh(returnStdout: true, script: "git log --pretty=%H -n 1").trim()
docker.withRegistry("https://798366298150.dkr.ecr.us-east-1.amazonaws.com", "ecr:us-east-1:aws-ecr-credentials-id") {
try {
updateGitHubCommitStatus('jenkins/build', 'Build in progress', 'PENDING')
sshagent(credentials: ['GitHubDgReadOnlyKey']) {
withEnv(['DOCKER_BUILDKIT=1']) {
sh "ssh-add -L"
sh "docker build " +
"--progress=plain " +
"--ssh default " +
"-t ${image} " +
"--build-arg BUILD_SOURCE='${tag}' " +
"--build-arg AMP_URL='${ampUrl}' " +
"--build-arg AMP_PULL_REQUEST='${pr}' " +
"--build-arg AMP_BRANCH='${branch}' " +
"--build-arg AMP_REGISTRY_PRIVATE_KEY='${registryKey}' " +
"--label git-hash='${hash}' " +
"amp"
}
}
sh "docker push ${image} > /dev/null"
updateGitHubCommitStatus('jenkins/build', 'Built successfully', 'SUCCESS')
} catch (e) {
updateGitHubCommitStatus('jenkins/build', 'Build failed', 'ERROR')
throw e
} finally {
// Cleanup after Docker & Maven
sh returnStatus: true, script: "docker rmi ${image}"
}
}
}
}
def deployed = false
// If this stage fails then next stage will retry deployment. Otherwise next stage will be skipped.
stage('Deploy') {
node('ansible') {
try {
sshagent(credentials: [DEPLOY_CRED_ID]) {
dbVersion = sh(
returnStdout: true,
script: "ssh ${env.jenkinsUser}@${environment} 'cd /opt/amp_dbs && amp-db find ${codeVersion} ${country}'"
).trim()
withEnv([
"DEPLOY_HOST=${environment}",
"DEPLOY_USER=${env.jenkinsUser ?: 'jenkins'}",
"DEPLOY_TAG=${tag}",
"DEPLOY_COUNTRY=${country}",
"DEPLOY_DBVER=${dbVersion}",
"DEPLOY_PGVER=${pgVersion}"
]) {
sh '''#!/usr/bin/env bash
set -eox pipefail
mkdir -p ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/known_hosts && chmod 600 ~/.ssh/known_hosts
ssh-keygen -R "${DEPLOY_HOST}" 2>/dev/null || true
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
ssh -o StrictHostKeyChecking=yes \
"${DEPLOY_USER}@${DEPLOY_HOST}" \
"amp-up2 ${DEPLOY_TAG} ${DEPLOY_COUNTRY} ${DEPLOY_DBVER} ${DEPLOY_PGVER}"
'''
}
slackSend(
channel: 'amp-ci',
color: 'good',
message: "Deploy AMP - Success\nDeployed ${changePretty} will be ready for testing at ${ampUrl} in about 3 minutes"
)
deployed = true
}
}catch (e) {
slackSend(
channel: 'amp-ci',
color: 'warning',
message: "Deploy AMP - Failed\nFailed to deploy ${changePretty}"
)
currentBuild.result = 'UNSTABLE'
}
}
}
// Retry deploy with the same country.
stage('Deploy again') {
if (deployed) {
println 'Already deployed, skipping this step.'
} else {
timeout(time: 1, unit: 'HOURS') {
milestone()
input message: "Proceed with repeated deploy for ${country}?"
milestone()
}
node {
try {
withEnv([
"DEPLOY_HOST=${environment}",
"DEPLOY_USER=${env.jenkinsUser ?: 'jenkins'}",
"DEPLOY_TAG=${tag}",
"DEPLOY_COUNTRY=${country}",
"DEPLOY_DBVER=${dbVersion}",
"DEPLOY_PGVER=${pgVersion}"
]) {
sshagent(credentials: [DEPLOY_CRED_ID]) {
// Deploy AMP
sh '''#!/usr/bin/env bash
# Be strict but allow empty vars (no `-u`)
set -eox pipefail
# Ensure ~/.ssh/known_hosts is sane
mkdir -p ~/.ssh && chmod 700 ~/.ssh
touch ~/.ssh/known_hosts && chmod 600 ~/.ssh/known_hosts
# Clear any stale host key and re-pin the current one
ssh-keygen -R "${DEPLOY_HOST}" 2>/dev/null || true
ssh-keyscan -H "${DEPLOY_HOST}" >> ~/.ssh/known_hosts 2>/dev/null
# Run the remote command
ssh -o StrictHostKeyChecking=yes \
"${DEPLOY_USER}@${DEPLOY_HOST}" \
"amp-up2 ${DEPLOY_TAG} ${DEPLOY_COUNTRY} ${DEPLOY_DBVER} ${DEPLOY_PGVER}"
'''
slackSend(
channel: 'amp-ci',
color: 'good',
message: "Deploy AMP - Success\nDeployed ${changePretty} will be ready for testing at ${ampUrl} in about 3 minutes"
)
currentBuild.result = 'SUCCESS'
}
}
} catch (e) {
slackSend(
channel: 'amp-ci',
color: 'warning',
message: "Deploy AMP - Failed\nFailed to deploy ${changePretty}"
)
throw e
}
}
}
}