Table of contents
- Introduction
- TECHNICAL OVERVIEW
- Features and Advantages 0f CI/CD Pipeline:
- Build CI/CD Pipeline on AWS with Code Commit | Code Build | Code Deploy
- What is CodeCommit?
- What is CodeBuild ?
- Flow of Execution
- →Let’s Create a CodeCommit:
- Enter your credentials:
- →Now Let’s create a CodeBuild:
- What is CodeDeploy?
- Flow of execution
- Step by Step implementation:
- This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.
- Run the command “bash install.sh” to run your bash file which is created in the previous step.
- 11. Add appspec.yaml file to CodeCommit Repository and complete the deployment process.
- Add Script for installing & starting NGINX Webserver:
- 21. Browse the instance public IP address, it will show the output of the index.html file.
- Hooray! We have successfully created a CI/CD Pipeline leveraging AWS services.
- CONCLUSION & DISCUSSIONS
- LIMITATION
- FUTURE ENHANCEMENT
- CONCLUSION
- The continuous Integration/Continuous Delivery (CI/CD) pipeline is a crucial part of modern software development practices. It helps automate the software delivery process, making it faster, more efficient, and less error-prone. The ultimate goal of a CI/CD pipeline is to ensure that software changes are tested, integrated, and delivered to end users in a timely and reliable manner.
Introduction
CI/CD stands for Continuous Integration/Continuous Delivery or Continuous Deployment. It is a set of practices and tools that enable developers to deliver code changes more frequently and reliably. In this article, we will discuss the basics of the CI/CD pipeline, its benefits, and how it works.
What is CI/CD pipeline?
CI/CD pipeline is a process of automating the build, test, and deployment of software applications. It starts with Continuous Integration (CI) which involves automating the process of building and testing code changes whenever developers push code to a central repository. The goal of CI is to detect problems early in the development cycle, so they can be fixed before they become too costly or time-consuming.
Continuous Delivery (CD) is the next step in the process, which involves automating the deployment of code changes to production. The goal of CD is to enable teams to release software more frequently and reliably by automating the deployment process. Continuous Deployment (CD) is an extension of Continuous Delivery where every change that passes through the pipeline is automatically deployed to production.
CI/CD pipeline Benefits:
CI/CD pipeline has several benefits. Firstly, it helps to catch errors early in the development process, reducing the overall cost of development. By automating the build and test process, teams can detect problems quickly and fix them before they become too costly or time-consuming.
Secondly, it enables teams to release software more frequently and reliably. By automating the deployment process, teams can deploy changes quickly and with fewer errors. This leads to faster release cycles and better customer satisfaction.
Thirdly, it improves collaboration and communication between teams. By automating the build and test process, teams can work together more effectively, and ensure that everyone is working towards the same goal. This leads to better code quality, faster delivery times, and improved productivity.
How does CI/CD pipeline work?
CI/CD pipeline typically involves the following stages:
Code changes - Developers make changes to the code and push it to the central repository.
Code Build - The build process is triggered automatically when changes are pushed to the repository. This process involves compiling the code, running unit tests, and creating a deployable artifact.
Automated Testing - The code is then tested automatically using a variety of tests such as unit, integration, functional, and performance tests. This helps to identify any bugs or issues that need to be addressed.
Deployment - If the code passes all the tests, it is deployed automatically to a staging environment. This allows for further testing in an environment that closely mirrors the production environment.
User Acceptance Testing - The code is tested by the users to ensure that it meets their requirements and expectations.
Production - If the code passes all the tests and is approved by the users, it is deployed to the production environment. This enables users to access the new features and functionality.
Monitoring - The system is continually monitored for issues and errors, and if any are detected, the pipeline is triggered again to fix the issues.
TECHNICAL OVERVIEW
A CI/CD pipeline is a set of automated processes and tools that enable the development, testing, and deployment of code changes. A typical CI/CD pipeline consists of the following stages:
Code Changes: The first stage of a CI/CD pipeline is to create code changes. Developers create code changes, make updates, or fix bugs.
Version Control: The second stage of a CI/CD pipeline is to commit the code changes to a version control system (VCS), such as Git. This ensures that all code changes are tracked and can be rolled back if necessary.
Continuous Integration (CI): The third stage of a CI/CD pipeline is the process of automatically building, testing, and validating code changes whenever code is committed to the VCS. The CI process can be configured to run various types of tests, including unit tests, integration tests, and functional tests, to verify that the code changes are working as intended.
Artifact Generation: Once the code changes have passed the CI stage, the next step is to generate an artifact. The artifact is a deployable package that contains the code changes and any necessary dependencies.
Continuous Delivery (CD): The CD stage of a CI/CD pipeline involves automatically deploying the artifact to a staging environment. This environment is typically a replica of the production environment, where the code changes can be tested and verified before being released to production.
User Acceptance Testing (UAT): In the UAT stage, the code changes are tested by the intended users. This stage can be done manually or automatically, depending on the organization's requirements.
Continuous Deployment (CD): The final stage of a CI/CD pipeline is the deployment of the code changes to the production environment. In this stage, the changes are pushed live and made available to users.
In addition to the above stages, a CI/CD pipeline can also include other processes, such as code analysis, security scanning, and performance testing. These additional processes can help ensure that the code changes are high-quality, secure, and performant.
CI/CD pipeline tools: There are many tools available for building CI/CD pipelines. Some of the popular tools include:
Jenkins: An open-source automation server that supports building, testing, and deploying software.
GitLab CI/CD: A complete DevOps platform that supports building, testing, and deploying software.
Travis CI: A cloud-based continuous integration and deployment platform.
CircleCI: A cloud-based continuous integration and deployment platform that supports multiple programming languages.
AWS CodePipeline: A cloud-based CI/CD service that supports building, testing, and deploying software on AWS.
Features and Advantages 0f CI/CD Pipeline:
Faster Time to Market: One of the primary benefits of CI/CD pipelines is the ability to release new features and bug fixes faster. By automating the build, testing, and deployment process, developers can release updates more frequently, reducing the time it takes to get new features into the hands of users.
Improved Quality: CI/CD pipelines ensure that code changes are thoroughly tested and validated before they are released into production. This reduces the likelihood of bugs and other issues that could affect the user experience or cause downtime.
Increased Collaboration: By automating the build and testing process, CI/CD pipelines help to facilitate collaboration between developers, testers, and other stakeholders. This allows for faster feedback and helps to ensure that everyone is working towards a common goal.
Greater Agility: CI/CD pipelines make it easier to adapt to changing requirements or user needs. Because code changes can be released quickly and easily, teams can respond more quickly to feedback and make adjustments as needed.
Reduced Risk: CI/CD pipelines help to mitigate risk by catching issues earlier in the development process. This reduces the likelihood of major issues arising later on in the development cycle, which can be costly and time-consuming to fix.
Consistent Environments: CI/CD pipelines ensure that code changes are tested and validated in consistent environments, reducing the likelihood of issues that arise due to differences in system configurations or dependencies.
Increased Productivity: CI/CD pipelines automate many of the repetitive tasks involved in software development, freeing up developers to focus on more important tasks, such as writing new code or improving existing features.
Cost Savings: CI/CD pipelines can help to reduce costs by reducing the time and resources required to deploy new features or fix issues. By automating the process, teams can work more efficiently and with fewer errors, reducing the need for expensive manual processes.
Build CI/CD Pipeline on AWS with Code Commit | Code Build | Code Deploy
This Article will demonstrate how to Create Git repository on AWS CodeCommit. Also , Compiles source code of repository with Success using CodeBuild.
What is CodeCommit?
AWS CodeCommit is a version control service hosted by Amazon Web Services (AWS) that you can use to privately store and manage assets such as documents, source code, and binary files. It is an in-house repository or infrastructure that lets you host or hold repositories. AWS CodeCommit basically gives you an environment where you can actually go ahead commit your code, code push it, or pull it. CodeCommit can be found in your AWS management console under Developer Tools.
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.
Flow of Execution
-> Process For Code Commit, we will:
Create Repository
Create Crendentials
IAM Attach policy
Add index.html file in repository
-> Process For Code build, we will:
Create a Code Build Poject
Create buidspec.yml File
Create S3 bucket
Add Artifact
→Let’s Create a CodeCommit:
Step by Step implementation:
1.Go to AWS console home and search a service “Code Commit” . Click on “Create repository”.
- Enter a Repository name and optionally description, “Click on Create”.
After creating your repository, You need GitCredentials for your AWS IAM User, Create IAM User navigate to “Security Credentials”, and Select “HTTPS GIT credentials for AWS code commit”
Click on Generate Credentials and download it.
- Go to your VS Code. Make a folder, open the terminal, and Clone your repository from CodeCommit using the command “git clone <repository_url>”.
Enter your credentials:
Username and password in the given prompt that you generated and downloaded.
- you will get an error, for this error, you have to give permission to IAM User for code commit, Back to the IAM dashboard, Click on the “Add permissions” dropdown, and click on “Add permissions”.
Pick “AWSCodePowerUser” from the given options.
Here you will get access of the code commit.
Come back to visual studio, After that, again Run the command “git clone <repository_url>”. Now the repository will be cloned.
Make a new file “index.html” and write desired code in it as given below in the screenshot.
Push the local changes to the CodeCommit repository Using the command “git add .”, git commit -m “added sample”, and “git push origin master”.
Open CodeCommit. Go to your repository and click on it, You will be able to see the “index.html” file in your repository
→Now Let’s create a CodeBuild:
1 . Go to the AWS CodeBuild page and click “Create Project”
- Enter the project name.
- Next, choose the source location of the code AWS CodeBuild will clone on its server. In this case, the source is my website’s AWS CodeCommit repo’s master branch.
- Next, I will choose the operating system image that I want to be running on the server building my project. I chose Ubuntu in this case.
- buildspec File → This is a YAML file called buildspec.yml that should be placed in the root of my repository project, and AWS CodeBuild looks for it after cloning the repo to follow the instructions declared in it.
Add buildspec.yaml file to CodeCommit Repository and complete the build process.
Go to vs code, create buildspec.yml file. Add configurations to install nginx as given below in the screenshot and save the file.
AWS CodeBuild executes certain commands at different phases of the build process. These phases are named “install”, “build”, and “post_build”. I will only be using the install and build phases to install dependencies and build my web app.The artifacts section specifies the files that should be included in the output artifact produced by the build process. In this case, the files section specifies that all files and directories in the build output should be included in the artifact. The */ pattern matches any file or directory recursively in the build output directory
The buildspec.yml file will be visible in CodeCommit.
- Go back to CodeBuild where you left Task 1. Scroll down and click on “Create build project”.
Your project will be created successfully. Click on “Start build”.
The project will start building and build successfully.
If you want your project to be built at a specific place, then you can specify the artifact upload location.
- First, create an ‘S3 bucket’
In build projects, Edit and choose ‘Artifacts’.
- Before creating artifact, Create a folder that will be used in the artifact setup
In Artifacts, select artifact type as Amazon S3 and choose bucket name also create a folder, click on “update artifacts”
- Artifact upload location successfully added. Click on ‘Start build’
The build is Succeeded.
What is CodeDeploy?
AWS CodeDeploy is a deployment service that automates application deployments to Amazon EC2 instances, on-premises instances, serverless Lambda functions, or Amazon ECS services.
AWS CodeDeploy allows developers and administrators to centrally control and track their application deployments across their different development, testing, and production environments. The service scales with your infrastructure so you can easily deploy to one EC2 instance.
Flow of execution
Process For Code Deploy, we will:
Create application
Create Deployment Group
Create Deployment
Create 2 IAM roles we will use in this blog:
IAM role for CodeDeploy to talk to EC2 instances.
IAM role for EC2 to access S3.
Create appspec.yml File
Step by Step implementation:
Deploy index.html file on EC2 machine using nginx.
- In CodeDeploy, go to Applications and click on ‘Create application’.
- Select compute platform ‘EC2/on premises’ and click on ‘Create application’.
The application is successfully created.
- Create a deployment group:
Once you have created a CodeDeploy application, you need to create a deployment group. A deployment group is a set of EC2 instances where you can deploy your application.
- Before you create a deployment group, you will need to create Service Role for code deploy.
Go to IAM Dashboard -> Roles Click on “Create Role”.Click on the dropdown there will be an option code deploy, click on Next
you will able to see AWS already selected PrePolicie “AWSCodeDeployRole”
Create the name for role and click on Create.
- Now, In the deployment group name and choose ‘Service role’. You will get generated code-deploy service role in the search bar.
- Register the instance. But before this, you have to Create an Ec2 Instance, Here you can see I have created a running instance.
- Now, In configuration, you have to setup instance detail.
- Code deployment agent
- You have to setup a CodeDeploy agent in order to deploy code on EC2.
Install the CodeDeploy agent:
You need to install the CodeDeploy agent on your Ubuntu EC2 instance. The CodeDeploy agent is a software package that runs on your instance and interacts with CodeDeploy to deploy your application. You can install the CodeDeploy agent by running the following script on your EC2 instance:
Run the script using command “vim install.sh” and paste configuration as given below.
This installs the CodeDeploy agent and its prerequisites on Ubuntu 22.04.
#!/bin/bash
sudo apt-get update
sudo apt-get install ruby-full ruby-webrick wget -y
cd /tmp
mkdir codedeploy-agent_1.3.2-1902_ubuntu22
dpkg-deb -R codedeploy-agent_1.3.2-1902_all.deb codedeploy-agent_1.3.2-1902_ubuntu22
sed 's/Depends:.*/Depends:ruby3.0/' -i ./codedeploy-agent_1.3.2-1902_ubuntu22/DEBIAN/control
dpkg-deb -b codedeploy-agent_1.3.2-1902_ubuntu22/
sudo dpkg -i codedeploy-agent_1.3.2-1902_ubuntu22.deb
systemctl list-units --type=service | grep codedeploy
sudo service codedeploy-agent status
Run the command “bash install.sh” to run your bash file which is created in the previous step.
Check whether code agent is running by firing command “sudo service code-deploy agent status”.
- Enable the load balancer and Click on Create deployment group.
11. Add appspec.yaml file to CodeCommit Repository and complete the deployment process.
- Create an appspec.yaml file:
You need to create an appspec.yaml file that tells CodeDeploy what to do with your application. Here is an appspec.yaml file that deploys the index.html file on nginx. also create 2 scripts for installing nginx and starting nginx.
Add Script for installing & starting NGINX Webserver:
Create a folder named scripts & Write install_nginx.sh to install Nginx
write start_nginx.sh in scripts folder to start the Nginx webserver
- Push all the files to code commit using ‘git add’ and ‘git commit’ commands.
- Back to Commit and see all files in the repository, All the files are updated in the code commit repository.
- Build Again these scripts, after build, the latest code will be stored in S3.
Go to Build -> Build Projects, and Click on “Start Build”. At the moment of build time check your s3 artifact, a zip file will created.
- After building completion, Go to the S3 bucket and “copy the S3 URL” of the code-output zip file inside artifact.zip. (which I created during Artifact setup).
- Create deployment:
Come Back to Deploy, Click on Application, Inside an application(Already created), click on deployment group(Already created) inside this, and Click on ‘Create deployment’.
In revision type, select amazon S3 and paste the above copied S3 url to the revision location.
Deployment is created. but events are in pending state.
EC2 doesn’t have any role policy to retrieve the data from S3 to CodeDeploy.
- So create a new service role for enabling communication between EC2 and S3, code deploy.
Here I have created a role for Ec2,
In search box ,search codedeploy and press enter, Select “AmazonEc2RoleForAWSCodeDeploy” policy.
Create the name for role and click on Create.
- Attach that service role to EC2 instance.
Select EC2 instance, In actions, go to security and click on ‘Modify IAM role’.
- After updating the IAM role, Go to instance bash, run the command “restart code-deploy agent”.
- Deployment status is Succeeded.
21. Browse the instance public IP address, it will show the output of the index.html file.
Hooray! We have successfully created a CI/CD Pipeline leveraging AWS services.
CONCLUSION & DISCUSSIONS
LIMITATION
While Continuous Integration/Continuous Delivery (CI/CD) pipelines offer many benefits, they also have some limitations that organizations should be aware of when implementing them. Here are some of the limitations of CI/CD pipelines:
Resource requirements: Implementing a CI/CD pipeline requires a significant investment in resources, including hardware, software, and personnel. Small organizations with limited resources may find it challenging to establish and maintain an effective CI/CD pipeline.
Learning curve: Implementing a CI/CD pipeline requires a high level of technical expertise and knowledge of various tools and technologies. Developers, testers, and operations teams need to learn how to use these tools and collaborate effectively in a DevOps culture, which can take time and effort.
False positives: Automated testing can sometimes produce false positives, which can lead to wasted time and effort in identifying and fixing non-existent issues. This can also lead to frustration among team members who must deal with these false positives.
Security risks: CI/CD pipelines can introduce security risks if they are not properly configured or monitored. Security testing and monitoring should be incorporated into the pipeline to ensure that software releases are secure and compliant with relevant regulations.
Complex integration: Some software systems may be too complex to be integrated into a CI/CD pipeline. These systems may require manual testing and deployment, which can slow down the software delivery process.
FUTURE ENHANCEMENT
The Continuous Integration/Continuous Delivery (CI/CD) pipeline has been evolving rapidly, and there are several future enhancements that could further improve its efficiency and effectiveness. Here are some potential future enhancements of CI/CD pipelines:
Integration with AI and ML: AI and machine learning (ML) techniques can be used to enhance automation and decision-making capabilities in CI/CD pipelines. For example, ML algorithms can analyze data from software builds to identify patterns and predict issues before they occur.
Self-healing pipelines: The self-healing CI/CD pipeline is an emerging concept that uses AI and ML to detect issues in the pipeline and automatically resolve them. Self-healing pipelines can reduce downtime and improve reliability by fixing issues quickly and without human intervention.
Continuous deployment: While CI/CD pipelines are designed to enable continuous integration and continuous delivery, the next step is to achieve continuous deployment. Continuous deployment enables automatic release of software changes to production without human intervention, further streamlining the software delivery process.
Increased focus on security: Security is an increasingly critical concern in software development, and CI/CD pipelines can play a significant role in ensuring secure software releases. Future enhancements may include more extensive security testing and monitoring throughout the pipeline.
Hybrid cloud-native solutions: As organizations increasingly adopt hybrid and multi-cloud environments, CI/CD pipelines may need to evolve to support these environments. This may include the integration of cloud-native technologies, such as Kubernetes and Docker, to enable more flexible and scalable deployment options.
CONCLUSION
The continuous Integration/Continuous Delivery (CI/CD) pipeline is a crucial part of modern software development practices. It helps automate the software delivery process, making it faster, more efficient, and less error-prone. The ultimate goal of a CI/CD pipeline is to ensure that software changes are tested, integrated, and delivered to end users in a timely and reliable manner.
In conclusion, the benefits of implementing a CI/CD pipeline are numerous. It enables faster and more frequent releases, improves the quality of the software by catching bugs early in the development cycle, and reduces the risk of deployment failures. Additionally, it provides a more collaborative and streamlined workflow for developers, testers, and operations teams.
However, building an effective CI/CD pipeline requires careful planning, design, and implementation. It requires a range of tools and technologies, including version control systems, build servers, automated testing frameworks, and deployment tools. Moreover, it requires a shift in the organizational culture towards DevOps practices and a commitment to continuous improvement.
In terms of discussions, there are several areas where CI/CD pipelines can be improved. For instance, there is a growing interest in applying machine learning and artificial intelligence techniques to enhance the automation and decision-making capabilities of CI/CD pipelines. Additionally, there is a need to incorporate security testing and monitoring into the pipeline to ensure that software releases are secure and compliant with relevant regulations.
Overall, the use of CI/CD pipelines is becoming increasingly important for organizations that want to stay competitive in today's fast-paced digital world. By adopting a CI/CD approach, organizations can improve their software delivery practices, increase their agility, and deliver value to their customers more quickly and efficiently.