Multi-Branch CI/CD (2 Branches, 2 Pipelines) allows developers to efficiently refine and test applications before deployment to live servers. By leveraging AWS CodeDeploy, CodePipeline, and GitHub repositories, developers can seamlessly integrate and deliver their code through Continuous Integration and Continuous Delivery (CI/CD) pipelines.
In the development branch, developers iteratively build and test applications, ensuring that every change is thoroughly vetted. Once the updates in the development branch meet the required standards, they are merged into the production branch. This merge automatically triggers the production pipeline, deploying the finished application for real-world usage. This streamlined process ensures rigorous testing and validation before an application reaches production, thereby increasing reliability and stability.
Create an EC2 instance in a public subnet. Configure the instance with the EC2 CodeDeploy Role instance profile and edit the EC2's security group to allow Port 80 from anywhere.
sudo yum update
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status:
sudo systemctl status nginx
sudo yum install -y ruby wget
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
# Check status:
sudo service codedeploy-agent status
Connect to Github repository. Select No filter
> Skip build stage
> Select CodeDeploy as the deploy provider > Select the Instance and CodeDeploy Application created from Step 3 and 4 > Create pipeline
.
To change the contents of the webpage along with the necessary resources, create appspec.yml
file in the GitHub respository with the following configuration:
version: 0.0
os: linux
files:
- source: /index.html
destination: /usr/share/nginx/html
- source: /style.css
destination: /usr/share/nginx/html
- source: /avatarmaker.png
destination: /usr/share/nginx/html
hooks:
BeforeInstall:
- location: scripts/before_install.sh
timeout: 300
runas: root
AfterInstall:
- location: scripts/after_install.sh
timeout: 300
runas: root
#!/bin/bash
# Stop Nginx service
service nginx stop
# Remove existing webpage files
rm -rf /usr/share/nginx/html/*
#!/bin/bash
# Copy updated webpage files to Nginx document root
cp -r /path/to/updated/html/files/* /usr/share/nginx/html/
# Restart Nginx service
service nginx start
Refresh the webpage. If everything is properly configured, you will be able to see the new webpage you've added! Any changes made to the GitHub repository will be updated automatically to the instance as well.
When satisfied with the application and updates in the Dev
branch, merge with the Prod
branch in the GitHub repository. This will send contents from the Dev branch to the Prod branch - triggering the production pipeline. The resources will mirror the contents within the Dev branch.
Finish! Congratulations you've setup and configured Multi-Branch CI/CD using AWS CodeDeploy and CodePipeline!
- Make sure to install the CodeDeploy agent on EC2 instance.
- Change
destination
parameter in theappspec.yml
file based on the OS/distributon + scripts. - Delete and recreate scripts files in proper order (before 1st, after 2nd) if script error occurs.
- Make sure EC2 instance and CodeDeploy Application have proper IAM permissions.
- Use "Release Change" to manually trigger pipeline in CodePipeline.
- Connect/Install a new app when making pipeline.
-
Stop CodeDeploy Agent
sudo service codedeploy-agent stop
-
Start CodeDeploy Agent
sudo service codedeploy-agent start
-
Check CodeDeploy Agent Status
sudo service codedeploy-agent status
-
Install the NGINX webserver. Use commands:
sudo yum update
sudo yum install nginx
sudo systemctl start nginx
sudo systemctl enable nginx
# Check status:
sudo systemctl status nginx
- Install the CodeDeploy Agent using commands:
sudo yum install -y ruby wget
wget https://aws-codedeploy-us-east-1.s3.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
# Check status:
sudo service codedeploy-agent status