Last active
September 15, 2022 07:36
-
-
Save TylerBrock/59c0b92ab363c1d6e0174ae275d816a3 to your computer and use it in GitHub Desktop.
Amazon ECS Deploy Script -- for public use
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
#!/usr/bin/env bash | |
set -e | |
status() { | |
echo -e "[DEPLOY]: ${1}" | |
} | |
# Set the version to the branch name by default | |
ver_tag=${CIRCLE_BRANCH} | |
sha_tag=$(echo $CIRCLE_SHA1 | cut -c -8) | |
# This is a build destined for release | |
if [ -n "$CIRCLE_TAG" ]; then | |
ver_tag=${CIRCLE_TAG} | |
fi | |
# Build Docker Registry namespace | |
namespace="hustle/${CIRCLE_PROJECT_REPONAME}" | |
# A string reference to the new image | |
sha_image="${ECR_REPO}/${namespace}:git-${sha_tag}" | |
ver_image="${ECR_REPO}/${namespace}:${ver_tag}" | |
echo ${sha_image} | |
echo ${ver_image} | |
configure_aws_cli() { | |
aws configure set default.region us-east-1 | |
aws configure set default.output json | |
eval $(aws ecr get-login --no-include-email --region us-east-1) | |
} | |
build() { | |
docker build -f Dockerfile.prod -t backend:prod . | |
} | |
tag() { | |
docker tag backend:prod ${sha_image} | |
docker tag backend:prod ${ver_image} | |
} | |
push() { | |
docker push ${sha_image} | |
docker push ${ver_image} | |
} | |
configure_aws_cli | |
build | |
tag | |
push | |
exit 0 |
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
#!/usr/bin/env bash | |
# Hustle ECS Deploy Script v1.0.0 | |
# Requires docker + jq + aws-cli | |
set -e | |
usage() { | |
echo "Usage: $0 -c <cluster name> -s <services>" | |
} | |
status() { | |
echo -e "[DEPLOY]: ${1}" | |
} | |
while getopts ":c:s:" o; do | |
case "${o}" in | |
c) | |
cluster=${OPTARG} | |
;; | |
s) | |
services=${OPTARG} | |
;; | |
*) | |
usage | |
;; | |
esac | |
done | |
if [ -z "${cluster}" ]; then | |
usage; | |
exit -1; | |
fi | |
if [ -z "${services}" ]; then | |
usage; | |
exit -1; | |
fi | |
# Split services by comma into array | |
servicesArray=(${services//,/ }) | |
# Set the version to the branch name by default | |
tag=$(echo $CIRCLE_SHA1 | cut -c -8) | |
# Build Docker Registry namespace | |
ecr_repo=${ECR_REPO} | |
namespace="hustle/${CIRCLE_PROJECT_REPONAME}" | |
# A string reference to the new image | |
image="${ecr_repo}/${namespace}:git-${tag}" | |
configure_aws_cli() { | |
aws configure set default.region us-east-1 | |
aws configure set default.output json | |
eval $(aws ecr get-login --region us-east-1) | |
} | |
get_task_arn() { | |
# get ARN of task running on the service | |
active_task_arn=$( | |
aws ecs describe-services \ | |
--cluster ${cluster} \ | |
--services ${service} \ | |
--output text \ | |
--query 'services[?serviceName==`'"${service}"'`].taskDefinition' | |
) | |
} | |
get_task_def() { | |
# get task definition - selecting only the fields we need | |
task_definition=$( | |
aws ecs describe-task-definition \ | |
--task-definition ${active_task_arn} \ | |
--query 'taskDefinition' \ | |
| jq '{containerDefinitions, volumes, family}' | |
) | |
} | |
update_task_def() { | |
# modify task definition to use new image | |
new_task_definition=$(echo ${task_definition} | jq ".containerDefinitions[0].image = \"${image}\"") | |
} | |
register_task_def() { | |
# create a new version (same other than pointing to the latest docker image) | |
new_task_arn=$( | |
aws ecs register-task-definition \ | |
--output text \ | |
--query 'taskDefinition.taskDefinitionArn' \ | |
--cli-input-json "${new_task_definition}" | |
) | |
} | |
update_service() { | |
status "Updating service '${service}' to task definition revision (${new_task_arn})" | |
updated_service=$( | |
aws ecs update-service \ | |
--cluster ${cluster} \ | |
--service ${service} \ | |
--task-definition ${new_task_arn} | |
) | |
} | |
wait_for_service() { | |
status "Waiting for ECS service '${service}' to become stable on ${cluster} cluster..." | |
aws ecs wait services-stable --cluster ${cluster} --service ${service} | |
status "Done!" | |
} | |
deploy_service() { | |
get_task_arn | |
get_task_def | |
update_task_def | |
register_task_def | |
update_service | |
} | |
deploy() { | |
for service in "${servicesArray[@]}"; do | |
status "Deploying '${service}' service on ${cluster} cluster" | |
deploy_service | |
done | |
for service in "${servicesArray[@]}"; do | |
status "Waiting for '${service}' service on ${cluster} cluster..." | |
wait_for_service | |
done | |
} | |
configure_aws_cli | |
deploy | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment