Skip to content

Commit

Permalink
Support blue/green deployments (based on silinternational#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
danpaz committed Apr 1, 2017
1 parent 15358f5 commit d27197c
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions ecs-deploy
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,13 @@ function registerNewTaskDefinition() {
NEW_TASKDEF=`$AWS_ECS register-task-definition --cli-input-json "$NEW_DEF" | jq -r .taskDefinition.taskDefinitionArn`
}

function rollback() {
echo "Rolling back to ${TASK_DEFINITION_ARN}"
$AWS_ECS update-service --cluster $CLUSTER --service $SERVICE --task-definition $TASK_DEFINITION_ARN > /dev/null
}

function updateService() {
UPDATE_SERVICE_SUCCESS="false"
DEPLOYMENT_CONFIG=""
if [ $MAX != false ]; then
DEPLOYMENT_CONFIG=",maximumPercent=$MAX"
Expand Down Expand Up @@ -319,23 +325,54 @@ function updateService() {
fi

fi

exit 0
UPDATE_SERVICE_SUCCESS="true"
break
fi
fi

sleep $every
i=$(( $i + $every ))
done

# Timeout
echo "ERROR: New task definition not running within $TIMEOUT seconds"
exit 1
if [[ "${UPDATE_SERVICE_SUCCESS}" != "true" ]]; then
# Timeout
echo "ERROR: New task definition not running within $TIMEOUT seconds"
rollback
exit 1
fi
else
echo "Skipping check for running task definition, as desired-count <= 0"
fi
}

function waitForGreenDeployment {
DEPLOYMENT_SUCCESS="false"
every=2
i=0
echo "Waiting for service deployment to complete..."
while [ $i -lt $TIMEOUT ]
do
NUM_DEPLOYMENTS=$($AWS_ECS describe-services --services $SERVICE --cluster $CLUSTER | jq "[.services[].deployments[]] | length")

# Wait to see if more than 1 deployment stays running
# If the wait time has passed, we need to roll back
if [ $NUM_DEPLOYMENTS -eq 1 ]; then
echo "Service deployment successful."
DEPLOYMENT_SUCCESS="true"
# Exit the loop.
i=$TIMEOUT
else
sleep $every
i=$(( $i + $every ))
fi
done

if [[ "${DEPLOYMENT_SUCCESS}" != "true" ]]; then
rollback
exit 1
fi
}

######################################################
# When not being tested, run application as expected #
######################################################
Expand Down Expand Up @@ -456,8 +493,12 @@ if [ "$BASH_SOURCE" == "$0" ]; then
echo "Task definition updated successfully"
else
updateService

waitForGreenDeployment
fi

exit 0

fi
#############################
# End application run logic #
Expand Down

0 comments on commit d27197c

Please sign in to comment.