-
Notifications
You must be signed in to change notification settings - Fork 62
/
buildspec.yml
84 lines (82 loc) · 4.41 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
81
82
83
84
version: 0.2
env:
shell: bash
variables:
#SLACK_CHANNEL: "p-qpp-sub-alerts"
SLACK_API_URL: "https://slack.com/api/chat.postMessage"
parameter-store:
AWS_ACCOUNT_ID: "/qppar-sf/account_id"
DOCKERHUB_USERNAME: "/qppar-sf/DOCKERHUB_USERNAME"
DOCKERHUB_PASS: "/qppar-sf/DOCKERHUB_PASS"
#SLACK_WEBHOOK: "/qppar-sf/dev/conversion_tool/slack_hook_url"
SLACK_CHANNEL: "/qppar-sf/global/slack_channel"
SLACK_SECRET_TOKEN: "/qppar-sf/global/slack_token"
PART_FILE: "/qppar-sf/conversion_tool/CPC_PLUS_FILE_NAME"
PART_FILE_BUCKET: "/qppar-sf/$ENVIRONMENT/conversion_tool/CPC_PLUS_BUCKET_NAME"
OUTPUT_PART_FILE: "/qppar-sf/$ENVIRONMENT/conversion_tool/CPC_PLUS_VALIDATION_FILE"
phases:
install:
runtime-versions:
python: 3.8
pre_build:
on-failure: ABORT
commands:
- echo ${CODEBUILD_SRC_DIR}
- echo ${AWS_DEFAULT_REGION}
- echo ${AWS_ACCOUNT_ID}
- echo ${ENVIRONMENT}
- echo ${CODEBUILD_RESOLVED_SOURCE_VERSION}
- echo ${CODEBUILD_BUILD_ID}
- echo ${CODEBUILD_BUILD_ARN}
- echo Getting Certificates for ${ENVIRONMENT}
- chmod +x ./qppsfct-copy-certs.sh
- ./qppsfct-copy-certs.sh $ENVIRONMENT $AWS_DEFAULT_REGION
- pip install openpyxl
- echo "Updating participation file"
- chmod +x ./upload-part-file.sh
- ./upload-part-file.sh $PART_FILE_BUCKET $PART_FILE $OUTPUT_PART_FILE $AWS_DEFAULT_REGION
- echo "Logging in to Amazon dockerHub ECR"
- docker login --username $DOCKERHUB_USERNAME --password $DOCKERHUB_PASS
- aws ecr get-login-password --region $AWS_DEFAULT_REGION | docker login --username AWS --password-stdin ${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com
build:
on-failure: ABORT
commands:
- REPOSITORY_URI="${AWS_ACCOUNT_ID}.dkr.ecr.us-east-1.amazonaws.com/qppsf/conversion-tool/${ENVIRONMENT}"
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION)
- IMAGE_TAG=${COMMIT_HASH:=latest}
- ECR_IMAGE="${REPOSITORY_URI}:${IMAGE_TAG}"
- TASK_FAMILY="qppsf-conversion-tool-td-${ENVIRONMENT}"
- ECS_CLUSTER="qppsf-conversion-tool-${ENVIRONMENT}"
- SERVICE_NAME="conversion-tool-service-${ENVIRONMENT}"
- echo "Building Container Image for ConversionTool"
- docker build -t $REPOSITORY_URI:latest .
- docker tag $REPOSITORY_URI:latest ${ECR_IMAGE}
- echo Pushing the Docker image
- docker push $REPOSITORY_URI:latest
- docker push ${ECR_IMAGE}
- TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition ${TASK_FAMILY} --query taskDefinition)
- NEW_TASK_DEFINITION=$(echo $TASK_DEFINITION | jq -r --arg IMAGE ${ECR_IMAGE} '.containerDefinitions[0].image=$IMAGE | del(.taskDefinitionArn) | del(.revision) | del(.status) | del(.requiresAttributes) | del(.compatibilities) | del(.registeredAt) | del(.registeredBy)')
- echo $NEW_TASK_DEFINITION > qppsf-conversion-tool-td-${ENVIRONMENT}.json
- aws ecs register-task-definition --family ${TASK_FAMILY} --region "$AWS_DEFAULT_REGION" --cli-input-json file://qppsf-conversion-tool-td-${ENVIRONMENT}.json
- LATEST_TASK_DEFINITION=$(aws ecs describe-task-definition --task-definition ${TASK_FAMILY} --query taskDefinition)
- NEW_REVISION=$(echo $LATEST_TASK_DEFINITION | jq '.revision')
- echo "Starting ECS Deployment"
- aws ecs update-service --cluster ${ECS_CLUSTER} --service ${SERVICE_NAME} --task-definition ${TASK_FAMILY}:${NEW_REVISION} --force-new-deployment
- aws ecs wait services-stable --cluster ${ECS_CLUSTER} --services ${SERVICE_NAME}
post_build:
commands:
- |
SLACK_MSG=""
SLACK_EMOJI=""
if [[ $CODEBUILD_BUILD_SUCCEEDING -eq 1 ]]
then
SLACK_EMOJI=":ok:"
SLACK_MSG="${SLACK_EMOJI} BUILD SUCCEEDED for Conversion Tool ${ENVIRONMENT} Environment, ${CODEBUILD_BUILD_ARN}"
else
SLACK_EMOJI=":warning:"
SLACK_MSG="${SLACK_EMOJI} BUILD FAILED for Conversion Tool ${ENVIRONMENT} Environment, ${CODEBUILD_BUILD_ARN}"
fi
SLACK_HDR1="'Authorization:Bearer ${SLACK_SECRET_TOKEN}'"
SLACK_HDR2="'Content-type: application/json'"
SLACK_PAYLOAD="'"$(echo "{ \"channel\":\"${SLACK_CHANNEL}\", \"blocks\": [ { \"type\":\"section\", \"text\": { \"type\":\"mrkdwn\", \"text\":\"${SLACK_MSG}\" } } ] }" | jq -rc '.')"'"
eval $(echo curl -X POST -H ${SLACK_HDR1} -H ${SLACK_HDR2} -d ${SLACK_PAYLOAD} ${SLACK_API_URL} )