Day 50: Your CI/CD pipeline on AWS - Part 2 ๐Ÿš€ โ˜

Rahul sharmaRahul sharma
5 min read

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:

  1. Purpose: The buildspec file outlines the build phases, commands, and actions CodeBuild should take when building your project.

  2. Format: It's usually written in YAML format, making it human-readable and easy to understand.

  3. 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.

  4. 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.

  5. 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.

  6. 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:

  1. Access CodeCommit: Go to the AWS Management Console and navigate to the CodeCommit service.

  2. 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.

  3. Clone Repository: Clone the repository to your local machine using Git. You can find the clone URL on the CodeCommit console.

  4. Create index.html: Using a text editor or IDE, create a new file named index.html in the cloned repository directory.

  5. 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>
  1. Save Changes: Save the index.html file.

  2. Commit Changes: Stage the index.html file and commit your changes locally.

bashCopy codegit add index.html
git commit -m "Add index.html file"
  1. 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:

  1. 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 the index.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
  1. Build Docker Image: Build the Docker image using the Dockerfile.
bashCopy codedocker build -t my-nginx-image .
  1. 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:

  1. Create buildspec.yaml: Create a buildspec.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
  1. 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
  1. Set up CodeBuild: Go to the AWS Management Console and navigate to the CodeBuild service.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

0
Subscribe to my newsletter

Read articles from Rahul sharma directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Rahul sharma
Rahul sharma