-
Notifications
You must be signed in to change notification settings - Fork 0
/
jenkins
204 lines (187 loc) · 6.92 KB
/
jenkins
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
def URL = ''
def OLD_BACKEND = ''
def NEW_BACKEND = ''
pipeline {
agent any
environment {
DOCKER_USER = credentials('DOCKER_USER')
DOCKER_PASSWORD = credentials('DOCKER_PASSWORD')
}
stages {
stage('Git checkout') {
steps {
sh(script: """
rm -r devops-capstone
git clone https://github.com/fr3d3rico/devops-capstone.git
cd ./devops-capstone/backend
""", returnStdout: true)
}
}
stage('Dockerfile hadolint v2.7.0') {
steps {
sh(script: """
cd ./devops-capstone/backend
hadolint Dockerfile
""", returnStdout: true)
}
}
stage('Npm Install Dependencies') {
steps {
sh(script: """
cd ./devops-capstone/backend
npm i
""", returnStdout: true)
}
}
stage('Npm Scan') {
steps {
sh(script: """
cd ./devops-capstone/backend
npm audit fix --audit-level=critical --force
""", returnStdout: true)
}
}
stage('Backend lint') {
steps {
sh(script: """
cd ./devops-capstone/backend
npm i -D
npm run eslint
""", returnStdout: true)
}
}
stage('Test application') {
steps {
sh(script: """
cd ./devops-capstone/backend
npm run test
""", returnStdout: true)
}
}
stage('Docker build') {
steps {
sh 'sudo docker build -t udacity-web-app ./devops-capstone/backend'
}
}
stage('Docker tag') {
steps {
sh(script: """
sudo docker tag udacity-web-app fr3d3rico/udacity-web-app:latest
""", returnStdout: true)
}
}
stage('Docker Push') {
steps {
sh(script: """
sudo docker login -u $DOCKER_USER -p $DOCKER_PASSWORD
sudo docker push fr3d3rico/udacity-web-app:latest
""", returnStdout: true)
}
}
stage('Kubernetes deploy') {
//https://serverfault.com/questions/724730/unable-to-use-aws-cli-in-jenkins-due-to-unable-to-locate-credentials-error
//https://serverfault.com/a/1059988
//https://askubuntu.com/a/6735
steps {
script {
sh 'aws eks --region us-east-1 update-kubeconfig --name devops-capstone-eks'
OLD_BACKEND = sh(script: "kubectl get deployment -o=jsonpath={.items[*].metadata.name}", returnStdout: true).trim()
echo "OLD_BACKEND=$OLD_BACKEND"
NEW_BACKEND = sh(script: "echo ${env.BUILD_ID}", returnStdout: true).trim()
echo "NEW_BACKEND=$NEW_BACKEND"
}
script {
SED_REPLACE="s/BUILD_ID/${env.BUILD_ID}/"
sh(script: """
cd ./devops-capstone
sed -i ${SED_REPLACE} deployment.yml
""", returnStdout: true)
sh(script: """
cd ./devops-capstone
sed -i ${SED_REPLACE} service.yml
""", returnStdout: true)
sh(script: """
cd ./devops-capstone
kubectl apply -f deployment.yml
kubectl apply -f service.yml
""", returnStdout: true)
}
}
}
stage('Wait service getting up(5 minutes)') {
steps {
sh 'sleep 5m'
}
}
stage('Kubernetes smoke test') {
steps {
script {
try {
URL = sh(script: """
kubectl get service --sort-by=.metadata.creationTimestamp -o=jsonpath={.items[2].status.loadBalancer.ingress[0].hostname} | head -1
""", returnStdout: true)
COMPLETE_URL="http://${URL}:3000"
sh(script: """
if curl -s ${COMPLETE_URL} | grep "Hello World!"
then
return 0
else
return 1
fi
""", returnStdout: true)
}
catch(Exception e) {
echo 'Exception occurred: ' + e.toString()
sh(script: """
kubectl delete deployment,service backend-$NEW_BACKEND
""", returnStdout: true)
throw new Exception("Error - Kubernetes smoke test")
}
}
}
}
stage('Kubernetes replace old application') {
steps {
script {
try {
URL = sh(script: """
kubectl get service --sort-by=.metadata.creationTimestamp -o=jsonpath={.items[2].status.loadBalancer.ingress[0].hostname} | head -1
""", returnStdout: true)
SED_REPLACE="s/NEW_BACKEND/${URL}/"
sh(script: """
cd ./devops-capstone
sed -i ${SED_REPLACE} apigateway.yml
""", returnStdout: true)
sh(script: """
cd ./devops-capstone
aws apigatewayv2 reimport-api --body file://apigateway.yml --api-id 595oh68rv7
""", returnStdout: true)
}
catch(Exception e) {
echo 'Exception occurred: ' + e.toString()
sh(script: """
kubectl delete deployment,service backend-$NEW_BACKEND
""", returnStdout: true)
throw new Exception("Error - Kubernetes replace old application")
}
}
}
}
stage('Kubernetes remove old application') {
steps {
script {
sh(script: """
kubectl delete deployment,service $OLD_BACKEND
""", returnStdout: true)
}
}
}
stage('Docker remove images') {
steps {
sh(script: """
sudo docker image prune
""", returnStdout: true)
}
}
}
}