Created
November 26, 2016 13:56
-
-
Save hachi-eiji/084187610e6934a6dc57b35cb36a41ef to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
set -eu | |
usage() | |
{ | |
cat <<EOF | |
$(basename ${0}) is deploy tool on CodeDeploy | |
Usage: | |
$(basename ${0}) <parameters> | |
Parameters: | |
--application-name application name | |
--deployment-group-name deployment group | |
--bucket bucket name | |
--key file key | |
--region aws region (default: ap-northeast-1) | |
--debug debug | |
Options: | |
--help, -h print this | |
EOF | |
} | |
watch_deployment() | |
{ | |
deployment_id="$1" | |
region="$2" | |
deploy_info=`aws deploy get-deployment --region ${region} --deployment-id "$deployment_id"` | |
deploy_status=`ruby -e "require 'json'; d = JSON.parse('$deploy_info'); puts d['deploymentInfo']['status']"` | |
case "$deploy_status" in | |
Succeeded) | |
echo "status is Succeeded" | |
exit 0 | |
;; | |
Created|Queued|InProgress) | |
ruby -e "require 'json'; d = JSON.parse('$deploy_info'); puts Time.now.strftime('%Y-%m-%d %H:%M:%S') + ' ' + d['deploymentInfo']['deploymentOverview'].to_s" | |
sleep 5 | |
watch_deployment $deployment_id $region | |
;; | |
Failed) | |
echo "status is Failed" | |
exit 1 | |
;; | |
*) | |
echo "get unknown status ${deploy_status}" | |
exit 1 | |
;; | |
esac | |
} | |
APP_NAME='' | |
BUCKET='' | |
DEPLOYMENT_GROUP_NAME='' | |
KEY='' | |
REGION='ap-northeast-1' | |
DEBUG='' | |
while [ $# -gt 0 ]; | |
do | |
case ${1} in | |
--application-name) | |
APP_NAME=${2} | |
shift | |
;; | |
--bucket) | |
BUCKET="${2}" | |
shift | |
;; | |
--key) | |
KEY="${2}" | |
shift | |
;; | |
--deployment-group-name) | |
DEPLOYMENT_GROUP_NAME="${2}" | |
shift | |
;; | |
--region) | |
REGION="${2}" | |
shift | |
;; | |
--debug) | |
DEBUG='--debug' | |
;; | |
--help|-h) | |
usage | |
exit 0 | |
;; | |
*) | |
echo "[ERROR] invalid option ${1}" | |
usage | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
# デプロイ実行&ステータスを確認する | |
if [ -z "$APP_NAME" -o -z "$BUCKET" -o -z "${KEY}" -o -z "$DEPLOYMENT_GROUP_NAME" ]; then | |
echo "[ERROR] parameter invalid" | |
usage | |
exit 1 | |
fi | |
S3_LOCATION="${BUCKET}/${KEY}" | |
echo "${APP_NAME} deploy to ${S3_LOCATION}" | |
aws deploy push ${DEBUG} --ignore-hidden-files \ | |
--region ${REGION} \ | |
--application-name ${APP_NAME} \ | |
--s3-location s3://${S3_LOCATION} | |
response=`aws deploy create-deployment ${DEBUG} \ | |
--region ${REGION} \ | |
--application-name ${APP_NAME} \ | |
--deployment-group-name ${DEPLOYMENT_GROUP_NAME} \ | |
--s3-location bucket=${BUCKET},bundleType=zip,key=${KEY}` | |
deployment_id=`ruby -e "require 'json'; d = JSON.parse('$response'); puts d['deploymentId']"` | |
watch_deployment $deployment_id $REGION |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment