Skip to content

Commit 3f54bc9

Browse files
author
Workshop Participant
committed
Add BuildSpec for pushing Docker image to ECR
1 parent 910924d commit 3f54bc9

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

buildspec_docker.yml

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: 0.2
2+
3+
env:
4+
variables:
5+
IMAGE_TAG: "latest" # Default value for the tag. Can be overridden.
6+
7+
phases:
8+
install:
9+
commands:
10+
- npm install
11+
12+
pre_build:
13+
commands:
14+
- echo "Extracting AWS Account ID from CODEBUILD_BUILD_ARN..."
15+
- export ACCOUNT_ID=$(echo $CODEBUILD_BUILD_ARN | cut -f5 -d ':')
16+
- export repository_name="hotel-app" # Set your repository name here
17+
- echo "Logging into Amazon ECR..."
18+
- aws ecr get-login-password --region $AWS_REGION | docker login --username AWS --password-stdin $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com
19+
20+
build:
21+
commands:
22+
- echo "Building the Docker image..."
23+
- docker build -t $repository_name:$IMAGE_TAG .
24+
- echo "Tagging the Docker image for ECR..."
25+
- docker tag $repository_name:$IMAGE_TAG $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$repository_name:$IMAGE_TAG
26+
- echo "Pushing the Docker image to ECR..."
27+
- docker push $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$repository_name:$IMAGE_TAG
28+
29+
post_build:
30+
commands:
31+
- echo Writing image definitions file...
32+
- printf '[{"name":"hotel-app","imageUri":"%s"}]' $ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.com/$repository_name:$IMAGE_TAG > imagedefinitions.json
33+
34+
artifacts:
35+
files: imagedefinitions.json

0 commit comments

Comments
 (0)