CI/CD Pipeline On AWS (Part - 2) CodeBuild
#90 Days of DevOps Challenge - Day 51
What is CodeBuild?
AWS CodeBuild is a fully managed continuous integration and continuous delivery (CI/CD) service provided by Amazon Web Services (AWS). It is designed to compile source code, run tests, and produce software packages that are ready for deployment. CodeBuild eliminates the need to set up and manage build servers, allowing developers to focus on writing code.
key features and aspects of AWS CodeBuild:
Build Environment:- CodeBuild provides a customizable build environment where developers can define their build specifications. It supports various programming languages, build tools, and operating systems, allowing for flexibility in building different types of applications.
Continuous Integration and Continuous Delivery:- CodeBuild integrates with other AWS services, such as AWS CodeCommit, AWS CodePipeline, and AWS CodeDeploy, enabling seamless integration into CI/CD workflows. It can be used to trigger builds automatically whenever changes are pushed to a version control repository.
Build Configurations:- CodeBuild uses build specifications defined in the form of a buildspec.yml file. The buildspec.yml file contains instructions on how to build, test, and package the application. It can specify commands, dependencies, environment variables, and other build settings.
Scalability and Parallel Execution:- CodeBuild can scale automatically to handle build workloads of any size. It can run builds in parallel, allowing multiple builds to execute simultaneously, which helps to reduce build times and improve overall efficiency.
Build Artifacts and Logs:-CodeBuild generates build artifacts, such as compiled binaries or deployment packages, that can be used for further deployment or testing. It also provides detailed build logs, including information about build phases, commands executed, and any errors or warnings encountered during the build process.
Integration with Third-Party Tools:-CodeBuild supports integration with popular build and testing tools, such as Jenkins, Gradle, Maven, and others. This allows developers to leverage their existing tooling and workflows while benefiting from the scalability and managed infrastructure provided by CodeBuild.
By using AWS CodeBuild, developers can automate their build processes, improve code quality through continuous integration, and accelerate software delivery. It simplifies the setup and management of build environments, provides scalability, and integrates seamlessly with other AWS services, making it an efficient choice for CI/CD workflows in DevOps environments.
Buildspec file for Codebuild
A buildspec file is a YAML file used in AWS CodeBuild to define the build steps and settings for a project. It provides instructions on how to build, test, and package your application during the build process.
version: 0.2
phases:
install:
runtime-versions:
nodejs: 12
commands:
- npm install
build:
commands:
- npm run build
post_build:
commands:
- echo "Build completed on `date`"
artifacts:
files:
- '**/*'
discard-paths: yes
base-directory: 'dist'
In this example, the build spec file consists of the following sections:
version
: Specifies the version of the build spec file format. The current version is 0.2.phases
: Defines the different phases of the build process.install
: Specifies the installation phase, where you can define the runtime versions and any necessary setup commands. In this example, it installs Node.js version 12 and runsnpm install
to install project dependencies.build
: Specifies the build phase, where you can define commands to build your application. In this example, it runsnpm run build
to execute the build process.post_build
: Specifies the post-build phase, where you can define any additional commands or actions to perform after the build. In this example, it echoes a message indicating the build completion.
artifacts
: Defines the artifacts generated by the build process.files
: Specifies the files to include in the build artifact. In this example, it includes all files and directories under thedist
directory.discard-paths
: Indicates whether to discard the paths of the included files in the artifact. In this example, it is set toyes
, which means the files will be stored in the artifact without the base directory structure.base-directory
: Specifies the base directory for the artifact files. In this example, it sets thedist
directory as the base directory.
This is a basic example of a buildspec file. You can customize it based on your specific build requirements, such as adding additional build phases, running tests, deploying artifacts, or integrating with other tools or services.
Task-01 :
Read about the Buildspec file for Codebuild.
Create a simple index.html file in CodeCommit Repository
You have to build the index.html using the nginx server
Step 1:- Now we need to create a simple index.html file in CodeCommit Repository
We have already created Repo in code commit same repo we will be using
Now we need to go inside the repo and we need to create an index.html file
Now we need to add the file and commit it and push to CodeCommit repo
Now if we see in the code commit repo we can see the index.html file
Task-02 :
Add
buildspec.yaml
file to CodeCommit Repository and complete the build process.
Step 1:- We need to create a Buildspec file to build the file using an Nginx server.
vi 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
Step 2:- Now we need to add the file and commit it and push to CodeCommit repo
Step 3:- Now if we see in the code commit repo we can see buildspec.yaml file
Step 4:- Now we need to create build project so need to go to CodeBuild-> Build projects ->Create build project
Need to give project name
In the source section, select the source provider as AWS CodeCommit, select the repository that you created earlier and select branch master.
In the Environment section, choose the operating system, runtime and image. Here I'm choosing Ubuntu OS and the environment will be Linux.
Now we need to click on create a new service role and Under the "Buildspec" section, click on "Use a buildspec file".
Now we need to click on create build project and once the build project is successfully created we can start build now
once build now started We can see the phase deatils
To add artifacts to a CodeBuild project and store them in an S3 bucket.
Step 1:- First, We need to create an S3 bucket and provide the public access
We need to click on Edit-> Artifacts
Now In Artifacts, We need to select S3 bucket which we have created
Now click on update artifacts
After clicking on update artifacts need to click on start build
After the build process is complete, the artifacts will be uploaded to the specified S3 bucket location.
Click on 'index.html' file, below you can see the properties of the file.
Click on 'open' on the right-hand side.
Now if we browse it we can see the results
Thank you for reading!! I hope you find this article helpful!!
if any queries or corrections to be done to this blog please let me know.
Happy Learning!!
Saikat Mukherjee