Logo

dev-resources.site

for different kinds of informations.

6-part series - (5) Create a CodeBuild Project, upload the code and push it to the master repository

Published at
2/17/2023
Categories
codepipeline
codebuild
ecr
ecs
Author
awsmine
Categories
4 categories in total
codepipeline
open
codebuild
open
ecr
open
ecs
open
Author
7 person written this
awsmine
open
6-part series - (5) Create a CodeBuild Project, upload the code and push it to the master repository

In this 6-part series on configuring a CI/CD pipeline using Customized Docker image on an Apache Web Server, Application Load Balancer, ECS, ECR, CodeCommit, CodeBuild, CodeDeply services -

In this 5th article, We will create a CodeBuild Project, upload the code and push it to the master repository

1st article

2nd article

3rd article

4th article

Let’s get started!

Please visit my GitHub Repository for Docker/ECS/ECR articles on various topics being updated on constant basis.

Objectives:

1. Create a CodeBuild Project

2. Create a buildspec.yaml file

3. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

4. Provide the ECR permissions to the Codebuild role

5. Start build project

6. Create a imagedefinitions.json file

7. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

Pre-requisites:

  • AWS user account with admin access, not a root account.
  • AWS CLI.

Resources Used:

CodeCommit

CodeBuild

AWS CodePipeline

Steps for implementation to this project

1. Create a CodeBuild Project

  • On the Codecommit dashboard, Build, Build projects, Create build project, my_codebuild_project, Source provider : Select AWS CodeCommit, Repository - my-codecommit-repo, Branch - master, Environment image - Managed image, Operating system - Amazon Linux 2, Runtime(s) - Standard, image : Choose aws/codebuild/amazonlinux2-x86_64-standard:3.0

Check the privileged, as it will build the docker image.[Important]

  • In Service role, New service role - codebuild-my-service-role (Do not change), Buildspec, Use a buildspec file selected automatically, all defaults

  • Create build project

Image description

2. Create a buildspec.yaml file

  • On to the created ECR repository, click my-ecr, View push commands

  • Copy the first command, i.e the login command.

aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode
  • Close the push commands page, Now copy the repository URI beside the repo name.
<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr
Enter fullscreen mode Exit fullscreen mode

Image description

In the following code, change only the URI

version: 0.2
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t my-ecr:latest .
      - docker tag my-ecr:latest <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode

create the buildspec.yaml file

  • SSH into the EC2 Instance and navigate to the repo folder
cd /opt/docker/my-codecommit-repo
Enter fullscreen mode Exit fullscreen mode
  • Create a buildspec.yaml file

  • vi buildspec.yaml

  • copy this code

version: 0.2
phases:
  pre_build:
    commands:
      - echo Logging in to Amazon ECR...
      - aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com
  build:
    commands:
      - echo Build started on `date`
      - echo Building the Docker image...          
      - docker build -t my-ecr:latest .
      - docker tag my-ecr:latest <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest      
  post_build:
    commands:
      - echo Build completed on `date`
      - echo Pushing the Docker image...
      - docker push <YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode

Image description

3. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

git add .

git status

git commit -m "Adding buildspec.yaml File"

git push
Enter fullscreen mode Exit fullscreen mode

Image description

  • The file is successfully pushed to the master branch, Confirm by checking the file in the Codecommit repository.

Image description

4. Provide the ECR permissions to the Codebuild role

  • On the IAM console, Roles and search for the role starting with codebuild- which was created with the codebuild project, codebuild-my-service-role, click codebuild-my-service-role, On the Permissions tab, Add permissions, Attach policies, Search and select AmazonEC2ContainerRegistryFullAccess, Attach policies

Image description

5. Start build project

  • CodeBuild under Developer Tools, click Build projects, my_codebuild_project, the Start build

  • Wait for 5-6 minutes to build the project.

Image description

  • Check the runtime logs in the Build logs tab.

Image description

  • On to the ECR repository to confirm the newly created image.

Image description

6. Create a imagedefinitions.json file

  • When we upload any code and push it to the master repository, it should automatically create a build and the build should be pushed to the ECR. From the ECR it should be pushed to the ECS container.
  • To do so, we need to create a CodePipeline.

  • Before creating CodePipeline, let us create imagedefinitions.json file.

  • SSH into the EC2 Instance and go to the repo folder

sudo su
cd /opt/docker/my-codecommit-repo
Enter fullscreen mode Exit fullscreen mode
  • Create the file with the name - imagedefinitions.json

  • vi imagedefinitions.json

[
    {
        "name": "CONTAINER-NAME",
        "imageUri": "ECR-REPO-URI:latest"
    }
]
Enter fullscreen mode Exit fullscreen mode
  • To get the Container name, Go to ECS, Task definition, my_task, Task definition: revision, my_task:2, Scroll down and find the container name, my_container

  • To get the ECS-Repo-URI, Go to ECR, Copy the URI for latest image

<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest
Enter fullscreen mode Exit fullscreen mode
  • change
[
    {
        "name": "my_container",
        "imageUri": "<YOUR ACCOUNT NUMBER>.dkr.ecr.us-east-1.amazonaws.com/my-ecr:latest"
    }
]
Enter fullscreen mode Exit fullscreen mode

7. Add, commit and push the file buildspec.yaml to the remote repository’s master branch

git add .
git commit -m "Adding imagedefinitions File"
git push
Enter fullscreen mode Exit fullscreen mode

Image description

What we have done so far

  • We have successfully created a CodeBuild Project, uploaded the code and pushed it to the master repository
codepipeline Article's
30 articles in total
Favicon
Streamlining CI/CD with AWS CodePipeline and GitHub Actions: A DevOps Perspective
Favicon
New Free CI/CD Platform Enhances Mobile App Deployment: A Comprehensive Solution for Developers
Favicon
AWS CodePipeline introduces the Commands action that enables customer to easily run shell commands as part of pipeline execution
Favicon
AWS CodePipeline V2 type pipelines supports to automatically retry a stage if there is a failure in the stage.
Favicon
Automate Application Deployment to AWS Elastic Beanstalk with Terraform and CodePipeline
Favicon
Say Goodbye to Extra CodeBuild Projects: AWS CodePipeline’s New Commands Action Explained
Favicon
AWS CodePipeline V2 type pipelines introduces pipeline variable check rule
Favicon
Deploying Flutter Web to S3 with CodeCommit, Codepipeline, Codebuild, and CodeDeploy
Favicon
Update Github token in Codepipeline with Cloudformation
Favicon
Setting up CI/CD in AWS with CodeCommit, CodeDeploy, CodePipeline, ECR, and ECS
Favicon
Creating a Continuous Delivery Pipeline in AWS (Hands-On)
Favicon
Deploy NodeJS REST API on ECS Fargate using AWS CodePipeline
Favicon
Automatizando infraestructura tecnolĂłgica con DevOps
Favicon
Review of Elastic beanstalk, CodeDeploy, CodePipeline, CodeBuild
Favicon
Despliega tu Asistente de IA Generativa en AWS
Favicon
UI Devs on AWS Should Start with Amplify
Favicon
Blue-Green deployment with GitLab CI and CodePipeline on an AWS ECS cluster
Favicon
Part 1: Pipeline fun with AWS
Favicon
Extending CloudFormation's Power: Creating Custom Resources for Enhanced AWS Resource Management
Favicon
ÂżQue es la cultura DevOps?
Favicon
Receive Slack Notification of CodePipeline with SNS and Lambda
Favicon
Deploy S3 hosted application using CodePipeline
Favicon
Continuous Cloning of CodeCommit Repo in Multiple Regions Using CodeBuild and CodePipeline
Favicon
Cross account deployments using a Customer Managed KMS key
Favicon
3-part series - (3) Create a CodePipeline pipeline and deploy the application on the instance
Favicon
3-part series - (1) Launch an EC2 Instance, Install CodeDeploy Agent and upload App_Linux.zip file to version enabled S3 bucket
Favicon
6-part series - (5) Create a CodeBuild Project, upload the code and push it to the master repository
Favicon
6-part series - (4) Create, clone a CodeCommit Repo, move-push the Dockerfile and index.html to remote repository master branch
Favicon
Sexiest way to manage your AWS resources
Favicon
Building applications with pipelines

Featured ones: