1+ #! /bin/bash
2+
3+ ENV=$1
4+ APPVER=$2
5+
6+ if [ -z $APPVER ] || [ -z $ENV ];
7+ then
8+ echo " The script need to be executed with version ex:deploy.sh ENV 123"
9+ exit 1
10+ fi
11+
12+ JQ=" jq --raw-output --exit-status"
13+ COUNTER_LIMIT=20
14+
15+ BUILD_VARIABLE_FILE_NAME=" ./buildvar.conf"
16+ source $BUILD_VARIABLE_FILE_NAME
17+
18+ AWS_CD_APPNAME=$( eval " echo \$ ${ENV} _AWS_CD_APPNAME" )
19+ AWS_CD_DG_NAME=$( eval " echo \$ ${ENV} _AWS_CD_DG_NAME" )
20+ AWS_CD_DG_CONFIGURATION=$( eval " echo \$ ${ENV} _AWS_CD_DG_CONFIGURATION" )
21+ AWS_S3_BUCKET=$( eval " echo \$ ${ENV} _AWS_S3_BUCKET" )
22+ AWS_S3_KEY_LOCATION=$( eval " echo \$ ${ENV} _AWS_S3_KEY_LOCATION" )
23+
24+ AWS_CD_PACKAGE_NAME=" ${APPNAME} -${PACKAGETYPE} -${APPVER} .zip"
25+ if [ " $AWS_S3_KEY_LOCATION " = " " ] ;
26+ then
27+ AWS_S3_KEY=" ${AWS_CD_PACKAGE_NAME} "
28+ else
29+ AWS_S3_KEY=" $AWS_S3_KEY_LOCATION /${AWS_CD_PACKAGE_NAME} "
30+ fi
31+ DEPLOYID=" "
32+ # log Function - Used to provide information of execution information with date and time
33+ log ()
34+ {
35+ echo " ` date +' %D %T' ` : $1 "
36+ }
37+
38+ # track_error function validates whether the application execute without any error
39+ track_error ()
40+ {
41+ if [ $1 != " 0" ]; then
42+ log " $2 exited with error code $1 "
43+ log " completed execution IN ERROR at ` date` "
44+ exit $1
45+ fi
46+
47+ }
48+
49+ # uploading to S3 bucket
50+ upload_cd_pakcage ()
51+ {
52+ S3_URL=" "
53+ if [ " $AWS_S3_KEY_LOCATION " = " " ] ;
54+ then
55+ S3_URL=" s3://${AWS_S3_BUCKET} /"
56+ else
57+ S3_URL=" s3://${AWS_S3_BUCKET} /${AWS_S3_KEY_LOCATION} /"
58+ fi
59+ aws s3 cp ${AWS_CD_PACKAGE_NAME} $S3_URL
60+ track_error $? " Package S3 deployment"
61+ log " CD Package uploaded successfully to S3 bucket $S3_URL "
62+ }
63+ # register the revision in Code deploy
64+ update_cd_app_revision ()
65+ {
66+ aws deploy register-application-revision --application-name " ${AWS_CD_APPNAME} " --s3-location " bucket=${AWS_S3_BUCKET} ,bundleType=zip,key=${AWS_S3_KEY} "
67+ track_error $? " CD applicaton register"
68+ log " CD application register completed successfully"
69+ }
70+ # Invoke the code deploy
71+ cd_deploy ()
72+ {
73+ 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} " `
74+ track_error $? " CD applicaton deployment intiation"
75+ DEPLOYID=` echo $RESULT | $JQ .deploymentId`
76+ log " CD application deployment initiation completed successfully. Please find the $DEPLOYID "
77+ }
78+ # Checing the status
79+ cd_deploy_status ()
80+ {
81+ echo " check tatusget info aws deploy get-deployment --deployment-id $DEPLOYID "
82+ counter=0
83+ BUFFER=0
84+ DEPLOYMENT_STATUS=` aws deploy get-deployment --deployment-id " $DEPLOYID " | $JQ .deploymentInfo.status`
85+ if [ " $DEPLOYMENT_STATUS " = " Succeeded" ] || [ " $DEPLOYMENT_STATUS " = " Failed" ];
86+ then
87+ BUFFER=1
88+ fi
89+ while [ " $BUFFER " = " 0" ]
90+ do
91+ echo " Current Deployment status : $DEPLOYMENT_STATUS "
92+ echo " Waiting for 15 sec to check the Deployment status...."
93+ sleep 15
94+ DEPLOYMENT_STATUS=` aws deploy get-deployment --deployment-id " $DEPLOYID " | $JQ .deploymentInfo.status`
95+ if [ " $DEPLOYMENT_STATUS " = " Succeeded" ] || [ " $DEPLOYMENT_STATUS " = " Failed" ];
96+ then
97+ BUFFER=1
98+ fi
99+ counter=` expr $counter + 1`
100+ if [[ $counter -gt $COUNTER_LIMIT ]] ; then
101+ echo " Deployment does not reach staedy state with in 600 seconds. Please check"
102+ exit 1
103+ fi
104+ done
105+ if [[ " $DEPLOYMENT_STATUS " = " Succeeded" ]] ;
106+ then
107+ echo " Deployment status is $DEPLOYMENT_STATUS "
108+ else
109+ echo " Deployment Failed. Please caheck AWS Code Deploy event logs"
110+ exit 1
111+ fi
112+
113+ }
114+ # configure_aws_cli
115+ upload_cd_pakcage
116+ update_cd_app_revision
117+ if [ " $DEPLOY " = " 1" ] ;
118+ then
119+ echo " Proceeding deployment"
120+ else
121+ echo " User skipped deployment by updating the DEPLOY variable other than 1"
122+ exit 0
123+ fi
124+ cd_deploy
125+ cd_deploy_status
0 commit comments