-
Notifications
You must be signed in to change notification settings - Fork 12
/
buildspec.yml
80 lines (79 loc) · 3.79 KB
/
buildspec.yml
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
version: 0.2
phases:
pre_build:
commands:
- echo Build started on `date`
- echo Entered the pre_build phase...
- echo Logging in to Amazon ECR...
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com
- echo Logging in to docker hub...
- echo "$DOCKER_ACCESS_TOKEN" | docker login -u "$DOCKER_USERNAME" --password-stdin
- echo Building the Docker image...
- chmod +x docker-entrypoint.sh
- docker build -t $CONTAINER_NAME:test -f docker/web/Dockerfile-prod .
build:
commands:
- echo not Testing the newly built Docker image...
post_build:
commands:
- IMAGE_TAG=commit-$CODEBUILD_RESOLVED_SOURCE_VERSION
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$CONTAINER_NAME
- echo Tagging the successfully tested image as latest...
- docker tag $CONTAINER_NAME:test $REPOSITORY_URI:latest
- docker tag $CONTAINER_NAME:test $REPOSITORY_URI:$IMAGE_TAG
- echo Pushing the Docker image...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Build completed on `date`
- >-
if [ -n "$BLUE_GREEN" ];
then
aws ecs describe-task-definition --task-definition "$TASK_DEFINITION_FAMILY" | jq > latest-task-definition.json;
cat latest-task-definition.json | jq -r --arg image "$REPOSITORY_URI:$IMAGE_TAG" '.taskDefinition.containerDefinitions | .[0].image = $image' > new-container-defs.json;
NEW_TASK_DEFINITION="$(aws ecs register-task-definition \
--family "$TASK_DEFINITION_FAMILY" \
--container-definitions file://new-container-defs.json \
--task-role-arn "$(cat latest-task-definition.json | jq -r '.taskDefinition.taskRoleArn')" \
--execution-role-arn "$(cat latest-task-definition.json | jq -r '.taskDefinition.executionRoleArn')" \
--network-mode "$(cat latest-task-definition.json | jq -r '.taskDefinition.networkMode')" \
--volumes "$(cat latest-task-definition.json | jq -r '.taskDefinition.volumes')" \
--placement-constraints "$(cat latest-task-definition.json | jq -r '.taskDefinition.placementConstraints')" \
--requires-compatibilities "$(cat latest-task-definition.json | jq -r '.taskDefinition.requiresCompatibilities')")";
NEW_TASK_DEFINITION_ARN=$(echo "$NEW_TASK_DEFINITION" | jq -r '.taskDefinition.taskDefinitionArn')
CONTAINER_PORT=$(echo "$NEW_TASK_DEFINITION" | jq -r '.taskDefinition.containerDefinitions[0].portMappings[0].containerPort')
APPSPEC=$(jq -rn \
--arg task_definition_arn "$NEW_TASK_DEFINITION_ARN" \
--arg container_name "$CONTAINER_NAME" \
--argjson container_port "$CONTAINER_PORT" \
'{
Resources: [
{
TargetService: {
Type: "AWS::ECS::Service",
Properties: {
TaskDefinition: $task_definition_arn,
LoadBalancerInfo: {
ContainerName: $container_name,
ContainerPort: $container_port
}
}
}
}
]
}')
echo "$APPSPEC" > appspec.json
fi
- touch appspec.json
- echo Writing image definitions file...
- printf '[{"name":"%s","imageUri":"%s"}]' $CONTAINER_NAME $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
artifacts:
files:
- imagedefinitions.json
- appspec.json
secondary-artifacts:
imagedefinitions:
files:
- imagedefinitions.json
appspec:
files:
- appspec.json