-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
172 lines (124 loc) · 5.24 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
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
#!/usr/bin/env bash
set -e
if [ $(which jq | wc -l) -eq "0" ]; then
1>&2 echo "Missing the jq command. Install jq in order to parse JSON from AWS responses cleanly."
exit 2
fi
if [ $(which aws | wc -l) -eq "0" ]; then
1>&2 echo "Missing the aws command. Install the AWS CLI."
exit 2
fi
SOURCE=$(dirname "$0")
if [ "${SOURCE}" = '.' ]; then
SOURCE=$(pwd)
fi
rm -rf ${SOURCE}/deploy
mkdir -p ${SOURCE}/deploy
echo ${SOURCE}
RANDOM_BYTES=$(php -r 'echo bin2hex(random_bytes(8));')
## CodeDeploy Application Name
APPLICATION="identity"
NOW=$(date +"%Y-%m-%d_%H:%M:%S")
echo "Date: ${NOW}"
## The credentials key / secret you'll be using (see ~/.aws/credentials)
AWS_PROFILE="udemy"
## Bucket to upload the zip file artifact to
AWS_BUCKET="codedeploy.jpeterson-udemy-example.com"
AWS_REGION="us-east-2"
## Use Beta by default unless specified as ```./deploy.sh production```, as an example.
DEPLOYMENT_GROUP=${1:-beta}
if [ -z "${AWS_PROFILE}" ]; then
1>&2 echo "missing AWS profile name"
exit 2
fi
# copy all the things but vendor and this folder into a subdir
if [ -d "tmp" ]; then
echo "removing local tmp dir (CLEANING)"
rm -rf ${SOURCE}/deploy/tmp
fi
echo "Synchronizing root-level folder excluding ... (some directories, symlinks)."
rsync -a --delete --no-links --exclude "vendor/" --exclude "deploy/" --exclude ".git/" --exclude "env.php" ${SOURCE}/ ${SOURCE}/deploy/tmp/
## copy the AWS CodeDeploy configuration in with a unique destination so multiple environments can be supported on a single ASG.
cat <<EOF > ${SOURCE}/deploy/tmp/appspec.yml
version: 0.0
os: linux
files:
- source: /
destination: /var/current-deployment-${RANDOM_BYTES}
hooks:
AfterInstall:
- location: codedeploy/${APPLICATION}/01-copy-release.sh
runas: ubuntu
timeout: 300
- location: codedeploy/${APPLICATION}/05-link-build.sh
runas: root
timeout: 180
ValidateService:
- location: codedeploy/${APPLICATION}/07-validate-service.sh
runas: root
EOF
sed -i "s/\/var\/current-deployment/\/var\/current-deployment-${RANDOM_BYTES}/g" ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/01-copy-release.sh
chmod +x ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/01-copy-release.sh
sed -i "s/\/var\/current-deployment/\/var\/current-deployment-${RANDOM_BYTES}/g" ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/05-link-build.sh
chmod +x ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/05-link-build.sh
sed -i "s/\/var\/current-deployment/\/var\/current-deployment-${RANDOM_BYTES}/g" ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/07-validate-service.sh
chmod +x ${SOURCE}/deploy/tmp/codedeploy/${APPLICATION}/07-validate-service.sh
mkdir -p ${SOURCE}/deploy/tmp/public
# PHP Application -- install composer dependencies
echo "Starting PHP dependency installation"
cd ${SOURCE}/deploy/tmp && php ${SOURCE}/composer.phar install
if [ ! -f "${SOURCE}/deploy/tmp/vendor/autoload.php" ]; then
1>&2 echo "missing vendor/autoload.php"
exit 2
fi
find ${SOURCE}/deploy/tmp -type d -exec chmod 0775 {} \;
find ${SOURCE}/deploy/tmp -type f -exec chmod 0664 {} \;
cd ${SOURCE}/deploy/tmp
# JavaScript dependencies
##npm install
# JavaScript build process
##webpack
## PHP UNIT TESTS
cp env.dist.php env.php
php composer.phar test
# this would be included in the release process
rm env.php
## API Documentation
## generate swagger.json, excluding the vendor path folder
##php vendor/bin/swagger -o swagger.json -e vendor .
## generate the swagger-UI pretty API documentation file
##pretty-swag -i swagger.json -c pretty-swag-config.json -o public/api.html
##rm -rf node_modules
aws deploy push --application-name "${APPLICATION}" --s3-location "s3://${AWS_BUCKET}/${APPLICATION}/${NOW}.zip" --source . --profile="${AWS_PROFILE}" --region="${AWS_REGION}"
if [ $(aws s3 ls s3://${AWS_BUCKET}/${APPLICATION}/${NOW}.zip --profile ${AWS_PROFILE} | wc -l) -eq "0" ]; then
1>&2 echo "The file, s3://${AWS_BUCKET}/${APPLICATION}/${NOW}.zip never made it to S3. Look above for AWS CLI errors."
exit 2
fi;
CD_CMD="aws deploy create-deployment --application-name ${APPLICATION} --s3-location bucket=${AWS_BUCKET},key=\"${APPLICATION}/${NOW}.zip\",bundleType=zip --deployment-group-name=${DEPLOYMENT_GROUP} --profile=${AWS_PROFILE} --region=${AWS_REGION}"
echo ${CD_CMD}
DEPLOYMENT_ID=$(${CD_CMD} | jq -r ".deploymentId")
if [ ${#DEPLOYMENT_ID} -eq "0" ]; then
1>&2 echo "AWS CodeDeploy Error. See above message.";
exit 2;
fi
echo "Deployment Id: ${DEPLOYMENT_ID}"
DEPLOY_CHECK_COUNT=0
while true; do
DEPLOY_CHECK_COUNT=$(expr ${DEPLOY_CHECK_COUNT} + 1)
if [ ${DEPLOY_CHECK_COUNT} -ge "50" ]; then
1>&2 echo "Been deploying for far too long. failing."
exit 2
fi
echo "sleeping for 10 seconds."
sleep 10;
DEPLOY_STATUS=$(aws deploy get-deployment --deployment-id ${DEPLOYMENT_ID} --profile ${AWS_PROFILE} --region=${AWS_REGION} --query "deploymentInfo.status" --output text || true)
echo "Current deploy status: " . ${DEPLOY_STATUS}
if [ "$(echo ${DEPLOY_STATUS} | grep -i "succeeded" | wc -l)" == "1" ]; then
echo "DEPLOY COMPLETE"
exit 0;
fi
if [ "$(echo ${DEPLOY_STATUS} | grep -i "failed" | wc -l)" == "1" ]; then
1>&2 echo "DEPLOY FAILED"
exit 2;
fi
done;