Friday, December 1, 2023

Pblishing Docker Images to Amazon ECR with AWS CodeBuild on CodeCommit Changes

 

Description:  In this discussion, I will guide you through the process of automatically publishing Docker images to Amazon Elastic Container Registry (ECR) using AWS Code Build. Learn how to trigger this process each time changes are pushed to AWS Code Commit. Dive into the step-by-step setup, enabling seamless integration between Code Commit, Code Build, and ECR for efficient image management in your AWS environment.






Setup Repository in CodeCommit:

The first setup to prepare code commit repository with source code, Dockerfile for build the image and buildspec.yml file for managing the build workflow


Kindly check the GitHub repository for reference URL

buildspec.yml 

phases: pre_build: commands: - echo Logging in to Amazon ECR... - ECR_LOGIN=$(aws ecr get-login-password --region $AWS_DEFAULT_REGION) - echo $ECR_LOGIN | docker login -u AWS --password-stdin XXXXXXXX.dkr.ecr.ap-south-1.amazonaws.com - REPOSITORY_URI=XXXXXXXX.dkr.ecr.ap-south-1.amazonaws.com/reactrepository - COMMIT_HASH=$(echo $CODEBUILD_RESOLVED_SOURCE_VERSION | cut -c 1-7) - IMAGE_TAG=build-$(echo $CODEBUILD_BUILD_ID | awk -F":" '{print $2}') build: commands: - echo Build started on `date` - echo Building the Docker image... - docker build -t $REPOSITORY_URI:latest . - docker tag $REPOSITORY_URI:latest $REPOSITORY_URI:$IMAGE_TAG post_build: commands: - echo Pushing the Docker image to Amazon ECR... - docker push $REPOSITORY_URI

Setup ECR [Elastic Container Registry]

After setup repository Create the ECR private repository 




Setup CodeBuild for React:  We have code commit repository with source code so our first step is to setup Code Build and IAM role for Build.

To create code build navigate to CodeBuild stage and click on create buildstage







Once you create the Build you will get the project under list and also the custom role which created with the build project. Now need to allocate ECR permission to IAM role.

Allocate Permission to login ECR and upload the image:After build project created navigate to IAM role created with the project and assign permission to login with ECR and upload the image

Permission: AmazonEC2ContainerRegistryFullAccess



 

Build the Project: Once all the above changes applied click on Build the project 





Once the build completed you will get the success result and container also uploaded to ECR





No comments:

Post a Comment