-
Notifications
You must be signed in to change notification settings - Fork 355
/
Copy pathstable
297 lines (255 loc) · 12.5 KB
/
stable
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#!/usr/bin/env groovy
/*
# This pipeline can be tested by executing the following steps
* Create a branch with a non-existing version name such as "patch/4.1.2.3"
* Update all the pom files with the same version (4.1.2.3)
* Commit the pom files
* Tag the commit from above with "4.1.2.3"
* Push
* Update the "Filter by name (with regular expression)" field under "Branch Sources" (here)[https://ci.dhis2.org/view/dhis2-core/job/dhis2-core-stable/configure] to include your version.
An example of such regular expression could be: ^2[.]\d+[.]\d+$|^2[.]\d+[.]\d+-(?i)embargoed$|^patch\/2[.]\d+[.]\d+$|^patch\/(2|4)[.]\d+[.]\d+[.]\d+$|^2[.]\d+[.]\d+[.]\d+$
* Schedule a build for your branch here... https://ci.dhis2.org/view/dhis2-core/job/dhis2-core-stable/
# Clean up
* Restore the regular expression previously updated to include your branch
* Delete the war file from S3: aws s3 rm s3://releases.dhis2.org/...
* Delete the docker image from the repository
*/
@Library('pipeline-library') _
pipeline {
agent {
label 'ec2-jdk17'
}
options {
timeout(time: 60)
}
environment {
MAVEN_OPTS = '-Xms1024m -Xmx4096m -Dmaven.wagon.http.retryHandler.class=standard -Dmaven.wagon.http.retryHandler.count=3 -Dmaven.wagon.httpconnectionManager.ttlSeconds=125'
GITHUB_TOKEN = credentials('github-token')
DHIS2_VERSION = readMavenPom(file: 'dhis-2/pom.xml').getVersion()
DOCKER_IMAGE_NAME_DEV = "${DOCKER_HUB_OWNER}/core-dev"
DOCKER_IMAGE_NAME = "${DOCKER_HUB_OWNER}/core"
DOCKER_IMAGE_TAG = "${env.DHIS2_VERSION.replace('SNAPSHOT', 'rc')}"
DOCKER_IMAGE_NAME_PUBLISH_SOURCE = "tomcat:9.0-jre17" // source of image to publish to Dockerhub (one of the matrix axes)
DOCKER_IMAGE_NAME_PUBLISH_TARGET = "${DOCKER_IMAGE_NAME}:${DOCKER_IMAGE_TAG}" // used to publish to Dockerhub
}
stages {
stage('Patch') {
steps {
echo 'Patching DHIS2 ...'
sh './run-cve-patcher.sh'
}
}
stage('Build') {
steps {
echo 'Building DHIS2 ...'
script {
withMaven(options: [artifactsPublisher(disabled: true)]) {
sh 'mvn --threads 4 --batch-mode --no-transfer-progress clean install --file dhis-2/pom.xml --activate-profiles -default --update-snapshots'
}
}
}
}
stage('Run api tests') {
environment {
RP_UUID = credentials('report-portal-access-uuid')
RP_ENABLE = 'true'
RP_ATTRIBUTES = "version:${env.GIT_BRANCH};"
DOCKER_IMAGE_NAME_BASE = 'tomcat'
}
matrix {
axes {
axis {
name 'DOCKER_IMAGE_TAG_BASE'
values '9.0-jre17', '8.5-jre17'
}
}
environment {
DOCKER_IMAGE_NAME_FULL = "${DOCKER_IMAGE_NAME_DEV}:${DOCKER_IMAGE_TAG}-${DOCKER_IMAGE_TAG_BASE}" // used to test against different Tomcat variants
BASE_IMAGE = "${DOCKER_IMAGE_NAME_BASE}:${DOCKER_IMAGE_TAG_BASE}"
IMAGE_REPOSITORY = "$DOCKER_IMAGE_NAME_DEV"
UNARCHIVED_WAR_DIR = "dhis2-war-$DOCKER_IMAGE_TAG_BASE" // this needs to be different for the two matrix stages, as they run in parallel
}
stages {
stage('Build Docker image') {
steps {
withDockerRegistry([credentialsId: "docker-hub-credentials", url: ""]) {
withMaven(options: [artifactsPublisher(disabled: true)]) {
sh './dhis-2/build-docker-image.sh -t "${DOCKER_IMAGE_TAG}-${DOCKER_IMAGE_TAG_BASE}" -d'
}
}
}
}
stage('Run tests') {
steps {
script {
dir("dhis-2/dhis-test-e2e") {
sh "docker pull ${DOCKER_IMAGE_NAME_FULL}"
sh "DHIS2_IMAGE=${DOCKER_IMAGE_NAME_FULL} docker-compose --project-name ${DOCKER_IMAGE_TAG_BASE} --file docker-compose.yml --file docker-compose.e2e.yml up --remove-orphans --exit-code-from test"
}
}
}
post {
always {
script {
dir("dhis-2/dhis-test-e2e") {
archiveArtifacts artifacts: "coverage.csv", allowEmptyArchive: true
}
}
}
failure {
script {
dir("dhis-2/dhis-test-e2e") {
sh "docker-compose --project-name ${DOCKER_IMAGE_TAG_BASE} logs web > ${DOCKER_IMAGE_TAG_BASE}_logs.txt"
archiveArtifacts artifacts: "${DOCKER_IMAGE_TAG_BASE}_logs.txt"
}
}
}
}
}
}
}
}
stage('Publish images') {
environment {
BASE_IMAGE = "$DOCKER_IMAGE_NAME_PUBLISH_SOURCE"
IMAGE_REPOSITORY = "$DOCKER_IMAGE_NAME"
}
steps {
script {
// Remove -rc suffix in case it's there, will be added later if needed.
oldImageTag = env.DOCKER_IMAGE_TAG.replace("-rc", "")
// If version contains more than 2 dots... It's a hotfix.
boolean isHotfix = oldImageTag.length() - oldImageTag.replace(".", "").length() > 2
if (!isHotfix) {
oldImageTag = "$oldImageTag.0"
}
imageTag = oldImageTag.replaceFirst(/^2\./, "")
withDockerRegistry([credentialsId: "docker-hub-credentials", url: ""]) {
withMaven(options: [artifactsPublisher(disabled: true)]) {
if (env.DOCKER_IMAGE_TAG.endsWith("-rc")) {
imageTag = "$imageTag-rc"
// Release candidates don't need immutable and rolling tags.
sh "./dhis-2/build-docker-image.sh -t $imageTag -d"
} else {
sh "./dhis-2/build-docker-image.sh -t $imageTag"
}
}
}
}
}
}
stage('Sync WAR') {
steps {
echo 'Syncing WAR ...'
sh 'curl "https://raw.githubusercontent.com/dhis2/dhis2-server-setup/master/ci/scripts/copy-war-s3.sh" -O'
sh 'chmod +x copy-war-s3.sh'
sh './copy-war-s3.sh stable ${GIT_BRANCH}'
}
}
stage('Update stable.json') {
when {
buildingTag()
}
environment {
DHIS2_RELEASES_REPO = "dhis2/dhis2-releases"
GITHUB_EMAIL = '[email protected]'
GITHUB_USER = 'dhis2-bot'
}
steps {
script {
dir('dhis2-releases') {
git url: "https://github.com/$DHIS2_RELEASES_REPO"
majorVersion = imageTag.split("\\.")[0]
sh """
./tools/update-stable-json/update_stable_json.py \
--version $imageTag \
--war-path ${WORKSPACE}/dhis-2/dhis-web-server/target/dhis.war \
--war-url https://releases.dhis2.org/${majorVersion}/dhis2-stable-${imageTag}.war \
--release-date \$(date +%F) \
--json-file ./downloads/v1/versions/stable.json
"""
sh 'git config user.email "$GITHUB_EMAIL"'
sh 'git config user.name "$GITHUB_USER"'
releasesBranch = "add-release-${imageTag}"
sh "git checkout -b $releasesBranch"
sh 'git add ./downloads/v1/versions/stable.json'
sh "git diff-index --quiet HEAD || git commit -m \"chore: add version $imageTag to stable.json\""
sh "git push https://${GITHUB_TOKEN}@github.com/$DHIS2_RELEASES_REPO"
sh "gh pr create --head $releasesBranch --fill-first --reviewer Philip-Larsen-Donnelly,dhis2/devops"
}
}
}
}
stage('Update IM Play instance') {
when {
expression { !env.DOCKER_IMAGE_TAG.endsWith("rc") }
}
environment {
HTTP = "http --check-status"
IM_REPO_URL = "https://github.com/dhis2-sre/im-manager"
IM_HOST = "https://api.im.dhis2.org"
INSTANCE_URL = "https://play.im.dhis2.org"
IMAGE_REPOSITORY = "core"
IMAGE_PULL_POLICY = "Always"
FLYWAY_MIGRATE_OUT_OF_ORDER = "true"
FLYWAY_REPAIR_BEFORE_MIGRATION = "true"
INSTANCE_TTL = "315360000"
STARTUP_PROBE_FAILURE_THRESHOLD = "50"
LIVENESS_PROBE_TIMEOUT_SECONDS = "5"
READINESS_PROBE_TIMEOUT_SECONDS = "5"
CORE_RESOURCES_REQUESTS_MEMORY = "2500Mi"
}
steps {
echo 'Creating DHIS2 instance on IM...'
script {
withCredentials([usernamePassword(credentialsId: 'dhis2-im-bot', passwordVariable: 'PASSWORD', usernameVariable: 'USER_EMAIL')]) {
dir('im-manager') {
gitHelper.sparseCheckout(IM_REPO_URL, "${gitHelper.getLatestTag(IM_REPO_URL)}", '/scripts')
def version = env.GIT_BRANCH
if (version.startsWith("patch/")) {
version = version.split("/")[1]
}
env.IMAGE_TAG = version
def instanceName = "stable-${version.replace(".", "-")}"
if (isHotfix) {
def endIndex = version.lastIndexOf(".")
version = version.substring(0, endIndex)
}
dir('scripts/databases') {
env.DATABASE_ID = sh(
returnStdout: true,
script: "./list.sh | jq -r '.[] | select(.name == \"test-dbs\") | .databases[] | select(.name == \"sierra-leone/${version}.sql.gz\") | .slug'"
).trim()
}
sh '[ -n "$DATABASE_ID" ]'
echo "Database: ${env.DATABASE_ID}"
dir('scripts/instances') {
description = "DHIS 2 stable branch ${env.GIT_BRANCH}"
sh "./findByName.sh play ${instanceName} | jq --exit-status 'has(\"id\")' && ./restart.sh \$(./findByName.sh play ${instanceName} | jq '.instances[] | select(.stackName==\"dhis2-core\") | .id') || ./deploy-dhis2.sh play ${instanceName} ${description}"
timeout(5) {
waitFor.statusOk("${env.INSTANCE_URL}/${instanceName}")
}
}
}
}
}
}
}
}
post {
failure {
script {
slack.sendMessage(
'#ff0000',
slack.buildUrl() + "\nLatest run on ${GIT_BRANCH} failed and needs investigation. :detective-duck:\nCommit: <${GIT_URL}/commit/${GIT_COMMIT}|${GIT_COMMIT}>",
'team-backend'
)
}
}
aborted {
script {
slack.sendTimedoutMessage()
}
}
}
}