-
Notifications
You must be signed in to change notification settings - Fork 4
/
buildspec.yml
212 lines (180 loc) · 8.26 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
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
# Build app in Docker and push to repo
#
# https://docs.aws.amazon.com/AmazonECS/latest/userguide/ecs-cd-pipeline.html
# Environment vars:
# REPO_URL: ECS app repository
# CACHE_REPO_URL: ECS repository for caching
# CONTAINER_NAME: container name in service task definition
# https://docs.aws.amazon.com/codebuild/latest/userguide/build-env-ref-env-vars.html
# AWS_DEFAULT_REGION
# CODEBUILD_RESOLVED_SOURCE_VERSION
# https://docs.aws.amazon.com/codebuild/latest/userguide/build-spec-ref.html
version: 0.2
env:
variables:
DOCKER_BUILDKIT: 1
DOCKER_CLI_EXPERIMENTAL: enabled
COMPOSE_DOCKER_CLI_BUILD: 1
# TASKDEF=ecs/taskdef.json
TASKDEF: ecs/taskdef-otel.json
CPU_ARCH: ARM64
# CPU_ARCH: X86_64
COMPOSE_FILE: docker-compose.yml
DOCKER_FILE: deploy/debian.Dockerfile
parameter-store:
DOCKERHUB_USERNAME: /cogini/foo/dev/creds/dockerhub_username
DOCKERHUB_TOKEN: /cogini/foo/dev/creds/dockerhub_token
phases:
install:
# runtime-versions:
# docker: latest
commands:
- apt-get install jq
- export REGISTRY_NAME="$(dirname $REPO_URL)"
- export REGISTRY="${REGISTRY_NAME}/"
# - mkdir -p /etc/docker
# - sed -i -e "s!<REGISTRY_NAME>!${REGISTRY_NAME}!g" ecs/docker-daemon.json | tee /etc/docker/daemon.json
# - cat /etc/docker/daemon.json
- /usr/sbin/service docker start
# https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectEnvironment.html
# - nohup /usr/local/bin/dockerd --host=unix:///var/run/docker.sock --host=tcp://0.0.0.0:2375 --storage-driver=overlay&
# - timeout 15 sh -c "until docker info; do echo .; sleep 1; done"
# - export DOCKERHUB_AUTH="$(echo "${DOCKERHUB_USERNAME}:${DOCKERHUB_TOKEN}" | base64)"
- mkdir -p /root/.docker
# - echo '{"credsStore":"ecr-login"}' | tee /root/.docker/config.json
# - sed -i -e "s/<REGISTRY_NAME>/${REGISTRY_NAME}/g" -e "s/<DOCKERHUB_AUTH>/${DOCKERHUB_AUTH}/g" ecs/docker-config.json
# - cp ecs/docker-config.json /root/.docker/config.json
- sed -e "s/<REGISTRY_NAME>/${REGISTRY_NAME}/g" ecs/docker-config.json > /root/.docker/config.json
# - jq --arg registry "$REGISTRY_NAME" '.credHelpers += {($registry):"ecr-login"}' ecs/docker-config.json
# - cat /root/.docker/config.json
# Add support for cross arch builds (Arm)
# - docker run --rm --privileged linuxkit/binfmt:v0.8
# - cat /proc/sys/fs/binfmt_misc/qemu-aarch64
# - ls -l /proc/sys/fs/binfmt_misc/*
# - ls -1 /proc/sys/fs/binfmt_misc/qemu-*
# Create builder instance
- docker buildx create --name docker-container
- docker buildx use docker-container
# - docker buildx use default
# - docker buildx inspect --bootstrap
# Log into Docker Hub
- echo "$DOCKERHUB_TOKEN" | docker login --username "$DOCKERHUB_USERNAME" --password-stdin
# https://docs.aws.amazon.com/codebuild/latest/APIReference/API_ProjectEnvironment.html
# https://github.com/awslabs/amazon-ecr-credential-helper
# - PYTHON=python2 amazon-linux-extras enable docker
# - yum clean metadata
# - yum -y install amazon-ecr-credential-helper
# - cat /etc/lsb-release
# - python3 --version
# - aws --version
# - docker compose --version
- docker info
- docker system df -v
- docker df -v
- df -k
- docker --version
- uname -s
- uname -m
pre_build:
commands:
# Log into Amazon ECR
# Use new aws cli for login
# - aws ecr get-login-password --region "$AWS_DEFAULT_REGION" | docker login --username AWS --password-stdin "$REGISTRY"
# Use old aws cli for login
# - $(aws ecr get-login --region "$AWS_DEFAULT_REGION" --no-include-email)
# Use amazon-ecr-credential-helper for login
# Use amazon-ecr-credential-helper for login, installed in custom build image
# - mkdir -p /root/.docker
# - echo '{"credsStore":"ecr-login"}' | tee /root/.docker/config.json
# - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- COMMIT_HASH=$CODEBUILD_RESOLVED_SOURCE_VERSION
- IMAGE_TAG=${COMMIT_HASH:=latest}
- IMAGE_URI="${REPO_URL}:${IMAGE_TAG}"
# Ensure that scripts are executable
- chmod u+x rel/overlays/bin/*
build:
commands:
# Scan app source for vulnerabilities
# - trivy filesystem --exit-code 0 --severity HIGH --no-progress .
# - trivy filesystem --exit-code 1 --severity CRITICAL --no-progress .
# - grype -vv --fail-on medium .
# - grype -vv .
# - BUILD_CACHE_REPO_URL="${REGISTRY}foo-app-ecs-build-cache"
# - COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker compose build
# - docker compose run test mix test
# - $TEMPLATE_DIR/build.sh
# - docker buildx bake -f docker-compose.yml --print
# - docker buildx bake -f docker-compose.yml
#
# - docker-compose push app
# - docker buildx bake -f docker-bake.hcl ci
# Build app container for tests
- docker compose build test
# Run tests
- docker compose up --detach test
- docker compose run test mix test
- docker compose run test mix do format --check-formatted, deps.unlock --check-unused, credo --all, hex.audit, deps.audit, sobelow
- docker compose run test mix dialyzer --no-check
# Build and push final deploy container
# - $TEMPLATE_DIR/build.sh
# - env DOCKERFILE=deploy/debian.Dockerfile $TEMPLATE_DIR/build.sh
# Build multiple architectures
# - env PLATFORM="--platform linux/amd64,linux/arm64" DOCKERFILE=deploy/alpine.Dockerfile $TEMPLATE_DIR/build.sh
# Extract assets from build container
# - $TEMPLATE_DIR/build-artifacts.sh
# - CACHE_REPO_URL="${REGISTRY}foo-app-ecs-build-cache"
# - CACHE_TYPE=registry $TEMPLATE_DIR/build.sh
# Build final image
- docker compose build prod
# Scan for security vulnerabilities
# - docker compose build vuln
# - docker buildx bake -f docker-bake.hcl vuln
# Push final image to container registry
- docker compose push prod
# - docker buildx bake -f docker-bake.hcl --push app
# Write image definition files
# https://docs.aws.amazon.com/codepipeline/latest/userguide/file-reference.html
# Generate imagedefinitions.json file for standard ECS deploy action
- printf '[{"name":"%s","imageUri":"%s"}]' "$CONTAINER_NAME" "$IMAGE_URI" | tee imagedefinitions.json
# Generate imageDetail.json file for CodeDeploy ECS blue/green deploy action
- printf '{"ImageURI":"%s"}' "$IMAGE_URI" | tee imageDetail.json
# Generate files based on env vars passed from Terraform
# https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-resources.html
- sed -i -e "s!<NAME>!${CONTAINER_NAME}!g" -e "s!<PORT>!${PORT}!g" ecs/appspec.yml
- sed -i -e "s!<AWSLOGS_GROUP>!${AWSLOGS_GROUP}!g" $TASKDEF
- sed -i -e "s!<AWSLOGS_REGION>!${AWS_REGION}!g" $TASKDEF
- sed -i -e "s!<AWSLOGS_STREAM_PREFIX>!${AWSLOGS_STREAM_PREFIX}!g" $TASKDEF
- sed -i -e "s!<AWS_ACCOUNT_ID>!${AWS_ACCOUNT_ID}!g" $TASKDEF
- sed -i -e "s!<AWS_REGION>!${AWS_REGION}!g" $TASKDEF
- sed -i -e "s!<CONFIG_S3_BUCKET>!${CONFIG_S3_BUCKET}!g" -e "s!<CONFIG_S3_PREFIX>!${CONFIG_S3_PREFIX}!g" $TASKDEF
- sed -i -e "s!<CPU>!${CPU}!g" -e "s!<MEMORY>!${MEMORY}!g" $TASKDEF
- sed -i -e "s!<CPU_ARCH>!${CPU_ARCH}!g" $TASKDEF
- sed -i -e "s!<EXECUTION_ROLE_ARN>!${EXECUTION_ROLE_ARN}!g" $TASKDEF
- sed -i -e "s!<NAME>!${CONTAINER_NAME}!g" -e "s!<PORT>!${PORT}!g" $TASKDEF
- sed -i -e "s!<TASK_ROLE_ARN>!${TASK_ROLE_ARN}!g" $TASKDEF
- jq --null-input -f ecs/taskdef-otel.jq
- cat ecs/appspec.yml
- cat $TASKDEF
- cp ecs/appspec.yml .
- cp $TASKDEF taskdef.json
# post_build:
# commands:
artifacts:
files:
- imagedefinitions.json
- imageDetail.json
- appspec.yml
- taskdef.json
# cache:
# paths:
# # - '/var/lib/docker/**/*'
# # - '/root/.cache/docker/**/*'
# # - '/root/.cache/trivy/**/*'
# # - '/root/.cache/grype/**/*'
# reports:
# mix-test-reports:
# files:
# # - "_build/test/lib/phoenix_container_example/reports/*.xml"
# - "junit-reports/*.xml"
# file-format: "JUNITXML"