-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuildspec.codebuild.yml
More file actions
96 lines (91 loc) · 3.47 KB
/
Copy pathbuildspec.codebuild.yml
File metadata and controls
96 lines (91 loc) · 3.47 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
version: 0.2
env:
variables:
DOCKER_BUILDKIT: "1"
CACHE_TAG: "buildcache"
TARGET_PLATFORM: "linux/arm64"
TEMPLATE_DIR: "build-templates/docker"
phases:
pre_build:
commands:
- set -eu
- test -n "${ECR_REPOSITORY_URI:-}"
- test -n "${CODE_BUCKET:-}"
- test -n "${S3_KEY:-}"
- test -n "${IMAGE_TAG:-}"
- ECR_REGISTRY="$(echo "$ECR_REPOSITORY_URI" | cut -d/ -f1)"
- ECR_REGION="$(echo "$ECR_REGISTRY" | cut -d. -f4)"
- echo "Logging in to Amazon ECR..."
- aws ecr get-login-password --region "$ECR_REGION" | docker login --username AWS --password-stdin "$ECR_REGISTRY"
- echo "Checking for remote cache image..."
- |
if docker pull "$ECR_REPOSITORY_URI:$CACHE_TAG"; then
echo "--cache-from $ECR_REPOSITORY_URI:$CACHE_TAG" > /tmp/cache-args.txt
echo "Remote cache found."
else
: > /tmp/cache-args.txt
echo "No remote cache found. First build will proceed without cache."
fi
- echo "Downloading code package from S3..."
- aws s3 cp "s3://$CODE_BUCKET/$S3_KEY" /tmp/code-package.zip
- rm -rf /tmp/build
- mkdir -p /tmp/build
- unzip -q /tmp/code-package.zip -d /tmp/build
- |
APP_PACKAGE_JSON="$(find /tmp/build -maxdepth 5 -type f -name package.json -not -path '*/node_modules/*' | head -n 1)"
if [ -z "$APP_PACKAGE_JSON" ]; then
echo "ERROR: package.json not found after unzip"
find /tmp/build -maxdepth 5 -type f -name package.json
exit 1
fi
APP_DIR="$(dirname "$APP_PACKAGE_JSON")"
echo "$APP_DIR" > /tmp/app-dir.txt
echo "Building from $APP_DIR"
build:
commands:
- set -eu
- APP_DIR="$(cat /tmp/app-dir.txt)"
- CACHE_FROM_ARGS="$(cat /tmp/cache-args.txt)"
- cp -R "$CODEBUILD_SRC_DIR/$TEMPLATE_DIR" /tmp/template-dir
- cp "$CODEBUILD_SRC_DIR/scripts/select-dockerfile-template.sh" /tmp/select-dockerfile-template.sh
- chmod +x /tmp/select-dockerfile-template.sh
- cd "$APP_DIR"
- /tmp/select-dockerfile-template.sh /tmp/template-dir
- echo "Building Docker image..."
- |
if [ -n "$CACHE_FROM_ARGS" ]; then
docker build \
--platform "$TARGET_PLATFORM" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
$CACHE_FROM_ARGS \
-t "$ECR_REPOSITORY_URI:$IMAGE_TAG" \
-t "$ECR_REPOSITORY_URI:latest" \
-t "$ECR_REPOSITORY_URI:$CACHE_TAG" \
.
else
docker build \
--platform "$TARGET_PLATFORM" \
--build-arg BUILDKIT_INLINE_CACHE=1 \
-t "$ECR_REPOSITORY_URI:$IMAGE_TAG" \
-t "$ECR_REPOSITORY_URI:latest" \
-t "$ECR_REPOSITORY_URI:$CACHE_TAG" \
.
fi
post_build:
commands:
- set -eu
- |
if [ "${CODEBUILD_BUILD_SUCCEEDING:-0}" = "1" ]; then
echo "Pushing Docker image..."
docker push "$ECR_REPOSITORY_URI:$IMAGE_TAG"
docker push "$ECR_REPOSITORY_URI:latest"
docker push "$ECR_REPOSITORY_URI:$CACHE_TAG"
echo "IMAGE_URI=$ECR_REPOSITORY_URI:$IMAGE_TAG" > /tmp/build-output.txt
echo "Build completed successfully"
else
echo "Build phase failed earlier. Skipping push."
exit 1
fi
artifacts:
files:
- /tmp/build-output.txt