-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
231 lines (216 loc) · 8.71 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
pipeline {
agent any
environment {
DOCKER_IMAGE_TAG = "jenkins-${env.BUILD_NUMBER}"
}
options {
disableConcurrentBuilds()
}
stages {
// build containers.
stage('Build') {
when {
expression {
return !params.PRODUCTION
}
}
agent {
label 'swarm-manager'
}
steps {
sh 'docker-compose build --parallel'
}
}
// build contains with tags ready for pushing to production registry.
stage('Build (PRODUCTION)') {
when {
expression {
return params.PRODUCTION
}
}
agent {
label 'swarm-manager'
}
steps {
withEnv([
"DOCKER_REGISTRY=${params.PRODUCTION_DOCKER_REGISTRY}",
"DOCKER_IMAGE_TAG=${params.PRODUCTION_GITHUB_TAG}"
]) {
sh 'docker-compose build --parallel'
}
}
}
// perform unit test, and push the containers to the test environment registry on success.
stage('Unit tests') {
when {
expression {
return !params.PRODUCTION
}
}
agent {
label 'swarm-manager'
}
steps {
echo 'Unit tests go here'
}
post {
success {
script {
if (env.DOCKER_REGISTRY) {
sh (
label: "Pushing images to repository '${env.DOCKER_REGISTRY}'",
script: 'docker-compose push'
)
}
}
}
}
}
// deploying containers on the test environment.
stage('Staging') {
when {
expression {
return !params.PRODUCTION
}
}
stages {
stage('Deploying containers') {
agent {
label 'swarm-manager'
}
steps {
sh (
label: 'Removing previous stack',
script: 'docker stack rm dr-data-stager'
)
sleep (
time: 30,
unit: 'SECONDS'
)
withCredentials ([
usernamePassword (
credentialsId: params.AUTH_CLIENT_CREDENTIAL,
usernameVariable: 'AUTH_CLIENT_ID',
passwordVariable: 'AUTH_CLIENT_SECRET'
),
]) {
sh (
label: 'Deploying new stack',
script: 'docker stack up -c docker-compose.yml -c docker-compose.swarm.yml --with-registry-auth dr-data-stager'
)
}
}
}
stage('Health check') {
agent {
label 'swarm-manager'
}
steps {
withDockerContainer(image: 'jwilder/dockerize', args: '--network dr-data-stager-net') {
sh (
label: 'Waiting for services to become available',
script: 'dockerize \
-timeout 120s \
-wait tcp://db:6379 \
-wait http://api-server:8080/v1/docs \
-wait http://ui:3080/fs/dir'
)
}
}
post {
failure {
sh (
label: 'Displaying service status',
script: 'docker stack ps dr-data-stager'
)
sh (
label: 'Displaying service logs',
script: 'docker stack services --format \'{{.Name}}\' dr-data-stager | xargs -n 1 docker service logs'
)
}
}
}
}
}
// making release tag and push containers to production registry.
stage('Tag and push (PRODUCTION)') {
when {
expression {
return params.PRODUCTION
}
}
agent {
label 'swarm-manager'
}
steps {
echo "production: true"
echo "production github tag: ${params.PRODUCTION_GITHUB_TAG}"
// Handle Github tags
withCredentials([
usernamePassword (
credentialsId: params.GITHUB_CREDENTIAL,
usernameVariable: 'GITHUB_USERNAME',
passwordVariable: 'GITHUB_PASSWORD'
)
]) {
// Remove local tag (if any)
script {
def statusCode = sh(script: "git tag --list | grep ${params.PRODUCTION_GITHUB_TAG}", returnStatus: true)
if(statusCode == 0) {
sh "git tag -d ${params.PRODUCTION_GITHUB_TAG}"
echo "Removed existing local tag ${params.PRODUCTION_GITHUB_TAG}"
}
}
// Create local tag
sh "git tag -a ${params.PRODUCTION_GITHUB_TAG} -m 'jenkins'"
echo "Created local tag ${params.PRODUCTION_GITHUB_TAG}"
// Remove remote tag (if any)
script {
def result = sh(script: "git ls-remote https://${GITHUB_USERNAME}:${GITHUB_PASSWORD}@github.com/dccn-tg/dr-data-stager.git refs/tags/${params.PRODUCTION_GITHUB_TAG}", returnStdout: true).trim()
if (result != "") {
sh "git push --delete https://${GITHUB_USERNAME}:${GITHUB_PASSWORD}@github.com/dccn-tg/dr-data-stager.git ${params.PRODUCTION_GITHUB_TAG}"
echo "Removed existing remote tag ${params.PRODUCTION_GITHUB_TAG}"
}
}
// Create remote tag
sh "git push https://${GITHUB_USERNAME}:${GITHUB_PASSWORD}@github.com/dccn-tg/dr-data-stager.git ${params.PRODUCTION_GITHUB_TAG}"
echo "Created remote tag ${params.PRODUCTION_GITHUB_TAG}"
}
// Override the env variables and
// push the Docker images to the production Docker registry
withEnv([
"DOCKER_REGISTRY=${params.PRODUCTION_DOCKER_REGISTRY}",
"DOCKER_IMAGE_TAG=${params.PRODUCTION_GITHUB_TAG}"
]) {
withCredentials([
usernamePassword (
credentialsId: params.PRODUCTION_DOCKER_REGISTRY_CREDENTIAL,
usernameVariable: 'DOCKER_USERNAME',
passwordVariable: 'DOCKER_PASSWORD'
)
]) {
sh "docker login -u ${DOCKER_USERNAME} -p ${DOCKER_PASSWORD} ${params.PRODUCTION_DOCKER_REGISTRY}"
sh 'docker-compose push api-server worker ui'
echo "Pushed images to ${DOCKER_REGISTRY}"
}
}
}
}
}
post {
success {
script {
// regenerate env.sh; but strip out the username/password
def statusCode = sh(returnStatus:true, script: "bash ./print_env.sh | sed 's/^AUTH_CLIENT_ID=.*/AUTH_CLIENT_ID=/' | sed 's/^AUTH_CLIENT_SECRET=.*/AUTH_CLEINT_SECRET=/' > env")
if ( statusCode != 0 ) {
echo "unable to generate env.sh file, check it manually."
}
}
archiveArtifacts "docker-compose.yml, docker-compose.swarm.yml, env"
}
always {
echo 'cleaning'
sh 'docker system prune -f'
}
}
}