-
Notifications
You must be signed in to change notification settings - Fork 13
/
deploy.sh
executable file
·125 lines (115 loc) · 3.82 KB
/
deploy.sh
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
#!/bin/bash
ENV=$1
APPVER=$2
if [ -z $APPVER ] || [ -z $ENV ];
then
echo "The script need to be executed with version ex:deploy.sh ENV 123"
exit 1
fi
JQ="jq --raw-output --exit-status"
COUNTER_LIMIT=20
BUILD_VARIABLE_FILE_NAME="./buildvar.conf"
source $BUILD_VARIABLE_FILE_NAME
AWS_CD_APPNAME=$(eval "echo \$${ENV}_AWS_CD_APPNAME")
AWS_CD_DG_NAME=$(eval "echo \$${ENV}_AWS_CD_DG_NAME")
AWS_CD_DG_CONFIGURATION=$(eval "echo \$${ENV}_AWS_CD_DG_CONFIGURATION")
AWS_S3_BUCKET=$(eval "echo \$${ENV}_AWS_S3_BUCKET")
AWS_S3_KEY_LOCATION=$(eval "echo \$${ENV}_AWS_S3_KEY_LOCATION")
AWS_CD_PACKAGE_NAME="${APPNAME}-${PACKAGETYPE}-${APPVER}.zip"
if [ "$AWS_S3_KEY_LOCATION" = "" ] ;
then
AWS_S3_KEY="${AWS_CD_PACKAGE_NAME}"
else
AWS_S3_KEY="$AWS_S3_KEY_LOCATION/${AWS_CD_PACKAGE_NAME}"
fi
DEPLOYID=""
#log Function - Used to provide information of execution information with date and time
log()
{
echo "`date +'%D %T'` : $1"
}
#track_error function validates whether the application execute without any error
track_error()
{
if [ $1 != "0" ]; then
log "$2 exited with error code $1"
log "completed execution IN ERROR at `date`"
exit $1
fi
}
#uploading to S3 bucket
upload_cd_pakcage()
{
S3_URL=""
if [ "$AWS_S3_KEY_LOCATION" = "" ] ;
then
S3_URL="s3://${AWS_S3_BUCKET}/"
else
S3_URL="s3://${AWS_S3_BUCKET}/${AWS_S3_KEY_LOCATION}/"
fi
aws s3 cp ${AWS_CD_PACKAGE_NAME} $S3_URL
track_error $? "Package S3 deployment"
log "CD Package uploaded successfully to S3 bucket $S3_URL"
}
#register the revision in Code deploy
update_cd_app_revision()
{
aws deploy register-application-revision --application-name "${AWS_CD_APPNAME}" --s3-location "bucket=${AWS_S3_BUCKET},bundleType=zip,key=${AWS_S3_KEY}"
track_error $? "CD applicaton register"
log "CD application register completed successfully"
}
#Invoke the code deploy
cd_deploy()
{
RESULT=`aws deploy create-deployment --application-name "${AWS_CD_APPNAME}" --deployment-config-name "${AWS_CD_DG_CONFIGURATION}" --deployment-group-name "${AWS_CD_DG_NAME}" --s3-location "bucket=${AWS_S3_BUCKET},bundleType=zip,key=${AWS_S3_KEY}"`
track_error $? "CD applicaton deployment intiation"
DEPLOYID=`echo $RESULT | $JQ .deploymentId`
log "CD application deployment initiation completed successfully. Please find the $DEPLOYID"
}
#Checing the status
cd_deploy_status()
{
echo "check tatusget info aws deploy get-deployment --deployment-id $DEPLOYID"
counter=0
BUFFER=0
DEPLOYMENT_STATUS=`aws deploy get-deployment --deployment-id "$DEPLOYID" | $JQ .deploymentInfo.status`
if [ "$DEPLOYMENT_STATUS" = "Succeeded" ] || [ "$DEPLOYMENT_STATUS" = "Failed" ];
then
BUFFER=1
fi
while [ "$BUFFER" = "0" ]
do
echo "Current Deployment status : $DEPLOYMENT_STATUS"
echo "Waiting for 15 sec to check the Deployment status...."
sleep 15
DEPLOYMENT_STATUS=`aws deploy get-deployment --deployment-id "$DEPLOYID" | $JQ .deploymentInfo.status`
if [ "$DEPLOYMENT_STATUS" = "Succeeded" ] || [ "$DEPLOYMENT_STATUS" = "Failed" ];
then
BUFFER=1
fi
counter=`expr $counter + 1`
if [[ $counter -gt $COUNTER_LIMIT ]] ; then
echo "Deployment does not reach staedy state with in 600 seconds. Please check"
exit 1
fi
done
if [[ "$DEPLOYMENT_STATUS" = "Succeeded" ]] ;
then
echo "Deployment status is $DEPLOYMENT_STATUS"
else
echo "Deployment Failed. Please caheck AWS Code Deploy event logs"
exit 1
fi
}
# configure_aws_cli
upload_cd_pakcage
update_cd_app_revision
if [ "$DEPLOY" = "1" ] ;
then
echo "Proceeding deployment"
else
echo "User skipped deployment by updating the DEPLOY variable other than 1"
exit 0
fi
cd_deploy
cd_deploy_status