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

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

ยท

3 min read

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-01 :

  • Read about Buildspec file for Codebuild.

  • create a simple index.html file in CodeCommit Repository

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

Step 1: Read about Buildspec file for CodeBuild

The buildspec.yml file is used in AWS CodeBuild to define the build process. It contains a series of build commands and settings that CodeBuild uses to run your build. Here is a simple example of a buildspec.yml file:

version: 0.2

phases:
  build:
    commands:
      - echo "Build phase"
      - npm install
      - npm run build

artifacts:
  files: 'index.html'

This is a basic example for a Node.js project, and it may vary based on your project requirements.

Step 2: Create a simple index.html file in CodeCommit Repository

Create a new file named index.html with some simple content. For example:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>My Nginx Website</title>
</head>
<body>
    <h1>Hello, this is a simple Nginx website!</h1>
</body>
</html>

This creates a simple HTML file with a "Hello, this is a simple Nginx website!" message.

  • Navigate to the CodeCommit service.

  • Create and select the repository where you want to add the index.html file.

  • Click on "Commit Changes" to save the changes.

  • Clone the repository to your instance and verify the files.

    Task-02 :

    • Add buildspec.yaml file to CodeCommit Repository and complete the build process.

      Now that we've successfully built our HTML page, it's time to integrate this process into our CodeCommit repository.

      Step 1: Add buildspec.yaml file to CodeCommit Repository

      • To make the build process automatic, we need to commit the buildspec.yaml file to our CodeCommit repository.

      • Push this file to our repository.

            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
        
        
            $ git add .
            $ git commit -m "Commit Message"
            $ git push <branch-name>
        

Step 2: Complete the Build Process

Once the buildspec.yaml file is in your repository, AWS CodeBuild will automatically pick it up when you push changes to your repository.

It will execute the build process as defined and generate the index.html file.

  • Create a CodeBuild project in AWS Console

    • Select the source provider, repository, and branch.

    • Select your environment and operating system & create the codeBuild Project

    • Now, start the build by clicking on Start Build & wait to complete it

    • Store the build details in the artifacts

      Before that, create an S3 bucket. Navigate to the S3 bucket in the AWS console and provide the bucket name and create the bucket.

    • Click on edit and select artifact. Provide all the details and create an artifact.

    • Now if you rebuild the project you can see the UPLOAD_ARTIFACTS section.

    • Navigate to the S3 bucket and you can see the build.

    • Click on the Open URL to open the nginx & see the output of the index.html file.

    • You've successfully set up AWS CodeBuild to automate the build process of your HTML page.

ย