This repository has been archived by the owner on May 2, 2020. It is now read-only.
forked from ccnmtl/countryx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJenkinsfile
211 lines (187 loc) · 5.14 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
// Sandbox approvals that you will need (at least):
// staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods plus java.lang.Object[]
// staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getAt java.lang.Iterable int
// java.lang.Object[]
// staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods plus java.util.List java.lang.Object
// check for required parameters. assign them to the env for
// convenience and make sure that an exception is raised if any
// are missing as a side-effect
env.APP = APP
env.REPO = REPO
env.ADMIN_EMAIL = ADMIN_EMAIL
def hosts = HOSTS.split(" ")
// optional (not all apps use celery/beat)
def celery_hosts = [:]
def beat_hosts = [:]
try {
celery_hosts = CELERY_HOSTS.split(" ")
} catch (hostsErr) {
celery_hosts = []
}
try {
beat_hosts = BEAT_HOSTS.split(" ")
} catch (beatserr) {
beat_hosts = []
}
def all_hosts = hosts + celery_hosts + beat_hosts as Set
def sentry = true
try {
env.SENTRY_URL = SENTRY_URL
} catch (sentryErr) {
println "sentry not configured"
sentry = false
}
def err = null
currentBuild.result = "SUCCESS"
try {
node {
stage('Checkout') {
checkout scm
TAG = sh (script: "git log -n 1 --pretty=format:'%H'", returnStdout: true)
env.TAG = TAG
}
stage("Build") {
retry_backoff(5) { sh "docker pull ${REPO}/${APP}:latest" }
sh "make build"
}
stage("Docker Push") {
retry_backoff(5) { sh "docker push ${REPO}/${APP}:${TAG}" }
}
}
node {
def branches = [:]
for (int i = 0; i < all_hosts.size(); i++) {
branches["pull-${i}"] = create_pull_exec(i, all_hosts[i])
}
parallel branches
def host = all_hosts[0]
stage("Migrate") {
sh "ssh ${host} /usr/local/bin/docker-runner ${APP} migrate"
}
stage("Collectstatic") {
sh "ssh ${host} /usr/local/bin/docker-runner ${APP} collectstatic"
}
stage("Compress") {
sh "ssh ${host} /usr/local/bin/docker-runner ${APP} compress"
}
}
node {
def branches = [:]
for (int i = 0; i < hosts.size(); i++) {
branches["web-restart-${i}"] = create_restart_web_exec(i, hosts[i])
}
parallel branches
}
node {
def branches = [:]
for (int i = 0; i < celery_hosts.size(); i++) {
branches["celery-restart-${i}"] = create_restart_celery_exec(i, celery_hosts[i])
}
parallel branches
}
node {
def branches = [:]
for (int i = 0; i < beat_hosts.size(); i++) {
branches["beat-restart-${i}"] = create_restart_beat_exec(i, beat_hosts[i])
}
parallel branches
}
if (sentry) {
node {
stage("Sentry") {
sh '''COMMIT=$(git log -n 1 --pretty=format:'%H')
curl ${SENTRY_URL} \
-X POST \
-H "Content-Type: application/json" \
-d "{\\\"version\\\": \\\"${COMMIT}\\\"}"
'''
}
}
}
} catch (caughtError) {
err = caughtError
currentBuild.result = "FAILURE"
} finally {
(currentBuild.result != "ABORTED") && node {
step([$class: 'Mailer',
notifyEveryUnstableBuild: true,
recipients: ADMIN_EMAIL,
sendToIndividuals: true])
}
/* Must re-throw exception to propagate error */
if (err) {
throw err
}
}
// -------------------- helper functions ----------------------
def create_pull_exec(int i, String host) {
cmd = {
node {
stage("Docker Pull - "+i) {
sh """
ssh ${host} docker pull \${REPOSITORY}\$REPO/${APP}:\$TAG
ssh ${host} cp /var/www/${APP}/TAG /var/www/${APP}/REVERT || true
ssh ${host} "echo export TAG=\$TAG > /var/www/${APP}/TAG"
"""
}
}
}
return cmd
}
def create_restart_web_exec(int i, String host) {
cmd = {
node {
stage("Restart Gunicorn - "+i) {
sh """
ssh ${host} sudo systemctl stop ${APP}.service || true
ssh ${host} sudo systemctl start ${APP}.service
"""
}
}
}
return cmd
}
def create_restart_celery_exec(int i, String host) {
cmd = {
node {
stage("Restart Worker - "+i) {
sh """
ssh ${host} sudo systemctl stop ${APP}-worker.service || true
ssh ${host} sudo systemctl start ${APP}-worker.service
"""
}
}
}
return cmd
}
def create_restart_beat_exec(int i, String host) {
cmd = {
node {
stage("Restart Beat - "+i) {
sh """
ssh ${host} sudo systemctl stop ${APP}-beat.service|| true
ssh ${host} sudo systemctl start ${APP}-beat.service
"""
}
}
}
return cmd
}
// retry with exponential backoff
def retry_backoff(int max_attempts, Closure c) {
int n = 0
while (n < max_attempts) {
try {
c()
return
} catch (err) {
if ((n + 1) >= max_attempts) {
// we're done. re-raise the exception
throw err
}
sleep(2**n)
n++
}
}
return
}