-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathJenkinsfile
224 lines (217 loc) · 8.34 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
pipeline {
agent {
kubernetes {
label 'jenkins-docker-agent'
yamlFile 'kubernetes_jenkins/jenkins-pod-template.yaml'
}
}
triggers {
pollSCM('H/2 * * * *')
}
environment {
GITHUB_REPO = 'https://github.com/atamankina/feedback-app.git'
DOCKER_CREDENTIALS_ID = 'dockerhub-token'
DOCKER_REPO = 'galaataman/feedback-app'
IMAGE_TAG = "${BUILD_NUMBER}"
DOCKER_IMAGE = "${DOCKER_REPO}:${IMAGE_TAG}"
}
stages {
stage('Checkout') {
steps {
echo 'Checking out code...'
git url: "${GITHUB_REPO}", branch: 'main'
}
}
stage('Run Unit Tests') {
steps {
echo 'Running unit tests...'
container('node') {
sh '''
npm install
npm test -- --maxWorkers=50%
'''
}
echo 'Unit tests completed successfully.'
}
}
stage('Terraform Init') {
steps {
echo 'Initializing Terraform...'
container('terraform') {
script {
dir('terraform-rds') {
sh 'rm -rf .terraform .terraform.lock.hcl'
sh 'terraform init'
}
}
}
}
}
stage('Terraform Plan') {
steps {
echo 'Planning Terraform execution...'
container('terraform') {
script {
dir('terraform-rds') {
sh 'terraform plan -out=tfplan -input=false'
}
}
}
}
}
stage('Terraform Apply') {
steps {
echo 'Applying Terraform configuration...'
container('terraform') {
script {
dir('terraform-rds') {
sh 'terraform apply -input=false tfplan'
}
}
}
}
}
stage('Retrieve RDS Endpoint') {
steps {
echo 'Retrieving the RDS endpoint...'
container('terraform') {
script {
dir('terraform-rds') {
def rdsEndpoint = sh(script: 'terraform output -raw rds_endpoint', returnStdout: true).trim()
echo "RDS Endpoint: ${rdsEndpoint}"
env.RDS_ENDPOINT = rdsEndpoint
}
}
}
echo 'Updating Kubernetes ConfigMap with RDS endpoint...'
container('kubectl') {
script {
sh """
sed -i 's|DB_HOST:.*|DB_HOST: ${env.RDS_ENDPOINT}|g' ./kubernetes/configmap.yaml
"""
sh 'cat ./kubernetes/configmap.yaml'
}
}
}
}
stage('Docker Build') {
steps {
echo 'Building the Docker image...'
container('docker') {
sh 'docker build -t $DOCKER_IMAGE .'
}
echo 'Docker build successful.'
}
}
stage('Docker Push') {
steps {
echo 'Pushing the Docker image to Docker Hub...'
container('docker') {
script {
docker.withRegistry('', "${DOCKER_CREDENTIALS_ID}") {
sh 'docker push $DOCKER_IMAGE'
}
}
}
echo 'Docker image pushed successfully.'
}
}
stage('Kubernetes Deploy App Dependencies') {
steps {
echo 'Deploying API dependencies to kubernetes cluster...'
container('kubectl') {
sh 'kubectl apply -f kubernetes/secret.yaml'
sh 'kubectl apply -f kubernetes/configmap.yaml'
sh 'kubectl apply -f kubernetes/database-volume.yaml'
sh 'kubectl apply -f kubernetes/database-deployment.yaml'
}
echo 'Deployment successful.'
}
}
stage('Kubernetes Deploy App') {
steps {
echo 'Deleting previous App deployment...'
container('kubectl') {
sh '''
kubectl delete deployment feedback-app-api || true
'''
}
echo 'Previous App deployment deleted successfully.'
echo 'Creating new App deployment...'
container('kubectl') {
script {
sh '''
sed -i "s|image: $DOCKER_REPO:latest|image: $DOCKER_IMAGE|g" kubernetes/api-deployment.yaml
'''
sh '''
kubectl apply -f kubernetes/api-deployment.yaml
kubectl rollout status deployment feedback-app-api --timeout=300s
'''
}
}
echo 'New App deployment created successfully.'
}
}
stage('Check App Status') {
steps {
echo 'Waiting for the App to become reachable...'
container('kubectl') {
script {
def retries = 30
def delay = 10
def url = "http://feedback-app-api-service:3000/feedback"
for (int i = 0; i < retries; i++) {
def result = sh(script: "curl -s -o /dev/null -w '%{http_code}' $url", returnStatus: true)
if (result == 0) {
def http_code = sh(script: "curl -s -o /dev/null -w '%{http_code}' $url", returnStdout: true).trim()
echo "App health check attempt ${i + 1}: HTTP $http_code"
if (http_code == '200') {
echo 'App is reachable!'
break
}
} else {
echo "App is not reachable yet (attempt ${i + 1}). Retrying in ${delay} seconds..."
}
if (i == retries - 1) {
error 'App is still unreachable after multiple attempts.'
}
sleep delay
}
}
}
}
}
stage('Run Integration Tests') {
steps {
echo 'Running integration tests...'
container('k6') {
sh 'k6 run --env BASE_URL=http://feedback-app-api-service:3000 --verbose ./tests/feedback-api.integration.js'
}
echo 'Integration tests completed successfully.'
}
}
}
post {
always {
echo 'Post: DockerHub URL...'
script {
def dockerHubUrl = "https://hub.docker.com/r/${DOCKER_REPO}/tags?name=${IMAGE_TAG}"
echo "DockerHub URL for the image: ${dockerHubUrl}"
writeFile file: 'dockerhub-url.txt', text: dockerHubUrl
archiveArtifacts artifacts: 'dockerhub-url.txt'
}
}
success {
echo 'Integration tests succeeded, tagging the image with "latest"...'
container('docker') {
script {
docker.withRegistry('', "${DOCKER_CREDENTIALS_ID}") {
sh "docker tag ${DOCKER_IMAGE} ${DOCKER_REPO}:latest"
sh "docker push ${DOCKER_REPO}:latest"
}
}
}
echo 'Docker image successfully pushed with "latest" tag.'
}
}
}