-
Notifications
You must be signed in to change notification settings - Fork 0
/
Jenkinsfile
193 lines (182 loc) · 6.83 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
def TEST_FORM_STATUS_CODE = 1
def TEST_API_STATUS_CODE = 1
pipeline{
agent any
environment {
DEV_EMAIL = 'sarthaksharma575@gmail.com'
ADMIN_EMAIL = 'sarthaksharma10022000@gmail.com'
}
stages{
stage("Developing"){
steps{
sh '''
echo "copy codes to workspace"
if sudo ls /root/ | grep deployAPI
then
echo "dir exists, copying code"
else
sudo mkdir /root/deployAPI
fi
sudo cp -rvf *.html *.py *.yaml /root/deployAPI/
'''
}
post{
success{
echo "codes copied to workspace"
}
failure{
echo "error in process"
}
}
}
stage("Testing"){
steps{
echo "testing codes via docker"
sh '''
if sudo docker ps -a | grep deploy-test-api
then
sudo docker rm deploy-test-api
fi
sudo docker run -dit -p 85:80 -v /root/deployAPI:/var/www/html --name test-deploy-form sarthaksharma5/webapp:latest
sudo docker run -dit -p 90:80 -v /root/deployAPI:/var/www/cgi-bin --name test-deploy-api sarthaksharma5/api-webcgi:latest
sudo docker exec test-deploy-api chmod +x /var/www/cgi-bin/cmdapi.py
'''
script{
// if SUCCESS then TEST_FORM_STATUS_CODE = 0 | i.e., sh returns 0 iff curl returns 200
TEST_FORM_STATUS_CODE = sh returnStatus: true, script: 'curl -o /dev/null -s -w "%{http_code}" -i 0.0.0.0:85'
TEST_API_STATUS_CODE = sh returnStatus: true, script: 'curl -o /dev/null -s -w "%{http_code}" -i 0.0.0.0:90/cgi-bin/cmdapi.py?cmd=date'
}
sh '''
sudo docker stop test-deploy-api
sudo docker stop test-deploy-form
sudo docker rm test-deploy-form
sudo docker rm test-deploy-api
'''
}
post{
always{
echo "Testing complete"
}
}
}
stage ('Notify Devs of FAILURE') {
when{
expression {
TEST_FORM_STATUS_CODE != 0 || TEST_API_STATUS_CODE != 0; // i.e., STATUS_CODE != 200 : Q/A Failed
}
}
steps {
echo "mailing developer: Issue with Code, STATUS_CODE != 200"
sh 'exit 1' // force FAILURE: force exit Pipeline execution
}
post{
failure{
emailext body: '''
Check console output to view the results.\n\n
${CHANGES}\n
--------------------------------------------------\n
${BUILD_LOG, maxLines=100, escapeHtml=false}
''',
to: "${DEV_EMAIL}",
subject: 'Build failed in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
}
}
}
stage ('Notify Devs of SUCCESS') {
when{
expression {
TEST_FORM_STATUS_CODE == 0 && TEST_API_STATUS_CODE ==0;
}
}
steps {
echo "mailing developer: STATUS_CODE = 200"
sh 'exit 0'
}
post{
success{
emailext body: '''
Initiating deployment ... \n\n
Check console output to view the results.\n\n
${CHANGES}\n
--------------------------------------------------\n
${BUILD_LOG, maxLines=100, escapeHtml=false}
''',
to: "${DEV_EMAIL}",
subject: 'Build SUCCESS in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
}
}
}
stage('Deploying'){
steps{
echo 'Deploying over Cluster'
sh '''
var=$(sudo kubectl get deploy cmd-api --ignore-not-found)
if [[ "$var" == "" ]]
then
sudo kubectl create -f /root/deployAPI/forms.yaml
sudo kubectl create -f /root/deployAPI/api.yaml
else
sudo kubectl apply -f /root/deployAPI/form.yaml
sudo kubectl apply -f /root/deployAPI/api.yaml
fi
sleep 30
pod_api=$(sudo kubectl get pods -l app=cmd-app,tier=backend -o jsonpath={.items[0].metadata.name})
pod_form=$(sudo kubectl get pods -l app=cmd-app,tier=frontend -o jsonpath={.items[0].metadata.name})
sudo kubectl cp /root/deployAPI/index.html $pod_form:/var/www/html
sudo kubectl cp /root/deployAPI/cmdapi.py $pod_api:/var/www/cgi-bin
sudo kubectl exec $pod_api -- chmod +x /var/www/cgi-bin/cmdapi.py
'''
}
post{
success{
echo 'Deployment success'
}
failure{
echo 'Deployment failed, mailing Admin ...'
emailext body: '''
Deployment failed: ISSUE with Kubernetes Cluster
Last update by: ${DEV_EMAIL}
Check console output to view the results.\n\n
${CHANGES}\n
--------------------------------------------------\n
${BUILD_LOG, maxLines=100, escapeHtml=false}
\n\nPipeline failure ''',
to: "${ADMIN_EMAIL}",
subject: 'Build FAILED in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
}
}
}
}
post{
success{
echo "Pipeline executed successfully"
echo "-- mailing Admin --"
emailext body: '''
App updated successfully
Last update by: ${DEV_EMAIL}
Check console output to view the results.\n\n
${CHANGES}\n
--------------------------------------------------\n
${BUILD_LOG, maxLines=100, escapeHtml=false}
\n\nPipeline executed successfully ''',
to: "${ADMIN_EMAIL}",
subject: 'Build SUCCESS in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
}
failure{
echo "Pipeline failed"
emailext body: '''
Last update by: ${DEV_EMAIL}
Check console output to view the results.\n\n
${CHANGES}\n
--------------------------------------------------\n
${BUILD_LOG, maxLines=100, escapeHtml=false}
\n\nPipeline executed successfully ''',
to: "${ADMIN_EMAIL}",
subject: 'Build FAILED in Jenkins: $PROJECT_NAME - #$BUILD_NUMBER'
}
}
}
/*
def TEST_FORM_STATUS_CODE = 1
def TEST_API_STATUS_CODE = 1
*/