Day 51: Your CI/CD pipeline on AWS — Part 2
On our journey of making a CI/CD pipeline on AWS with these tools, we completed AWS CodeCommit. Now in this section we will be looking CodeBuild of AWS.
Next few days we’ll learn these tools/services:
- CodeDeploy
- CodePipeline
- S3
What is CodeBuild ?
AWS CodeBuild is a fully managed build service in the cloud. CodeBuild compiles your source code, runs unit tests, and produces artifacts that are ready to deploy. CodeBuild eliminates the need to provision, manage, and scale your own build servers.
Here’s how CodeBuild works:
- Build Projects: In CodeBuild, you define build projects that contain information about your build process, including the source code repository, build environment settings, build commands, and artifacts to be generated.
- Source Code Integration: CodeBuild integrates with various source code repositories like AWS CodeCommit, GitHub, Bitbucket, and more. When changes are pushed to your repository, CodeBuild can automatically trigger a build process.
- Build Environment: CodeBuild allows you to specify the build environment, which can be based on predefined Docker images provided by AWS or custom Docker images you create. This environment includes the programming language, runtime, dependencies, and other necessary components.
- Build Commands: You define the build commands in the build specification (usually a YAML file) that instruct CodeBuild on how to build and test your application. These commands can include steps like compiling code, running tests, and generating artifacts.
- Artifacts: After the build is successful, CodeBuild can generate artifacts such as executable files, libraries, documentation, or any other output files. These artifacts can be used for deployment or distribution.
- Integration with CI/CD Pipelines: CodeBuild is often used as a build step within a larger CI/CD pipeline orchestrated by AWS CodePipeline. In this setup, CodePipeline manages the entire application release process, including source code integration, building, testing, and deploying.
Key benefits of using AWS CodeBuild include:
- Scalability: CodeBuild can automatically scale to handle varying workloads, ensuring that builds are completed efficiently even during peak times.
- Managed Infrastructure: AWS handles the underlying infrastructure, including provisioning and scaling of resources, so you can focus on building and testing your applications.
- Customizable Build Environments: You can create custom Docker images to match your application’s specific dependencies and requirements.
- Integration with Other AWS Services: CodeBuild seamlessly integrates with other AWS services, enabling you to create comprehensive CI/CD pipelines.
- Pay-as-You-Go Pricing: CodeBuild follows a pay-as-you-go pricing model, which means you’re billed only for the resources you use during your build processes.
Tasks -
Task 1)
Step A) Read about Buildspec file for Codebuild.
The Buildspec file is a key component of AWS CodeBuild. It’s a YAML file that defines the build and deployment instructions for your CodeBuild project. The Buildspec file specifies how CodeBuild should perform tasks like compiling code, running tests, and generating artifacts. When you start a build in CodeBuild, it reads the instructions from the Buildspec file and executes the defined steps accordingly.
What is a Buildspec.yml file?
This is just a file that provides the build instructions for the codeBuild.
Where should you put it?
For it to work, it needs to be at the root of your project folder.
Here is an example
version: 0.2phases:
install:
runtime-versions:
nodejs: 16
commands:
- npm install -g typescript
- npm install
pre_build:
commands:
- echo Installing source NPM dependencies...
build:
commands:
- echo Build started on `date`
- tsc
- npm prune --production
post_build:
commands:
- echo Build completed on `date`
artifacts:
type: zip
files:
- package.json
- package-lock.json
- "build/**/*"
- .ebextensions/**/*
Here’s an overview of what you can include in a Buildspec file:
- Version: This indicates the version of the Buildspec syntax being used. For example:
version: 0.2
2. Phases: Phases define the various stages of the build process. Common phases include:
install
: Commands to install dependencies or set up the build environment.pre_build
: Commands to be run before the actual build process starts.build
: The main build commands, such as compiling code, running tests, and generating artifacts.post_build
: Commands to run after the build process is complete.
phases:
install:
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- echo "Build completed successfully"
3. Artifacts: In this section, you define the artifacts that should be generated by the build process. These can include compiled binaries, packaged code, or any other files you want to keep as outputs.
artifacts:
files:
- app/**/* # Include all files in the 'app' directory
discard-paths: yes # Discard the paths, only keep the files in the root of the artifact
4. Cache: The cache section allows you to specify files and directories to be cached between builds. This can significantly improve build times by reusing dependencies from previous builds.
cache:
paths:
- node_modules/
5. Environment Variables: You can define environment variables that are available during the build process. These can be used to store sensitive information or configuration values.
env:
variables:
API_KEY: "my-secret-key"
6. Secrets: This section allows you to reference secrets stored in AWS Secrets Manager and use them as environment variables in your build.
secrets:
- MY_API_KEY
Step B) create a simple index.html file in CodeCommit Repository
Create a repository in aws to commit your code.
After creating the repository. Create a vim.html file and push it to the Day51 repository
content of vim.html file
<DOCTYPE html>
<html>
<head>
<style>
body {
font-family: Arial, sans-serif;
background-color: #f2f2f2;
color: #333;
text-align: center;
}
h1 {
font-size: 36px;
margin-top: 50px;
color: #6130e8;
}
p {
font-size: 18px;
margin: 20px 0;
}
</style>
</head>
<body>
<h1>Welcome to my new page - Radheya Zujur </h1>
<p>Contribute to DevOps Community</p>
</body>
</html>
Now push this file to CodeCommit repo using following commands
git add vim.html
git status
git commit -m "added vim.html"
git push origin master
Verify whether the file is present in repository in aws
Task 2) Add buildspec.yaml file to CodeCommit Repository and complete the build process.
Let us create the buildspec.yaml file and push it to our repository.
buildspec.yaml
Content of yaml will be
version: 0.2
phases:
install:
commands:
- echo Installing NGINX
- sudo apt-get update
- sudo apt-get install nginx -y
build:
commands:
- echo Build started on 'date'
- cp index.html /var/www/html/
post_build:
commands:
- echo Configuring NGINX
artifacts:
files:
- /var/www/html/index.html
Now commit this using following commands
git add .
git commit -m "added buildspec.yaml"
git push origin master
In the left navigation panel > Go to Build: CodeBuild > Build Projects > Click on Create Build Projects.
Give detials
Click on Create project
Click on Start Build. Wait until the build succeeds.
Yes, Our build is successfull.
We add these artifacts to the project and store them in an S3 bucket.
First, let us create an S3 Bucket.
After the bucket is created.
In the CodeBuild > Build console > Click on Edit > Artifacts.
Select the Amazon S3 for Artifact Type and select the bucket you just created.
And click on Update Artifacts.
Click on Build again.
Once the build is successful, you can see that the artifacts are uploaded to the S3 bucket.
Navigate through the index.html file in the CodeBuild:
Click on open and your page will be opened
Yayy, We’ve deployed the build here.