forked from VeerMuchandi/jenkinsose3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
deploytodev
163 lines (134 loc) · 4.16 KB
/
deploytodev
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
oc login -u$USER_NAME -p$USER_PASSWD --server=$OSE_SERVER --certificate-authority=$CERT_PATH
oc project $DEVEL_PROJ_NAME
#Is this a new deployment or an existing app? Decide based on whether the project is empty or not
#If BuildConfig exists, assume that the app is already deployed and we need a rebuild
BUILD_CONFIG=`oc get bc | tail -1 | awk '{print $1}'`
if [ -z "$BUILD_CONFIG" -o $BUILD_CONFIG == "NAME" ]; then
# no app found so create a new one
echo "Create a new app"
oc new-app --template=eap6-basic-sti -p \
APPLICATION_NAME=$APP_NAME,APPLICATION_HOSTNAME=$APP_HOSTNAME,EAP_RELEASE=6.4,GIT_URI=$APP_GIT,GIT_REF=$APP_GIT_REF,GIT_CONTEXT_DIR=$APP_GIT_CONTEXT_DIR\
-l name=$APP_NAME
echo "Find build id"
BUILD_ID=`oc get builds | tail -1 | awk '{print $1}'`
rc=1
attempts=75
count=0
while [ $rc -ne 0 -a $count -lt $attempts ]; do
BUILD_ID=`oc get builds | tail -1 | awk '{print $1}'`
if [ $BUILD_ID == "NAME" ]; then
count=$(($count+1))
echo "Attempt $count/$attempts"
sleep 5
else
rc=0
echo "Build Id is :" ${BUILD_ID}
fi
done
if [ $rc -ne 0 ]; then
echo "Fail: Build could not be found after maximum attempts"
exit 1
fi
else
# Application already exists, just need to start a new build
echo "App Exists. Triggering application build and deployment"
BUILD_ID=`oc start-build ${BUILD_CONFIG}`
fi
echo "Waiting for build to start"
rc=1
attempts=25
count=0
while [ $rc -ne 0 -a $count -lt $attempts ]; do
status=`oc get build ${BUILD_ID} -t '{{.status.phase}}'`
if [[ $status == "Failed" || $status == "Error" || $status == "Canceled" ]]; then
echo "Fail: Build completed with unsuccessful status: ${status}"
exit 1
fi
if [ $status == "Complete" ]; then
echo "Build completed successfully, will test deployment next"
rc=0
fi
if [ $status == "Running" ]; then
echo "Build started"
rc=0
fi
if [ $status == "Pending" ]; then
count=$(($count+1))
echo "Attempt $count/$attempts"
sleep 5
fi
done
# stream the logs for the build that just started
oc build-logs $BUILD_ID
echo "Checking build result status"
rc=1
count=0
attempts=100
while [ $rc -ne 0 -a $count -lt $attempts ]; do
status=`oc get build ${BUILD_ID} -t '{{.status.phase}}'`
if [[ $status == "Failed" || $status == "Error" || $status == "Canceled" ]]; then
echo "Fail: Build completed with unsuccessful status: ${status}"
exit 1
fi
if [ $status == "Complete" ]; then
echo "Build completed successfully, will test deployment next"
rc=0
else
count=$(($count+1))
echo "Attempt $count/$attempts"
sleep 5
fi
done
if [ $rc -ne 0 ]; then
echo "Fail: Build did not complete in a reasonable period of time"
exit 1
fi
echo "Checking build result status"
rc=1
count=0
attempts=100
while [ $rc -ne 0 -a $count -lt $attempts ]; do
status=`oc get build ${BUILD_ID} -t '{{.status.phase}}'`
if [[ $status == "Failed" || $status == "Error" || $status == "Canceled" ]]; then
echo "Fail: Build completed with unsuccessful status: ${status}"
exit 1
fi
if [ $status == "Complete" ]; then
echo "Build completed successfully, will test deployment next"
rc=0
else
count=$(($count+1))
echo "Attempt $count/$attempts"
sleep 5
fi
done
if [ $rc -ne 0 ]; then
echo "Fail: Build did not complete in a reasonable period of time"
exit 1
fi
# scale up the test deployment
RC_ID=`oc get rc | tail -1 | awk '{print $1}'`
echo "Scaling up new deployment $test_rc_id"
oc scale --replicas=1 rc $RC_ID
echo "Checking for successful test deployment at $HOSTNAME"
set +e
rc=1
count=0
attempts=100
while [ $rc -ne 0 -a $count -lt $attempts ]; do
if curl -s --connect-timeout 2 $APP_HOSTNAME >& /dev/null; then
rc=0
break
fi
count=$(($count+1))
echo "Attempt $count/$attempts"
sleep 5
done
set -e
if [ $rc -ne 0 ]; then
echo "Failed to access test deployment, aborting roll out."
exit 1
fi
################################################################################
##Include development test scripts here and fail with exit 1 if the tests fail##
################################################################################