Posted at

CicleCIからでCodeDeployを回すときに30分以上かかるときの対策

More than 1 year has passed since last update.

CircleCIからDodeDeployを実行するときに30分以上かかると watch_application_deployment でエラーになる。

サポートに問い合わせてもいい回答得られなかったので自分でシェル作った

#!/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