-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdeploy.sh
executable file
·55 lines (45 loc) · 1.71 KB
/
deploy.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/bin/bash
# usage: ./deploy.sh app-name linux/amd64
set -e
export APP=$1
export ARCH=$2
export VERSION=$(cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w 50 | head -n 1)
# login to ECR
ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
REGISTRY=${ACCOUNT}.dkr.ecr.${AWS_REGION}.amazonaws.com
echo "logging into to ecr: ${REGISTRY}"
aws ecr get-login-password | docker login --username AWS --password-stdin ${REGISTRY}
# build and push image
IMAGE=${REGISTRY}/${APP}:${VERSION}
echo ""
echo "building and pushing image: ${IMAGE}"
docker build --platform ${ARCH} -t ${IMAGE} .
docker push ${IMAGE}
# deploy new image to ECS
CLUSTER=${APP}
SERVICE=${APP}
TASK_FAMILY=${APP}
# create a new task definition
echo ""
echo "creating new task definition for image: ${IMAGE}"
TASK_DEF=$(aws ecs describe-task-definition --task-definition ${TASK_FAMILY})
NEW_TASK_DEF=$(echo $TASK_DEF | jq --arg IMAGE "$IMAGE" '.taskDefinition |
.containerDefinitions[0].image = $IMAGE |
del(.taskDefinitionArn) | del(.revision) | del(.status) |
del(.requiresAttributes) | del(.compatibilities) |
del(.registeredAt) | del(.registeredBy)
')
# register new task definition
REGISTRATION=$(aws ecs register-task-definition --cli-input-json "${NEW_TASK_DEF}")
REV=$(echo ${REGISTRATION} | jq '.taskDefinition.revision')
echo ""
echo "registered revision: ${REV}"
# tell service to use it
aws ecs update-service --cluster ${CLUSTER} --service ${SERVICE} \
--task-definition ${TASK_FAMILY}:${REV}
echo ""
echo "service updated to use the new task definition with image: ${IMAGE}"
echo "waiting for service to finish deployment..."
aws ecs wait services-stable --cluster ${CLUSTER} --services ${SERVICE}
echo ""
echo "deployment complete"