-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec.yml
More file actions
115 lines (110 loc) · 5.81 KB
/
buildspec.yml
File metadata and controls
115 lines (110 loc) · 5.81 KB
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
version: 0.2
env:
parameter-store:
GITHUB_PERSONAL_ACCESS_TOKEN: /clouddefense/git_private_token
variables:
ENVIRONMENT: "production"
exported-variables:
- IMAGE_TAG
phases:
pre_build:
commands:
- echo "Docker information"
- docker info
- echo "Logging in to Amazon ECR..."
- aws --version
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin $ECR_REPOSITORY_URI
- GITHUB_REPO_URL="https://${GITHUB_PERSONAL_ACCESS_TOKEN}@${CODEBUILD_SOURCE_REPO_URL#https://}"
- echo $GITHUB_REPO_URL
- echo ${CODEBUILD_SOURCE_REPO_URL}
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- export IMAGE_TAG=prod_${COMMIT_HASH}
- echo $IMAGE_TAG
- docker pull $ECR_REPOSITORY_URI:latest || true
- echo "end of pre_build phase"
build:
commands:
- echo Build started on `date`
- echo "Replace robots.txt with environment version"
- |
if [ "$ECS_CLUSTER_NAME" = "ProductionCluster" ]; then
cp absi/templates/robots.prod absi/templates/robots.txt
cp absi/settings_docker_production.py absi/settings_docker.py
else
cp absi/templates/robots.staging absi/templates/robots.txt
fi
cat absi/templates/robots.txt
- echo "Building the Docker image..."
- |
docker build \
--cache-from $ECR_REPOSITORY_URI:latest \
--tag $ECR_REPOSITORY_URI:latest \
--tag $ECR_REPOSITORY_URI:$IMAGE_TAG .
post_build:
commands:
- echo Build completed on `date`
- echo Replace robots.txt with environment version
- |
if [ "$ECS_CLUSTER_NAME" = "ProductionCluster" ]; then
cp absi/templates/robots.prod absi/templates/robots.txt
cp absi/settings_docker_production.py absi/settings_docker.py
else
cp absi/templates/robots.staging absi/templates/robots.txt
fi
cat absi/templates/robots.txt
- echo "Start Running tests"
- docker run --rm -e DEBUG=True -e SECRET_KEY=secret_test_key $ECR_REPOSITORY_URI:latest ./ve/bin/python manage.py test
- echo "Pushing the Docker images..."
- echo docker image git tag - $ECR_REPOSITORY_URI:$IMAGE_TAG
- docker push $ECR_REPOSITORY_URI:$IMAGE_TAG
- docker push $ECR_REPOSITORY_URI:latest
- echo "Writing image definitions file..."
- printf '[{"name":"$ECS_CONTAINER_NAME","imageUri":"%s"}]' $ECR_REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- echo "Retrieve the latest task definition ARN..."
- |
latest_arn=$(aws ecs list-task-definitions \
--family-prefix $ECS_TASK_DEFINITION_FAMILY \
--status ACTIVE \
--sort DESC \
--no-paginate \
--query 'taskDefinitionArns[0]' \
--output text)
- echo "Latest ARN:" $latest_arn
- echo "Describe the latest task definition and save the JSON to a file..."
- aws ecs describe-task-definition --task-definition $latest_arn --query 'taskDefinition' --output json > previous-task-definition.json
- echo "Below jq command to remove the warnings from github because the properties are not needed for the ECS deploy..."
- jq 'del(.compatibilities, .taskDefinitionArn, .requiresAttributes, .revision, .status, .registeredAt, .registeredBy)' previous-task-definition.json > tmpfile && mv tmpfile previous-task-definition.json
- echo "Modify the JSON to update the image..."
- jq --arg image "$ECR_REPOSITORY_URI:$IMAGE_TAG" '.containerDefinitions[0].image = $image' previous-task-definition.json > task-definition.json
- echo "Run aws ecs register task definition..."
- TASK_DEFINITION_ARN=$(aws ecs register-task-definition --cli-input-json file://task-definition.json --query "taskDefinition.taskDefinitionArn" --output text)
- echo "Registered new task definition - $TASK_DEFINITION_ARN"
- echo "Updating Production ECS service"
- |
aws ecs update-service --cluster $ECS_CLUSTER_NAME \
--service $ECS_SERVICE_NAME --task-definition $TASK_DEFINITION_ARN
- echo "Retrieve the latest worker task definition ARN..."
- |
latest_worker_arn=$(aws ecs list-task-definitions \
--family-prefix $ECS_WORKER_TASK_DEFINITION_FAMILY \
--status ACTIVE \
--sort DESC \
--no-paginate \
--query 'taskDefinitionArns[0]' \
--output text)
- echo "Latest worker ARN:" $latest_worker_arn
- echo "Describe the latest task definition and save the JSON to a file..."
- aws ecs describe-task-definition --task-definition $latest_worker_arn --query 'taskDefinition' --output json > previous-task-definition.json
- echo "Below jq command to remove the warnings from github because the properties are not needed for the ECS deploy..."
- jq 'del(.compatibilities, .taskDefinitionArn, .requiresAttributes, .revision, .status, .registeredAt, .registeredBy)' previous-task-definition.json > tmpfile && mv tmpfile previous-task-definition.json
- echo "Modify the JSON to update the image..."
- jq --arg image "$ECR_REPOSITORY_URI:$IMAGE_TAG" '.containerDefinitions[0].image = $image' previous-task-definition.json > task-definition.json
- echo "Run aws ecs register task definition..."
- WORKER_TASK_DEFINITION_ARN=$(aws ecs register-task-definition --cli-input-json file://task-definition.json --query "taskDefinition.taskDefinitionArn" --output text)
- echo "Registered new task definition - WORKER_$TASK_DEFINITION_ARN"
- echo "Updating Staging worker ECS service"
- |
aws ecs update-service --cluster $ECS_CLUSTER_NAME \
--service $ECS_WORKER_SERVICE_NAME --task-definition $WORKER_TASK_DEFINITION_ARN
artifacts:
files: imagedefinitions.json