Table of contents
What is Code Build ?
๐๏ธ CodeBuild is like having a construction crew ๐ง for your software projects. It's an AWS service that helps you build and test your code automatically.
๐ค You simply provide your source code ๐ฆ, and CodeBuild takes care of compiling it, running tests ๐งช, and producing deployable artifacts ๐.
๐ It's all about automation, saving you time and effort by eliminating manual steps in the build process. Plus, it's scalable, so whether you're working on a small project or a big one, Code Build can handle it.
Task-01 :
- Read about Buildspec file for Codebuild.
The buildspec
file is a key component used with AWS CodeBuild. It's essentially a configuration file that tells CodeBuild what to do when it runs a build. Here's a breakdown:
Purpose: The
buildspec
file outlines the build phases, commands, and actions CodeBuild should take when building your project.Format: It's usually written in YAML format, making it human-readable and easy to understand.
Phases: The
buildspec
file typically includes one or more phases, such as install, pre-build, build, post-build, and finally, artifacts. Each phase can contain a series of commands that CodeBuild will execute sequentially.Commands: These are the actual instructions for CodeBuild to execute during each phase. Commands can include tasks like installing dependencies, compiling code, running tests, and packaging artifacts.
Environment Variables: You can also define environment variables in the
buildspec
file, which CodeBuild will use during the build process. This allows for customization and flexibility based on your project's needs.Artifacts: Finally, the
buildspec
file specifies what artifacts should be generated by the build process and where they should be stored. Artifacts can include compiled binaries, packaged applications, or any other files generated during the build.
- create a simple index.html file in CodeCommit Repository
To create a simple index.html
file in your CodeCommit repository, you can follow these steps:
Access CodeCommit: Go to the AWS Management Console and navigate to the CodeCommit service.
Create a Repository: If you haven't already, create a new repository or select an existing one where you want to add the
index.html
file.Clone Repository: Clone the repository to your local machine using Git. You can find the clone URL on the CodeCommit console.
Create
index.html
: Using a text editor or IDE, create a new file namedindex.html
in the cloned repository directory.Add HTML Content: Write your HTML content inside the
index.html
file. For example:
htmlCopy code<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>My Website</title>
</head>
<body>
<h1>Hello, world!</h1>
<p>This is a simple HTML file.</p>
</body>
</html>
Save Changes: Save the
index.html
file.Commit Changes: Stage the
index.html
file and commit your changes locally.
bashCopy codegit add index.html
git commit -m "Add index.html file"
- Push Changes: Push the committed changes to the CodeCommit repository.
bashCopy codegit push origin master
This will push the index.html
file to your CodeCommit repository, making it available for use. You can then integrate this repository with AWS services like CodeBuild for automated builds, or with AWS Amplify for hosting your website.
- you have to build the index.html using nginx server
To serve the index.html
file using an NGINX server, you'll need to follow these steps:
- Create Dockerfile: Create a
Dockerfile
in the root of your repository. This file will contain instructions for building a Docker image that runs NGINX and serves theindex.html
file.
DockerfileCopy code# Use NGINX base image
FROM nginx
# Copy index.html into the NGINX html directory
COPY index.html /usr/share/nginx/html/
# Expose port 80
EXPOSE 80
- Build Docker Image: Build the Docker image using the Dockerfile.
bashCopy codedocker build -t my-nginx-image .
- Run Docker Container: Run a Docker container based on the built image.
bashCopy codedocker run -d -p 8080:80 my-nginx-image
This command will start a Docker container running NGINX, serving the index.html
file on port 8080 of your local machine. You can then access the file by opening a web browser and navigating to http://localhost:8080
.
These steps assume you have Docker installed and running on your machine. Additionally, ensure that your index.html
file is in the root of your repository and contains the desired content you want to serve.
Task-02 :
- Add buildspec.yaml file to CodeCommit Repository and complete the build process.
To complete the build process in CodeCommit using a buildspec.yaml
file, follow these steps:
- Create
buildspec.yaml
: Create abuildspec.yaml
file in the root of your CodeCommit repository. This file will define the build process for CodeBuild.
yamlCopy codeversion: 0.2
phases:
install:
runtime-versions:
docker: 18
pre_build:
commands:
- echo "Installing dependencies..."
build:
commands:
- echo "Building NGINX Docker image..."
- docker build -t my-nginx-image .
post_build:
commands:
- echo "Build completed successfully!"
artifacts:
files:
- Dockerrun.aws.json
- Commit Changes: Stage and commit the
buildspec.yaml
file to your CodeCommit repository.
bashCopy codegit add buildspec.yaml
git commit -m "Add buildspec.yaml file"
git push origin master
Set up CodeBuild: Go to the AWS Management Console and navigate to the CodeBuild service.
Create a Build Project: Click on "Create build project" and configure the project settings. Choose your CodeCommit repository as the source, and select the
buildspec.yaml
file as the build specification.Start a Build: Trigger a manual build in CodeBuild to initiate the build process. You can also set up triggers to automatically start builds whenever changes are pushed to the repository.
Monitor Build Progress: Monitor the build progress in the CodeBuild console. Once the build is complete, you should see the message "Build completed successfully!" indicating that the NGINX Docker image was built successfully.
Verify: You can verify the build by checking if the Docker image was created successfully and if it serves the
index.html
file as expected when running a Docker container based on the built image.
By following these steps, you've configured CodeBuild to build the NGINX Docker image using the buildspec.yaml
file in your CodeCommit repository.