Skip to content

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 - developers seamlessly integrate and deliver their code through CI/CD pipelines.

Notifications You must be signed in to change notification settings

ericincloud/Multi-Branch-CI-CD

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Multi-Branch-CI-CD

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.

Architectural Diagram

2BranchArch

Step 1: Create 2 IAM roles - CodeDeploy EC2 Role AND CodeDeploy Role

IAMroleCD IAMroleCD2

Step 2: Creating the Development Pipeline - Create Development branch

Create a Development or Dev branch with GitHub respository.

Step 3: Setup EC2 instance

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.

CDinstance CDinstance2 CDinstance3 AllowPort80

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

Verify functionality by accessing the instance IP. You should see a default NGINX webpage.

NginxDefWeb

Step 4: Create CodeDeploy Application

Head to CodeDeploy > Applications > Create Application > Create Deployment Group

CreateCDApp DepGroup DepGroup3 DepGroup4 DepGroup5

Step 5: Create Pipeline

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.

pipeline1 pipeline2 pipeline3 pipeline4

Step 6: Create appspec.yml file and upload files

Upload files. This example repository will contain index.html, style.css, and avatarmaker.png.

CodeDepFiles

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

Create a Scripts folder with files after_install.sh and before_install.sh.

Within the before_install.sh include:

#!/bin/bash

# Stop Nginx service
service nginx stop

# Remove existing webpage files
rm -rf /usr/share/nginx/html/*

Within the after_install.sh file include:

#!/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

CodeDepScripts

Step 7: Verify Functionality

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.

CDWebpage

Step 8: Create the Production Pipeline and Branch

Create a Production or Prod branch in GitHub repository.

Repeat Steps 3 to 5 to create Production pipeline.

Step 9: Merge Dev and Prod

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.

merge merge2 merged

Finish! Congratulations you've setup and configured Multi-Branch CI/CD using AWS CodeDeploy and CodePipeline!

Notes

  • Make sure to install the CodeDeploy agent on EC2 instance.
  • Change destination parameter in the appspec.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.

Reference

  • 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

About

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 - developers seamlessly integrate and deliver their code through CI/CD pipelines.

Topics

Resources

Stars

Watchers

Forks