Implementing Continuous Integration (CI) with AWS CodeBuild and Docker
Objective
The objective of the "Implementing Continuous Integration with AWS CodeBuild and Docker" is to establish a streamlined and automated process for building, testing, and validating software applications whenever changes are made to the source code. This is achieved by integrating AWS CodeBuild with Docker.
Step 1: Set Up Your AWS Environment
Login to AWS console & go into ECR and create a registry/repository there.
Notice the push commands for the repo you created, and we will be using them in our buildspec.yaml file, replace your values for REPOSITORY_URI & aws ecr get-login-password
Create an AWS CodeBuild project in the AWS Management Console.
Specify your source code repository and branch like GitHub.
Choose the build environment (e.g., Ubuntu).
Step 2: Create a Build Specification File & DockerFile
Create a
buildspec.yml
file in your source code repository.Define build phases such as
install
,build
, andpost_build
.Specify commands to build and test your Dockerized application
version: 0.2
phases:
pre_build:
commands:
- echo Logging in to Amazon ECR...
- aws --version
- aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 780621109903.dkr.ecr.us-east-1.amazonaws.com
- REPOSITORY_URI=780621109903.dkr.ecr.us-east-1.amazonaws.com/node-app
- 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 Build completed on `date`
- echo Pushing the Docker images...
- docker push $REPOSITORY_URI:latest
- docker push $REPOSITORY_URI:$IMAGE_TAG
- echo Writing image definitions file...
- printf '[{"name":"nodeapp","imageUri":"%s"}]' $REPOSITORY_URI:$IMAGE_TAG > imagedefinitions.json
- cat imagedefinitions.json
artifacts:
files: imagedefinitions.json
# Use an official Node.js runtime as a parent image
FROM node:14
# Set the working directory inside the container
WORKDIR /usr/src/app
# Copy package.json and package-lock.json to the container
COPY package*.json ./
# Install app dependencies
RUN npm install
# Copy the rest of your application source code to the container
COPY . .
# Expose a port for your Node.js application
EXPOSE 8080
# Define the command to run your Node.js application
CMD [ "node", "app.js" ]
Step 3: Configure Your Source Code Repository
Set up webhooks or triggers in your source code repository (e.g., GitHub webhook) to notify AWS CodeBuild when code changes are pushed.
Provide the webhook URL or trigger details in your source code repository settings.
- Note the Role name from this page.
Go to IAM user and give permissions to the role name that you noted above. AmazonEC2ContainerRegistryFullAccess & AmazonEC2ContainerRegistryPowerUser
Step 4: Build and Test Your Application
Whenever changes are pushed to your source code repository, AWS CodeBuild will automatically trigger a build using thebuildspec.yml
file. AWS CodeBuild will create a Docker build environment based on the specified image and execute the build commands. Your application will be built and tested within the Docker environment.