Hello everyone!
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 at 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.
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
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
- 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/
- 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 named Day37 and push a html file named vim.html
<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 - Pranjal Jain </h1>
<p>Contribute to DevOps Community</p>
</body>
</html>
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
The 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 the following commands
git add .
git commit -m "added buildspec.yaml"
git push origin main
In the left navigation panel > Go to Build: CodeBuild > Build Projects > Click on Create Build Projects.
Click on Create project
Click on Start Build. Wait until the build succeeds.
Yes, Our build is successful.
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.
Click on Update Artifacts.
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
Voila!, We’ve deployed the build here.