-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsfile.e2e
More file actions
177 lines (155 loc) · 6.69 KB
/
Jenkinsfile.e2e
File metadata and controls
177 lines (155 loc) · 6.69 KB
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
pipeline {
agent {
label 'vm-docker'
}
options {
disableConcurrentBuilds()
lock(resource: 'endpoint-insights-e2e-lock', skipIfLocked: false)
}
environment {
IMAGE_NAME = 'endpoint-insights-e2e'
IMAGE_TAG = "${GIT_COMMIT}"
CONTAINER_NAME = "endpoint-insights-e2e-app-${BUILD_TAG.replaceAll('[^a-zA-Z0-9_.-]', '_')}"
}
stages {
stage('Cleanup') {
steps {
script {
sh """
docker ps -a --filter "name=endpoint-insights-e2e-" -q | xargs -r docker rm -f || true
"""
sh """
docker images ${IMAGE_NAME} --format '{{.Tag}}' | tail -n +6 | xargs -r -I {} docker rmi ${IMAGE_NAME}:{} || true
"""
}
}
}
stage('Checkout') {
steps {
checkout scm
}
}
stage('Build') {
steps {
script {
sh """
docker build -f ./integration-tests/Dockerfile -t ${IMAGE_NAME}:${IMAGE_TAG} .
"""
}
}
}
stage('Install Test Dependencies') {
steps {
dir('integration-tests') {
sh """
npm ci
"""
}
}
}
stage('Start Application') {
steps {
script {
sh """
docker run -d \
--name ${CONTAINER_NAME} \
-p 8080:8080 \
--env-file /var/lib/jenkins/endpoint-insights-e2e.env \
${IMAGE_NAME}:${IMAGE_TAG}
"""
sh """
#!/bin/bash
set -e
TIMEOUT=90
ELAPSED=0
INTERVAL=5
echo "Waiting for container ${CONTAINER_NAME} to be healthy..."
echo "Container logs will be shown if startup fails"
while [ \$ELAPSED -lt \$TIMEOUT ]; do
if ! docker ps --filter "name=${CONTAINER_NAME}" --filter "status=running" -q | grep -q .; then
echo "ERROR: Container stopped running during startup!"
echo "Last 50 lines of container logs:"
docker logs --tail 50 ${CONTAINER_NAME}
exit 1
fi
HEALTH_STATUS=\$(docker inspect --format='{{.State.Health.Status}}' ${CONTAINER_NAME} 2>/dev/null || echo "none")
if [ "\$HEALTH_STATUS" = "healthy" ]; then
echo "Container is healthy!"
if curl -f -s http://localhost:8080/api/health > /dev/null; then
echo "Health endpoint verified!"
exit 0
else
echo "WARNING: Health status shows healthy but endpoint not responding"
fi
elif [ "\$HEALTH_STATUS" = "unhealthy" ]; then
echo "ERROR: Container health check failed!"
echo "Container logs:"
docker logs --tail 100 ${CONTAINER_NAME}
exit 1
fi
echo "Waiting for container to be healthy... (\${ELAPSED}s) [Status: \${HEALTH_STATUS}]"
sleep \$INTERVAL
ELAPSED=\$((ELAPSED + INTERVAL))
done
echo "ERROR: Container failed to become healthy within \${TIMEOUT}s"
echo "Final container logs:"
docker logs --tail 100 ${CONTAINER_NAME}
exit 1
"""
}
}
}
stage('E2E Tests') {
steps {
dir('integration-tests') {
sh """
set +e
set -a
. /var/lib/jenkins/endpoint-insights-e2e.env >/dev/null 2>&1
set +a
export APP_URL=http://localhost:8080
npx nightwatch test --env chrome
TEST_EXIT_CODE=\$?
exit \$TEST_EXIT_CODE
"""
}
}
}
}
post {
always {
script {
sh """
docker logs ${CONTAINER_NAME} > integration-tests/container.log 2>&1 || true
"""
archiveArtifacts artifacts: 'integration-tests/container.log', allowEmptyArchive: true
}
junit 'integration-tests/tests_output/*.xml'
sh """
docker stop ${CONTAINER_NAME} || true
docker rm ${CONTAINER_NAME} || true
docker rmi ${IMAGE_NAME}:${IMAGE_TAG} || true
"""
}
failure {
script {
sh """
echo "=== Debug Log Generated: \$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ) ===" > integration-tests/debug.log
echo "Jenkins Build: ${BUILD_TAG}" >> integration-tests/debug.log
echo "Build Number: ${BUILD_NUMBER}" >> integration-tests/debug.log
echo "Build URL: ${BUILD_URL}" >> integration-tests/debug.log
echo "" >> integration-tests/debug.log
echo "[\$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)] === Docker PS ===" >> integration-tests/debug.log
docker ps -a >> integration-tests/debug.log 2>&1 || true
echo "" >> integration-tests/debug.log
echo "[\$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)] === Docker Networks ===" >> integration-tests/debug.log
docker network ls >> integration-tests/debug.log 2>&1 || true
echo "" >> integration-tests/debug.log
echo "[\$(date -u +%Y-%m-%dT%H:%M:%S.%3NZ)] === App Container Logs ===" >> integration-tests/debug.log
docker logs ${CONTAINER_NAME} >> integration-tests/debug.log 2>&1 || true
"""
archiveArtifacts artifacts: 'integration-tests/debug.log', allowEmptyArchive: true
}
}
}
}