forked from dhis2/dhis2-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile-publish-image
173 lines (146 loc) · 5.99 KB
/
Jenkinsfile-publish-image
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
pipeline {
agent {
label 'large'
}
options {
disableConcurrentBuilds()
}
environment {
DHIS2_VERSION = ''
DOCKER_HUB_CORE_REPOSITORY = "${DOCKER_HUB_OWNER}/core-dev"
DOCKER_HUB_BASE_REPOSITORY = "${DOCKER_HUB_OWNER}/base-dev"
DOCKER_IMAGE_TAG = ''
DOCKER_CORE_IMAGE_FULL_NAME = ''
DOCKER_BASE_IMAGE_FULL_NAME = ''
COMPOSE_PARAMETER = ''
TESTS_IMAGE_FULL_NAME = ''
}
stages {
stage('Prepare env for release deployment') {
when {
buildingTag()
}
steps {
script {
DOCKER_HUB_CORE_REPOSITORY = "${DOCKER_HUB_OWNER}/core"
DOCKER_HUB_BASE_REPOSITORY = "${DOCKER_HUB_OWNER}/base"
pom = readMavenPom file: 'dhis-2/pom.xml'
DHIS2_VERSION = pom.version.toLowerCase()
echo "DHIS2 version: ${DHIS2_VERSION}"
DOCKER_IMAGE_TAG = "${DHIS2_VERSION}"
}
}
}
stage('Prepare env for dev deployment') {
when {
not {
buildingTag()
}
}
steps {
script {
DOCKER_IMAGE_TAG = "${env.BRANCH_NAME}"
}
}
}
stage('Prepare Docker compose') {
steps {
script {
COMPOSE_PARAMETER = "${env.JOB_NAME}"
echo "Docker compose parameter: ${COMPOSE_PARAMETER}"
}
}
}
stage('Build image') {
steps {
script {
DOCKER_BASE_IMAGE_FULL_NAME = "${DOCKER_HUB_BASE_REPOSITORY}:${DOCKER_IMAGE_TAG}"
echo "Will tag base image as ${DOCKER_BASE_IMAGE_FULL_NAME}"
sh "docker build -t ${DOCKER_BASE_IMAGE_FULL_NAME} --build-arg IDENTIFIER=${DOCKER_IMAGE_TAG} ."
}
}
}
stage('Build containers') {
steps {
script {
DOCKER_CORE_IMAGE_FULL_NAME = "${DOCKER_HUB_CORE_REPOSITORY}:${DOCKER_IMAGE_TAG}"
echo "Will tag container images as ${DOCKER_CORE_IMAGE_FULL_NAME}"
sh "./docker/extract-artifacts.sh ${DOCKER_BASE_IMAGE_FULL_NAME}"
sh "./docker/build-containers.sh ${DOCKER_CORE_IMAGE_FULL_NAME} ${DOCKER_IMAGE_TAG}"
}
}
}
stage('Run api tests') {
matrix {
axes {
axis {
name 'DOCKER_VARIANT'
values '-tomcat-8.5-jdk8-openjdk-slim', '-tomcat-9.0.27-jdk11-openjdk-slim'
}
}
stages {
stage('Start instance') {
steps {
script {
dir("dhis-2/dhis-e2e-test") {
sh "IMAGE_NAME=${DOCKER_CORE_IMAGE_FULL_NAME}${DOCKER_VARIANT} docker-compose -p ${COMPOSE_PARAMETER}${DOCKER_VARIANT} up -d"
}
}
}
}
stage('Run tests') {
steps {
script {
TESTS_IMAGE_FULL_NAME = "core-api-tests:${DOCKER_IMAGE_TAG}${DOCKER_VARIANT}"
dir("dhis-2/dhis-e2e-test") {
sh "docker build -t ${TESTS_IMAGE_FULL_NAME} ."
sh "IMAGE_NAME=${TESTS_IMAGE_FULL_NAME} docker-compose -p ${COMPOSE_PARAMETER}${DOCKER_VARIANT} -f docker-compose.e2e.yml up --exit-code-from e2e-test"
}
}
}
post {
failure {
script {
dir("dhis-2/dhis-e2e-test") {
sh "docker-compose -p ${COMPOSE_PARAMETER}${DOCKER_VARIANT} logs web > ${DOCKER_VARIANT}logs.txt"
archiveArtifacts artifacts: "${DOCKER_VARIANT}logs.txt"
}
}
}
cleanup {
script {
dir("dhis-2/dhis-e2e-test") {
sh "IMAGE_NAME=${DOCKER_CORE_IMAGE_FULL_NAME} docker-compose -p ${COMPOSE_PARAMETER}${DOCKER_VARIANT} down -v --remove-orphans"
sh "IMAGE_NAME=${TESTS_IMAGE_FULL_NAME} docker-compose -f docker-compose.e2e.yml -p ${COMPOSE_PARAMETER}${DOCKER_VARIANT} down --rmi all -v --remove-orphans"
}
}
}
}
}
}
}
}
stage('Publish images') {
steps {
withDockerRegistry([credentialsId: "docker-hub-credentials", url: ""]) {
sh "docker push ${DOCKER_BASE_IMAGE_FULL_NAME}"
sh "./docker/publish-containers.sh ${DOCKER_CORE_IMAGE_FULL_NAME}"
}
}
}
}
post {
always {
sh "docker image rm -f ${DOCKER_BASE_IMAGE_FULL_NAME}"
sh "./docker/cleanup-images.sh ${DOCKER_CORE_IMAGE_FULL_NAME}"
sh "docker image prune --force --filter label=identifier=${DOCKER_IMAGE_TAG}"
}
failure {
slackSend(
color: '#ff0000',
message: 'Publishing of docker images for branch ' + env.BRANCH_NAME + ' failed. Visit ' + env.BUILD_URL + ' for more information',
channel: 'jenkins'
)
}
}
}