CI/CD pipeline on AWS (Part 2) - CodeBuild
In this project, we will continue our journey with AWS CodeCommit and explore the process of adding a buildspec.yaml file to the repository and completing the build process. We will create a buildspec.yaml file that uses an Nginx server to build our files and push them to the repository. We will also configure CodeBuild to automate the build process and store the artifacts in an S3 bucket. By following these steps, you will gain hands-on experience with building and deploying applications using CodeCommit and CodeBuild in AWS.
Table of Contents:
Add buildspec.yaml file to CodeCommit Repository
1.1 Creating the buildspec.yaml file
1.2 Pushing the buildspec.yaml file to the CodeCommit repository
1.3 Verifying the file in the remote repositorySet up a CodeBuild project
2.1 Creating a CodeBuild project
2.2 Configuring the build environment and source
2.3 Specifying artifacts and starting the buildStoring artifacts in an S3 bucket
3.1 Creating an S3 bucket
3.2 Updating CodeBuild artifacts to use the S3 bucket
3.3 Starting a new build and verifying artifact upload
AWS 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.
It provides prepackaged build environments for popular programming languages and build tools such as Apache Maven, Gradle, and more.
You can also customize build environments in CodeBuild to use your own build tools. CodeBuild scales automatically to meet peak build requests.
How CodeBuild Works?
1. As input, you must provide CodeBuild with a build project.
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.
2. 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:
3. CodeBuild uses the build project to create the build environment.
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.
4. 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.
5. While the build is running, the build environment sends information to CodeBuild and Amazon CloudWatch Logs.
6. 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.
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 3: Add buildspec.yaml file to CodeCommit Repository and complete the build process.
Let us create the buildspec.yaml to build the file using an Nginx server and push it 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
Here's what each step of the build does:
version: 0.2 specifies the version of the Buildspec syntax we're using.
phases: contains the build phases for our project.
install: Installs nginx on the build environment using the apt-get package manager.
build: Copies the index.html file to the default web root directory for nginx.
post_build: Performs any additional configuration for nginx, if necessary.
artifacts: Specifies the location of the index.html file to be included in the build artifact.
Make sure you have the indentations correct for your buildspec.yml file.
push the committed file to the CodeCommit repository.
Verify the file in the remote repository along with the content.
Now In the left navigation panel > Go to Build: CodeBuild > Build Projects > Click on Create Build Projects.
Enter the project name and description.
In the source section, select source provider AWS CodeCommit, select Repository that you created earlier and select branch master.
In the Environment section, choose the operating system, runtime and image.
And let others be the default, click on the "Create build project" to create a project.
Build Project is created. Click the "Start build" button to start a new build.
Click on Start Build. Wait until the build succeeds.
To add artifacts to a CodeBuild project and store them in an S3 bucket click on 'edit' and select 'Artifacts'.
**
First, let us create an S3 Bucket.**
In the CodeBuild > Build console > Click on Edit > Artifacts.
In Artifacts, select type Amazon S3 and select the bucket name that we created in the above step and Click on 'Update artifacts'.
After updating artifacts, click the "Start build" button again to start a new build. It succeeded.
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 we can see the web app.
In this project, we successfully added a buildspec.yaml file to our CodeCommit repository, configured a CodeBuild project to automate the build process, and stored the artifacts in an S3 bucket. This allowed us to build our application using an Nginx server and securely store the build artifacts. AWS CodeCommit and CodeBuild provide powerful tools for version control and continuous integration/continuous deployment (CI/CD) workflows. By leveraging these services, developers can streamline their development processes and ensure efficient and reliable delivery of their applications.
Thank you for reading this blog. If you found this blog helpful, please like, share, and follow me for more blog posts like this in the future.
— Happy Learning !!!