forked from topcoder-platform/community-app
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeploy.sh
executable file
·117 lines (103 loc) · 2.97 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#!/usr/bin/env bash
set -eo pipefail
# more bash-friendly output for jq
JQ="jq --raw-output --exit-status"
ENV=$1
TAG=$2
AWS_REGION=$(eval "echo \$${ENV}_AWS_REGION")
AWS_ECS_CLUSTER=$(eval "echo \$${ENV}_AWS_ECS_CLUSTER")
ACCOUNT_ID=$(eval "echo \$${ENV}_AWS_ACCOUNT_ID")
AWS_ECS_SERVICE=$(eval "echo \$${ENV}_AWS_ECS_SERVICE")
AWS_REPOSITORY=$(eval "echo \$${ENV}_AWS_REPOSITORY")
AWS_ECS_TASK_FAMILY=$(eval "echo \$${ENV}_AWS_ECS_TASK_FAMILY")
DD_SERVICE_NAME=$(eval "echo \$${ENV}_DD_SERVICE_NAME")
DD_TRACE_AGENT_HOSTNAME=$(eval "echo \$${ENV}_DD_TRACE_AGENT_HOSTNAME")
echo $AWS_ECS_SERVICE
configure_aws_cli() {
AWS_ACCESS_KEY_ID=$(eval "echo \$${ENV}_AWS_ACCESS_KEY_ID")
AWS_SECRET_ACCESS_KEY=$(eval "echo \$${ENV}_AWS_SECRET_ACCESS_KEY")
aws --version
aws configure set aws_access_key_id $AWS_ACCESS_KEY_ID
aws configure set aws_secret_access_key $AWS_SECRET_ACCESS_KEY
aws configure set default.region $AWS_REGION
aws configure set default.output json
echo "Configured AWS CLI."
}
deploy_cluster() {
make_task_def
register_definition
if [[ $(aws ecs update-service --cluster $AWS_ECS_CLUSTER --service $AWS_ECS_SERVICE --task-definition $revision | $JQ '.service.taskDefinition') != $revision ]]; then
echo "Error updating service."
return 1
fi
echo "Deployed!"
return 0
}
make_task_def(){
task_template='[
{
"name": "%s",
"image": "%s.dkr.ecr.%s.amazonaws.com/%s:%s",
"essential": true,
"memory": 1000,
"cpu": 100,
"environment": [
{
"name": "NODE_CONFIG_ENV",
"value": "%s"
},
{
"name": "DD_SERVICE_NAME",
"value": "%s"
},
{
"name": "DD_TRACE_AGENT_HOSTNAME",
"value": "%s"
}
],
"portMappings": [
{
"hostPort": 0,
"containerPort": 3000,
"protocol": "tcp"
}
],
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-group": "/aws/ecs/%s",
"awslogs-region": "%s",
"awslogs-stream-prefix": "%s"
}
}
}
]'
if [ "$ENV" = "PROD" ]; then
NODE_CONFIG_ENV=production
elif [ "$ENV" = "PRODBETA" ]; then
NODE_CONFIG_ENV=production
elif [ "$ENV" = "DEV" ]; then
NODE_CONFIG_ENV=development
elif [ "$ENV" = "TEST" ]; then
NODE_CONFIG_ENV=development
fi
task_def=$(printf "$task_template" $AWS_ECS_CLUSTER $ACCOUNT_ID $AWS_REGION $AWS_REPOSITORY $TAG $NODE_CONFIG_ENV $DD_SERVICE_NAME $DD_TRACE_AGENT_HOSTNAME $AWS_ECS_CLUSTER $AWS_REGION $AWS_ECS_CLUSTER)
echo $task_def
}
push_ecr_image() {
echo "Pushing Docker Image..."
eval $(aws ecr get-login --region $AWS_REGION --no-include-email)
docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$AWS_REPOSITORY:$TAG
echo "Docker Image published."
}
register_definition() {
if revision=$(aws ecs register-task-definition --container-definitions "$task_def" --family $AWS_ECS_TASK_FAMILY | $JQ '.taskDefinition.taskDefinitionArn'); then
echo "Revision: $revision"
else
echo "Failed to register task definition"
return 1
fi
}
configure_aws_cli
push_ecr_image
deploy_cluster