forked from cogini/phoenix_container_example_old
-
Notifications
You must be signed in to change notification settings - Fork 0
/
buildspec.yml
167 lines (142 loc) · 6.39 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
---
# 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:
# Enable docker buildx
DOCKER_BUILDKIT: 1
DOCKER_CLI_EXPERIMENTAL: enabled
COMPOSE_DOCKER_CLI_BUILD: 1
phases:
install:
# runtime-versions:
# docker: 19
commands:
- /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"
pre_build:
commands:
# - cat /etc/lsb-release
# - python3 --version
# - aws --version
# - docker version
# - docker-compose --version
# 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-*
# - ls -l /root/.cache/docker
# - ls -l /var/lib/docker
# Create builder instance
- docker buildx create --name docker-container
- docker buildx use docker-container
# - docker buildx inspect --bootstrap
- export REGISTRY="$(dirname $REPO_URL)/"
# 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, installed in custom build image
- mkdir -p /root/.docker
- echo '{"credsStore":"ecr-login"}' | tee /root/.docker/config.json
# - rm -rf /root/.cache/docker
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- IMAGE_TAG=${COMMIT_HASH:=latest}
build:
commands:
# - BUILD_CACHE_REPO_URL="${REGISTRY}foo-app-ecs-build-cache"
# - docker buildx use docker-container
# - COMPOSE_DOCKER_CLI_BUILD=1 DOCKER_BUILDKIT=1 docker-compose build
# - docker-compose run test mix test
# - $TEMPLATE_DIR/build.sh
# - docker buildx use default
# - docker buildx bake -f docker-compose.yml --print
# - docker buildx bake -f docker-compose.yml
# - docker-compose run test mix test
# - docker buildx use docker-container
# - docker-compose push app
# - docker buildx use docker-container
# Scan for app 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 .
- docker buildx bake -f docker-bake.hcl ci
# Build app container for tests
# - docker-compose build test
# Run tests
- DATABASE_HOST=db docker-compose up test
- DATABASE_HOST=db docker-compose run test mix test
- DATABASE_HOST=db docker-compose run test mix credo
- DATABASE_HOST=db docker-compose run test mix deps.audit
- DATABASE_HOST=db docker-compose run test mix sobelow
# Build and push final deploy container
# - $TEMPLATE_DIR/build.sh
# - env DOCKERFILE=deploy/Dockerfile.debian $TEMPLATE_DIR/build.sh
# Build multiple architectures
# - env PLATFORM="--platform linux/amd64,linux/arm64" DOCKERFILE=deploy/Dockerfile.alpine $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 app
# 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 app
- 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" "$REPO_URL:$IMAGE_TAG" | tee imagedefinitions.json
# Generate imageDetail.json file for CodeDeploy ECS blue/green deploy action
- printf '{"ImageURI":"%s"}' "$REPO_URL:$IMAGE_TAG" | 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 's!<NAME>!'$CONTAINER_NAME'!g' ecs/appspec.yml ecs/taskdef.json
- sed -i 's!<PORT>!'$PORT'!g' ecs/appspec.yml ecs/taskdef.json
- sed -i 's!<TASK_ROLE_ARN>!'$TASK_ROLE_ARN'!g' ecs/taskdef.json
- sed -i 's!<EXECUTION_ROLE_ARN>!'$EXECUTION_ROLE_ARN'!g' ecs/taskdef.json
- sed -i 's!<CPU>!'$CPU'!g' ecs/taskdef.json
- sed -i 's!<MEMORY>!'$MEMORY'!g' ecs/taskdef.json
- sed -i 's!<AWSLOGS_GROUP>!'$AWSLOGS_GROUP'!g' ecs/taskdef.json
- sed -i 's!<AWSLOGS_REGION>!'$AWS_REGION'!g' ecs/taskdef.json
- sed -i 's!<AWSLOGS_STREAM_PREFIX>!'$AWSLOGS_STREAM_PREFIX'!g' ecs/taskdef.json
- sed -i 's!<CONFIG_S3_BUCKET>!'$CONFIG_S3_BUCKET'!g' ecs/taskdef.json
- sed -i 's!<CONFIG_S3_PREFIX>!'$CONFIG_S3_PREFIX'!g' ecs/taskdef.json
- sed -i 's!<AWS_REGION>!'$AWS_REGION'!g' ecs/taskdef.json
- sed -i 's!<AWS_ACCOUNT_ID>!'$AWS_ACCOUNT_ID'!g' ecs/taskdef.json
- cat ecs/appspec.yml
- cat ecs/taskdef.json
- cp ecs/appspec.yml .
- cp ecs/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/**/*'