-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec.yml
More file actions
84 lines (77 loc) · 3.13 KB
/
buildspec.yml
File metadata and controls
84 lines (77 loc) · 3.13 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
version: 0.2
# Environment variables that can be overridden in CodeBuild project
env:
variables:
AWS_DEFAULT_REGION: ap-northeast-1
AWS_ACCOUNT_ID: 123456789012
IMAGE_REPO_NAME: transcribe-service
IMAGE_TAG: latest
parameter-store:
DOCKER_BUILDKIT: /codebuild/docker_buildkit
phases:
install:
runtime-versions:
docker: 20
commands:
- echo Logging in to Amazon ECR...
- aws --version
- echo $AWS_DEFAULT_REGION
- echo $AWS_ACCOUNT_ID
- 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
pre_build:
commands:
- echo Pre-build started on `date`
- echo Checking if ECR repository exists...
- |
if ! aws ecr describe-repositories --repository-names $IMAGE_REPO_NAME --region $AWS_DEFAULT_REGION 2>/dev/null; then
echo "Creating ECR repository $IMAGE_REPO_NAME..."
aws ecr create-repository --repository-name $IMAGE_REPO_NAME --region $AWS_DEFAULT_REGION
aws ecr put-lifecycle-policy --repository-name $IMAGE_REPO_NAME --lifecycle-policy-text '{"rules":[{"rulePriority":1,"selection":{"tagStatus":"untagged","countType":"sinceImagePushed","countUnit":"days","countNumber":7},"action":{"type":"expire"}}]}'
else
echo "ECR repository $IMAGE_REPO_NAME already exists"
fi
- REPOSITORY_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_DEFAULT_REGION.amazonaws.com/$IMAGE_REPO_NAME
- COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7)
- BUILD_TAG=${COMMIT_HASH:=latest}
- echo Repository URI is $REPOSITORY_URI
- echo Build tag is $BUILD_TAG
build:
commands:
- echo Build started on `date`
- echo Building the Docker image...
# Enable Docker BuildKit for better caching and performance
- export DOCKER_BUILDKIT=1
# Build the Docker image with build arguments
- |
docker build \
--build-arg BUILDKIT_INLINE_CACHE=1 \
--cache-from $REPOSITORY_URI:latest \
-t $IMAGE_REPO_NAME:$IMAGE_TAG \
-t $IMAGE_REPO_NAME:$BUILD_TAG \
-t $REPOSITORY_URI:$IMAGE_TAG \
-t $REPOSITORY_URI:$BUILD_TAG \
.
- echo Build completed on `date`
post_build:
commands:
- echo Post-build started on `date`
- echo Pushing the Docker images...
# Push both latest and commit hash tagged images
- docker push $REPOSITORY_URI:$IMAGE_TAG
- docker push $REPOSITORY_URI:$BUILD_TAG
- echo Docker image push completed on `date`
# Create image definitions file for ECS deployment
- echo Writing image definitions file...
- printf '[{"name":"transcribe-container","imageUri":"%s"}]' $REPOSITORY_URI:$BUILD_TAG > imagedefinitions.json
- cat imagedefinitions.json
# Output the image URI for use in subsequent stages
- echo "Image URI:" $REPOSITORY_URI:$BUILD_TAG
artifacts:
files:
- imagedefinitions.json
- ecs-task-definition.json
name: transcribe-service-artifacts
cache:
paths:
- '/root/.cache/pip/**/*'
- '/var/lib/docker/**/*'