From 92521d74c476b3df4b0c2bd62296de9686763a5e Mon Sep 17 00:00:00 2001 From: Allan Denot Date: Wed, 20 May 2020 13:58:39 +1000 Subject: [PATCH] Wait for a previous deployment to finish before continuing --- src/deploy.sh | 57 ++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 19 deletions(-) diff --git a/src/deploy.sh b/src/deploy.sh index 833cc18..987c8fe 100755 --- a/src/deploy.sh +++ b/src/deploy.sh @@ -19,6 +19,14 @@ else echo "---> INFO: Deploy timeout set to ${DEPLOY_TIMEOUT} seconds"; fi +DEPLOY_CONCURRENCY_MODE=${DEPLOY_CONCURRENCY_MODE:-fail} +if [[ "$DEPLOY_CONCURRENCY_MODE" == "wait" ]] +then + echo "---> INFO: Deploy concurrency mode set to 'wait' a previous deployment to finish before continuing" +else + echo "---> INFO: Deploy concurrency mode set to 'fail'" +fi + envsubst < task-definition.tpl.json > task-definition.json echo "---> Task Definition" cat task-definition.json @@ -33,26 +41,37 @@ echo "---> Creating deployment with CodeDeploy" set +e # disable bash exit on error -# # Update the ECS service to use the updated Task version -DEPLOYMENT_ID=$(aws deploy create-deployment \ - --application-name $CLUSTER_NAME-$APP_NAME \ - --deployment-config-name CodeDeployDefault.ECSAllAtOnce \ - --deployment-group-name $CLUSTER_NAME-$APP_NAME \ - --description Deployment \ - --revision file://app-spec.json \ - --query="deploymentId" --output text) - -# In case there is already a deployment in progress, script will fail -if [ $? -eq 255 ]; then - echo - echo - echo "===> Deployment already in progress. Please approve current deployment before performing a new deployment" - echo - echo - exit 1 -fi +DEPLOY_TIMEOUT_PERIOD=0 + +while [ "${DEPLOYMENT_ID}" == "" ] +do + if [ "$DEPLOY_TIMEOUT_PERIOD" -ge "${DEPLOY_TIMEOUT:-900}" ]; then + echo "===> Timeout reached trying to create deployment. Exiting" + exit 1 + fi + + DEPLOYMENT_ID=$(aws deploy create-deployment \ + --application-name $CLUSTER_NAME-$APP_NAME \ + --deployment-config-name CodeDeployDefault.ECSAllAtOnce \ + --deployment-group-name $CLUSTER_NAME-$APP_NAME \ + --description Deployment \ + --revision file://app-spec.json \ + --query="deploymentId" --output text) -sleep 5 # Wait for deployment to be created + if [ $? -eq 255 ] && [ "${DEPLOY_CONCURRENCY_MODE}" == "fail" ] + then + # In case there is already a deployment in progress, script will fail + echo + echo + echo "===> Deployment already in progress for this application environment. Please approve or rollback current deployment before performing a new deployment" + echo + echo + exit 1 + fi + + sleep 10 # Wait until deployment is created + DEPLOY_TIMEOUT_PERIOD=$((DEPLOY_TIMEOUT_PERIOD + 10)) +done echo "---> For more info: https://$AWS_DEFAULT_REGION.console.aws.amazon.com/codesuite/codedeploy/deployments/$DEPLOYMENT_ID"