This repository has been archived by the owner on Aug 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathJenkinsfile
executable file
·315 lines (304 loc) · 18.1 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
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
pipeline {
agent{
label 'jenkins-workers'
}
environment {
BUILD_TAG = sh label: 'Generating build tag', returnStdout: true, script: 'python3 pipeline/scripts/tag.py ${GIT_BRANCH} ${BUILD_NUMBER} ${GIT_COMMIT}'
BUILD_TAG_LOWER = sh label: 'Lowercase build tag', returnStdout: true, script: "echo -n ${BUILD_TAG} | tr '[:upper:]' '[:lower:]'"
ENVIRONMENT_ID = "build"
MHS_INBOUND_QUEUE_NAME = "${ENVIRONMENT_ID}-inbound"
}
stages {
stage('Build & test common') {
steps {
dir('common') {
buildModules('Installing common dependencies')
executeUnitTestsWithCoverage()
}
}
}
stage('Build & test MHS Common') {
steps {
dir('mhs/common') {
buildModules('Installing mhs common dependencies')
executeUnitTestsWithCoverage()
}
}
}
stage('Build MHS') {
parallel {
stage('Inbound') {
stages {
stage('Build') {
steps {
dir('mhs/inbound') {
buildModules('Installing inbound dependencies')
}
}
}
stage('Unit test') {
steps {
dir('mhs/inbound') {
executeUnitTestsWithCoverage()
}
}
}
stage('Push image') {
steps {
script {
sh label: 'Pushing inbound image', script: "packer build -color=false pipeline/packer/inbound.json"
}
}
}
}
}
stage('Outbound') {
stages {
stage('Build') {
steps {
dir('mhs/outbound') {
buildModules('Installing outbound dependencies')
}
}
}
stage('Unit test') {
steps {
dir('mhs/outbound') {
executeUnitTestsWithCoverage()
}
}
}
stage('Push image') {
steps {
script {
sh label: 'Pushing outbound image', script: "packer build -color=false pipeline/packer/outbound.json"
}
}
}
}
}
stage('Route') {
stages {
stage('Build') {
steps {
dir('mhs/spineroutelookup') {
buildModules('Installing route lookup dependencies')
}
}
}
stage('Unit test') {
steps {
dir('mhs/spineroutelookup') {
executeUnitTestsWithCoverage()
}
}
}
stage('Push image') {
steps {
script {
sh label: 'Pushing spine route lookup image', script: "packer build -color=false pipeline/packer/spineroutelookup.json"
}
}
}
}
}
}
}
stage('Test') {
// NIAD-189: Parallel component and integration tests disabled due to intermittent build failures
//parallel {
stages {
stage('Run Component Tests') {
options {
lock('local-docker-compose-environment')
}
stages {
stage('Deploy component locally') {
steps {
sh label: 'Setup component test environment', script: './integration-tests/setup_component_test_env.sh'
sh label: 'Start containers', script: '''
docker-compose -f docker-compose.yml -f docker-compose.component.override.yml down -v
docker-compose -f docker-compose.yml -f docker-compose.component.override.yml -p custom_network down -v
. ./component-test-source.sh
docker-compose -f docker-compose.yml -f docker-compose.component.override.yml build
docker-compose -f docker-compose.yml -f docker-compose.component.override.yml -p ${BUILD_TAG_LOWER} up -d'''
}
}
stage('Component Tests') {
steps {
sh label: 'Run component tests', script: '''
docker build -t local/mhs-componenttest:$BUILD_TAG -f ./component-test.Dockerfile .
docker run --rm --network "${BUILD_TAG_LOWER}_default" \
--env "MHS_ADDRESS=http://outbound" \
--env "AWS_ACCESS_KEY_ID=test" \
--env "AWS_SECRET_ACCESS_KEY=test" \
--env "MHS_DB_ENDPOINT_URL=http://dynamodb:8000" \
--env "FAKE_SPINE_ADDRESS=http://fakespine" \
--env "MHS_INBOUND_QUEUE_BROKERS=amqp://rabbitmq:5672" \
--env "MHS_INBOUND_QUEUE_NAME=inbound" \
--env "SCR_ADDRESS=http://scradaptor" \
local/mhs-componenttest:$BUILD_TAG
'''
}
}
}
post {
always {
sh label: 'Docker status', script: 'docker ps --all'
sh label: 'Dump container logs to files', script: '''
mkdir logs
docker logs ${BUILD_TAG_LOWER}_route_1 > logs/route.log
docker logs ${BUILD_TAG_LOWER}_outbound_1 > logs/outbound.log
docker logs ${BUILD_TAG_LOWER}_inbound_1 > logs/inbound.log
docker logs ${BUILD_TAG_LOWER}_fakespine_1 > logs/fakespine.log
docker logs ${BUILD_TAG_LOWER}_rabbitmq_1 > logs/rabbitmq.log
docker logs ${BUILD_TAG_LOWER}_redis_1 > logs/redis.log
docker logs ${BUILD_TAG_LOWER}_dynamodb_1 > logs/dynamodb.log
'''
archiveArtifacts artifacts: 'logs/*.log', fingerprint: true
sh label: 'Docker compose logs', script: 'docker-compose -f docker-compose.yml -f docker-compose.component.override.yml -p ${BUILD_TAG_LOWER} logs'
sh label: 'Docker compose down', script: 'docker-compose -f docker-compose.yml -f docker-compose.component.override.yml -p ${BUILD_TAG_LOWER} down -v'
}
}
}
stage('Run Integration Tests') {
options {
lock('exemplar-test-environment')
}
stages {
stage('Deploy MHS') {
steps {
dir('pipeline/terraform/mhs-environment') {
sh label: 'Initialising Terraform', script: """
terraform init \
-backend-config="bucket=${TF_STATE_BUCKET}" \
-backend-config="region=${TF_STATE_BUCKET_REGION}" \
-backend-config="key=${ENVIRONMENT_ID}-mhs.tfstate" \
-backend-config="dynamodb_table=${ENVIRONMENT_ID}-${TF_MHS_LOCK_TABLE_NAME}" \
-input=false -no-color
"""
sh label: 'Applying Terraform configuration', script: """
terraform apply -no-color -auto-approve \
-var environment_id=${ENVIRONMENT_ID} \
-var build_id=${BUILD_TAG} \
-var supplier_vpc_id=${SUPPLIER_VPC_ID} \
-var opentest_vpc_id=${OPENTEST_VPC_ID} \
-var internal_root_domain=${INTERNAL_ROOT_DOMAIN} \
-var mhs_outbound_service_minimum_instance_count=3 \
-var mhs_outbound_service_maximum_instance_count=9 \
-var mhs_inbound_service_minimum_instance_count=3 \
-var mhs_inbound_service_maximum_instance_count=9 \
-var mhs_route_service_minimum_instance_count=3 \
-var mhs_route_service_maximum_instance_count=9 \
-var task_role_arn=${TASK_ROLE} \
-var execution_role_arn=${TASK_EXECUTION_ROLE} \
-var task_scaling_role_arn=${TASK_SCALING_ROLE} \
-var ecr_address=${DOCKER_REGISTRY} \
-var mhs_outbound_validate_certificate=${MHS_OUTBOUND_VALIDATE_CERTIFICATE} \
-var mhs_log_level=DEBUG \
-var mhs_outbound_http_proxy=${MHS_OUTBOUND_HTTP_PROXY} \
-var mhs_state_table_read_capacity=5 \
-var mhs_state_table_write_capacity=5 \
-var mhs_sync_async_table_read_capacity=5 \
-var mhs_sync_async_table_write_capacity=5 \
-var mhs_spine_org_code=${SPINE_ORG_CODE} \
-var inbound_queue_brokers="${MHS_INBOUND_QUEUE_BROKERS}" \
-var inbound_queue_name="${MHS_INBOUND_QUEUE_NAME}" \
-var inbound_queue_username_arn=${INBOUND_QUEUE_USERNAME_ARN} \
-var inbound_queue_password_arn=${INBOUND_QUEUE_PASSWORD_ARN} \
-var party_key_arn=${PARTY_KEY_ARN} \
-var client_cert_arn=${CLIENT_CERT_ARN} \
-var client_key_arn=${CLIENT_KEY_ARN} \
-var ca_certs_arn=${CA_CERTS_ARN} \
-var route_ca_certs_arn=${ROUTE_CA_CERTS_ARN} \
-var outbound_alb_certificate_arn=${OUTBOUND_ALB_CERT_ARN} \
-var route_alb_certificate_arn=${ROUTE_ALB_CERT_ARN} \
-var mhs_resynchroniser_max_retries=${MHS_RESYNC_RETRIES} \
-var mhs_resynchroniser_interval=${MHS_RESYNC_INTERVAL} \
-var spineroutelookup_service_sds_url=${SPINEROUTELOOKUP_SERVICE_LDAP_URL} \
-var spineroutelookup_service_search_base=${SPINEROUTELOOKUP_SERVICE_SEARCH_BASE} \
-var spineroutelookup_service_disable_sds_tls=${SPINEROUTELOOKUP_SERVICE_DISABLE_TLS} \
-var elasticache_node_type="cache.t2.micro" \
-var mhs_forward_reliable_endpoint_url=${MHS_FORWARD_RELIABLE_ENDPOINT_URL}
"""
script {
env.MHS_ADDRESS = sh (
label: 'Obtaining outbound LB DNS name',
returnStdout: true,
script: "echo \"https://\$(terraform output outbound_lb_domain_name)\""
).trim()
env.MHS_OUTBOUND_TARGET_GROUP = sh (
label: 'Obtaining outbound LB target group ARN',
returnStdout: true,
script: "terraform output outbound_lb_target_group_arn"
).trim()
env.MHS_INBOUND_TARGET_GROUP = sh (
label: 'Obtaining inbound LB target group ARN',
returnStdout: true,
script: "terraform output inbound_lb_target_group_arn"
).trim()
env.MHS_ROUTE_TARGET_GROUP = sh (
label: 'Obtaining route LB target group ARN',
returnStdout: true,
script: "terraform output route_lb_target_group_arn"
).trim()
env.MHS_STATE_TABLE_NAME = sh (
label: 'Obtaining the table name used for the MHS state',
returnStdout: true,
script: "terraform output mhs_state_table_name"
).trim()
env.MHS_SYNC_ASYNC_TABLE_NAME = sh (
label: 'Obtaining the table name used for the MHS sync/async state',
returnStdout: true,
script: "terraform output mhs_sync_async_table_name"
).trim()
}
}
}
}
stage('Integration Tests') {
steps {
dir('integration-tests/integration_tests') {
sh label: 'Installing integration test dependencies', script: 'pipenv install --dev --deploy --ignore-pipfile'
// Wait for MHS load balancers to have healthy targets
dir('../../pipeline/scripts/check-target-group-health') {
sh script: 'pipenv install'
timeout(13) {
waitUntil {
script {
def r = sh script: 'sleep 10; AWS_DEFAULT_REGION=eu-west-2 pipenv run main ${MHS_OUTBOUND_TARGET_GROUP} ${MHS_INBOUND_TARGET_GROUP} ${MHS_ROUTE_TARGET_GROUP}', returnStatus: true
return (r == 0);
}
}
}
}
sh label: 'Running integration tests', script: 'pipenv run inttests'
}
}
}
}
}
} // parallel
}
}
post {
always {
cobertura coberturaReportFile: '**/coverage.xml'
junit '**/test-reports/*.xml'
sh 'docker-compose -f docker-compose.yml -f docker-compose.component.override.yml -p ${BUILD_TAG_LOWER} down -v'
sh 'docker volume prune --force'
// Prune Docker images for current CI build.
// Note that the * in the glob patterns doesn't match /
sh 'docker image rm -f $(docker images "*/*:*${BUILD_TAG}" -q) $(docker images "*/*/*:*${BUILD_TAG}" -q) || true'
}
}
}
void executeUnitTestsWithCoverage() {
sh label: 'Running unit tests', script: 'pipenv run unittests-cov'
sh label: 'Displaying code coverage report', script: 'pipenv run coverage-report'
sh label: 'Exporting code coverage report', script: 'pipenv run coverage-report-xml'
sh label: 'Running SonarQube analysis', script: "sonar-scanner -Dsonar.host.url=${SONAR_HOST} -Dsonar.login=${SONAR_TOKEN}"
}
void buildModules(String action) {
sh label: action, script: 'pipenv install --dev --deploy --ignore-pipfile'
}