forked from fjudith/docker-limesurvey
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
181 lines (178 loc) · 8.54 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
//
// https://github.com/jenkinsci/pipeline-model-definition-plugin/wiki/Syntax-Reference
// https://jenkins.io/doc/book/pipeline/syntax/#parallel
// https://jenkins.io/doc/book/pipeline/syntax/#post
pipeline {
agent any
environment {
REPO = 'fjudith/limesurvey'
PRIVATE_REPO = "${PRIVATE_REGISTRY}/${REPO}"
DOCKER_PRIVATE = credentials('docker-private-registry')
}
stages {
stage ('Checkout') {
steps {
script {
COMMIT = "${GIT_COMMIT.substring(0,8)}"
if ("${BRANCH_NAME}" == "master"){
TAG = "latest"
NGINX = "nginx"
FPM = "fpm"
}
else {
TAG = "${BRANCH_NAME}"
NGINX = "${BRANCH_NAME}-nginx"
FPM = "${BRANCH_NAME}-fpm"
}
}
sh 'printenv'
}
}
stage ('Docker build Monolith') {
agent { label 'docker'}
steps {
sh "docker build -f ./Dockerfile -t ${REPO}:${COMMIT} ./"
}
post {
success {
echo 'Tag for private registry'
sh "docker tag ${REPO}:${COMMIT} ${PRIVATE_REPO}:${TAG}"
}
}
}
stage ('Docker build Micro-Service') {
parallel {
stage ('Limesurvey Nginx'){
agent { label 'docker'}
steps {
sh "docker build -f nginx/Dockerfile -t ${REPO}:${COMMIT}-nginx nginx/"
}
post {
success {
echo 'Tag for private registry'
sh "docker tag ${REPO}:${COMMIT}-nginx ${PRIVATE_REPO}:${NGINX}"
}
}
}
stage ('LimeSurvey PHP-FPM') {
agent { label 'docker'}
steps {
sh "docker build -f fpm/Dockerfile -t ${REPO}:${COMMIT}-fpm fpm/"
}
post {
success {
echo 'Tag for private registry'
sh "docker tag ${REPO}:${COMMIT}-fpm ${PRIVATE_REPO}:${FPM}"
}
}
}
}
}
stage ('Run'){
parallel {
stage ('Monolith'){
agent { label 'docker' }
steps {
// Create Network
sh "docker network create limesurvey-mono-${BUILD_NUMBER}"
// Start database
sh "docker run -d --name 'mysql-${BUILD_NUMBER}' -e MYSQL_ROOT_PASSWORD=limesurvey -e MYSQL_USER=limesurvey -e MYSQL_PASSWORD=limesurvey -e MYSQL_DATABASE=limesurvey --network limesurvey-mono-${BUILD_NUMBER} amd64/mysql:5.6"
sleep 15
// Start application
sh "docker run -d --name 'limesurvey-${BUILD_NUMBER}' --link mysql-mono-${BUILD_NUMBER}:mysql --network limesurvey-mono-${BUILD_NUMBER} ${REPO}:${COMMIT}"
// Get container ID
script{
DOCKER_LIME = sh(script: "docker ps -qa -f ancestor=${REPO}:${COMMIT}", returnStdout: true).trim()
}
}
}
stage ('Micro-Services'){
agent { label 'docker'}
steps {
// Create Network
sh "docker network create limesurvey-micro-${BUILD_NUMBER}"
// Start database
sh "docker run -d --name 'mariadb-${BUILD_NUMBER}' -e MYSQL_ROOT_PASSWORD=limesurvey -e MYSQL_USER=limesurvey -e MYSQL_PASSWORD=limesurvey -e MYSQL_DATABASE=limesurvey --network limesurvey-micro-${BUILD_NUMBER} amd64/mariadb:10.0"
sleep 15
// Start Memcached
sh "docker run -d --name 'memcached-${BUILD_NUMBER}' --network limesurvey-micro-${BUILD_NUMBER} memcached"
// Start application micro-services
sh "docker run -d --name 'fpm-${BUILD_NUMBER}' --link mariadb-${BUILD_NUMBER}:mariadb --link memcached-${BUILD_NUMBER}:memcached --network limesurvey-micro-${BUILD_NUMBER} -v limesurvey-micro-data:/var/www/html ${REPO}:${COMMIT}-fpm"
sh "docker run -d --name 'nginx-${BUILD_NUMBER}' --link fpm-${BUILD_NUMBER}:limesurvey --link memcached-${BUILD_NUMBER}:memcached --network limesurvey-micro-${BUILD_NUMBER} -v limesurvey-micro-data:/var/www/html ${REPO}:${COMMIT}-nginx"
// Get container IDs
script {
DOCKER_FPM = sh(script: "docker ps -qa -f ancestor=${REPO}:${COMMIT}-fpm", returnStdout: true).trim()
DOCKER_NGINX = sh(script: "docker ps -qa -f ancestor=${REPO}:${COMMIT}-nginx", returnStdout: true).trim()
}
}
}
}
}
stage ('Test'){
parallel {
stage ('Monolith'){
agent { label 'docker' }
steps {
sleep 20
sh "docker run --rm --network limesurvey-mono-${BUILD_NUMBER} blitznote/debootstrap-amd64:17.04 bash -c 'curl -iL -X GET http://${DOCKER_LIME}:80'"
}
post {
always {
echo 'Remove mono stack'
sh "docker rm -fv limesurvey-${BUILD_NUMBER}"
sh "docker rm -fv mysql-${BUILD_NUMBER}"
sleep 10
sh "docker network rm limesurvey-mono-${BUILD_NUMBER}"
}
success {
sh "docker login -u ${DOCKER_PRIVATE_USR} -p ${DOCKER_PRIVATE_PSW} ${PRIVATE_REGISTRY}"
sh "docker push ${PRIVATE_REPO}:${TAG}"
}
}
}
stage ('Micro-Services'){
agent { label 'docker'}
steps {
sleep 20
sh "docker logs nginx-${BUILD_NUMBER}"
// External
sh "docker run --rm --network limesurvey-micro-${BUILD_NUMBER} blitznote/debootstrap-amd64:17.04 bash -c 'curl -iL -X GET http://${DOCKER_NGINX}:8080'"
}
post {
always {
echo 'Remove micro-services stack'
sh "docker rm -fv nginx-${BUILD_NUMBER}"
sh "docker rm -fv fpm-${BUILD_NUMBER}"
sh "docker rm -fv memcached-${BUILD_NUMBER}"
sh "docker rm -fv mariadb-${BUILD_NUMBER}"
sleep 10
sh "docker network rm limesurvey-micro-${BUILD_NUMBER}"
}
success {
sh "docker login -u ${DOCKER_PRIVATE_USR} -p ${DOCKER_PRIVATE_PSW} ${PRIVATE_REGISTRY}"
sh "docker push ${PRIVATE_REPO}:${FPM}"
sh "docker push ${PRIVATE_REPO}:${NGINX}"
}
}
}
}
}
}
post {
always {
echo 'Run regardless of the completion status of the Pipeline run.'
}
changed {
echo 'Only run if the current Pipeline run has a different status from the previously completed Pipeline.'
}
success {
echo 'Only run if the current Pipeline has a "success" status, typically denoted in the web UI with a blue or green indication.'
}
unstable {
echo 'Only run if the current Pipeline has an "unstable" status, usually caused by test failures, code violations, etc. Typically denoted in the web UI with a yellow indication.'
}
aborted {
echo 'Only run if the current Pipeline has an "aborted" status, usually due to the Pipeline being manually aborted. Typically denoted in the web UI with a gray indication.'
}
}
}