Day 51: CI/CD pipeline on AWS - Part 2 🚀 ☁

Day 51: CI/CD pipeline on AWS - Part 2 🚀 ☁

What is CodeBuild ?

  • AWS CodeBuild is a fully managed continuous integration service that compiles source code, runs tests, and produces software packages that are ready to deploy. You don’t need to provision, manage, and scale your own build servers. CodeBuild can use either GitHub, GitHub Enterprise, BitBucket, AWS CodeCommit, or Amazon S3 as a source provider.

    CodeBuild scales continuously and can process multiple builds concurrently. CodeBuild offers various pre-configured environments for various versions of Microsoft Windows and Linux. Customers can also bring their customized build environments as Docker containers. CodeBuild also integrates with open-source tools such as Jenkins and Spinnaker.

    CodeBuild can also create reports for unit, functional, or integration tests. These reports provide a visual view of how many tests cases were run and how many passed or failed. The build process can also be run inside an Amazon Virtual Private Cloud (Amazon VPC) which can be helpful if your integration services or databases are deployed inside a VPC.

How CodeBuild Works?

  1. As input, you must provide CodeBuild with a build project.

  2. A build project includes information about how to run a build, including where to get the source code, which build environment to use, which build commands to run, and where to store the build output.

  3. A build environment represents a combination of operating systems, programming language runtime, and tools that CodeBuild uses to run a build. For more information, see:

  1. CodeBuild uses the build project to create the build environment.

  2. CodeBuild downloads the source code into the build environment and then uses the build specification (buildspec), as defined in the build project or included directly in the source code. A buildspec is a collection of build commands and related settings, in YAML format, that CodeBuild uses to run a build. For more information, see the Buildspec reference.

  3. If there is any build output, the build environment uploads its output to an S3 bucket. The build environment can also perform tasks that you specify in the buildspec. For an example, see Build notifications sample.

  4. While the build is running, the build environment sends information to CodeBuild and Amazon CloudWatch Logs.

  5. While the build is running, you can use the AWS CodeBuild console, AWS CLI, or AWS SDKs to get summarized build information from CodeBuild and detailed build information from Amazon CloudWatch Logs. If you use AWS CodePipeline to run builds, you can get limited build information from CodePipeline.

Buildspec file

A buildspec file is a YAML file used in AWS CodeBuild to define the build and deployment stages of your project.

It provides instructions on how CodeBuild should build, test, and deploy your application.

Buildspec file name and storage location

In AWS CodeBuild, the buildspec file should be named buildspec.yml or buildspec.yaml. It should be placed in the root directory of your source code repository.

You can use the buildspecOverride parameter to specify the file name and location of your buildspec.

You can specify only one buildspec for a build project, regardless of the buildspec file’s name.

Buildspec syntax

The syntax used in a buildspec file is:

COPY

COPY

version: 0.3

phases:
  pre_build:
    commands:
      - echo "Installing dependencies..."
      - npm install

  build:
    commands:
      - echo "Building the application..."
      - npm run build

  post_build:
    commands:
      - echo "Running tests..."
      - npm test

artifacts:
  files:
    - index.html
    - dist/**
  discard-paths: yes

version

Required mapping. Represents the buildspec version. We recommend that you use 0.2.

run-as

Optional sequence. Available to Linux users only. Specifies a Linux user that runs commands in this buildspec file. run-as grants the specified user read and run permissions. When you specify run-as at the top of the buildspec file, it applies globally to all commands.

env

Optional sequence. Represents information for one or more custom environment variables.

  • env/shell: Optional sequence. Specifies the supported shell for Linux or Windows operating systems.

  • env/variables: Required if env is specified, and you want to define custom environment variables in plain text.

  • env/parameter-store: Required if env is specified, and you want to retrieve custom environment variables stored in Amazon EC2 Systems Manager Parameter Store.

  • env/secrets-manager: Required if you want to retrieve custom environment variables stored in AWS Secrets Manager.

  • env/exported-variables: Used to list environment variables you want to export.

  • env/git-credential-helper: Used to indicate if CodeBuild uses its Git credential helper to provide Git credentials.

proxy

Used to represent settings if you run your build in an explicit proxy server. Optional setting.

phases

Required sequence. Represents the commands CodeBuild runs during each phase of the build.

artifacts

Optional sequence. Represents information about where CodeBuild can find the build output and how CodeBuild prepares it for uploading to the S3 output bucket.

Task-01 :

  • Read about the Buildspec file for Codebuild.

  • create a simple index.html file in the CodeCommit Repository

  • you have to build the index.html using nginx server

I am using the repository that was used.

Clone the repository using git clone.

Create an index.html file.

vim index.html

The contents of index.html would be:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Akash Singh - DevOps Engineer</title>
    <link rel="stylesheet" href="styles.css"> <!-- Link to your CSS file -->
</head>
<body>
    <header style="background-color: #333; color: #f0f0f0;">
        <h1>Akash Singh</h1>
        <p>DevOps Engineer</p>
    </header>

    <section id="about" style="background-color: #f0f0f0; color: #333;">
        <h2>About Me</h2>
        <p>Hello, I'm Akash Singh, a passionate DevOps engineer with expertise in automating, optimizing, and streamlining development and deployment processes. I am dedicated to enhancing collaboration between development and IT operations to deliver high-quality software efficiently.</p>
    </section>

    <section id="skills" style="background-color: #333; color: #f0f0f0;">
        <h2>Skills</h2>
        <ul>
            <li>Continuous Integration/Continuous Deployment (CI/CD)</li>
            <li>Containerization (Docker, Kubernetes)</li>
            <li>Infrastructure as Code (Terraform, Ansible)</li>
            <li>Scripting and Automation (Bash, Python)</li>
            <li>Version Control (Git)</li>
        </ul>
    </section>

    <section id="projects" style="background-color: #f0f0f0; color: #333;">
        <h2>Projects</h2>
        <div class="project">
            <h3>Project 1 Title</h3>
            <p>Description of Project 1. This project involved implementing a CI/CD pipeline for an e-commerce application, resulting in faster and more reliable deployments.</p>
        </div>

        <div class="project">
            <h3>Project 2 Title</h3>
            <p>Description of Project 2. This project focused on containerizing legacy applications and managing them with Kubernetes for scalability and resilience.</p>
        </div>
        <!-- Add more projects as needed -->
    </section>

    <section id="contact" style="background-color: #333; color: #f0f0f0;">
        <h2>Contact Me</h2>
        <p>If you'd like to contact me, please send an email to <a href="mailto:akash@example.com">akash@example.com</a>.</p>
    </section>

    <footer style="background-color: #f0f0f0; color: #333;">
        <p>&copy; 2023 Akash Singh</p>
    </footer>

    <script src="script.js"></script> <!-- Link to your JavaScript file -->
</body>
</html>

Add these changes and commit to the repository.

git add. 
git status
git commit -m "this is my index file"

Let us push these changes to the CodeCommit repository.

git push origin master

Verify the same in the CodeCommit.

Now Let us create the buildspec.yaml file and push it to our repository.

vim buildspec.yml
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 push to Rrepository

git push origin master

And now verify in the repository

In the left navigation panel > Go to Build: CodeBuild > Build Projects > Click on Create Build Projects

Project name: codebuild

In the source section, Select AWS CodeCommit as Source Provider and select the repository, and branch your code is hosted.

In the Environment section, Select OS as Ubuntu.

And create a New Service Role.

And let others be the default, click on Create build project.

Click on Start Build. Wait until the build succeeds.

We add these artifacts to the project and store them in an S3 bucket.

First, let us create an S3 Bucket.

Now In the CodeBuild > Build console > Click on Edit > 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:

Use the Open tab to reach the server.

In this blog, I have created a repository in CodeCommit, cloned it to the local, and pushed the files from local to the CodeCommit. Then using the CodeBuild, build the application using the Nginx server and uploaded the artifacts to S3. If you have any questions or want to share your experiences, please comment below. Don’t forget to read my blogs and connect with me on LinkedIn and let’s have a conversation.

To help me improve my blog and correct my mistakes, I am available on LinkedIn as Akash Singh. Do reach me, and I am open to suggestions and corrections.

#Day51 #90daysofdevops #happylearning #AWS

Did you find this article valuable?

Support Akash-DevOps by becoming a sponsor. Any amount is appreciated!