DEV Community

Cover image for CI/CD Pipeline with github actions & AWS EC2 instance
Noble Mutuwa  Mulaudzi
Noble Mutuwa Mulaudzi

Posted on • Updated on

CI/CD Pipeline with github actions & AWS EC2 instance

Introduction

In today's fast-paced software development environment, continuous integration and continuous deployment (CI/CD) pipelines are essential for streamlining the software delivery process. GitHub Actions, coupled with the power of AWS EC2 instances, provide a robust and scalable solution for automating CI/CD workflows. In this article, i will guide you through the process of setting up a CI/CD pipeline using GitHub Actions and an AWS EC2 instance.

Architecture diagram

Image description

NOTE:You can follow the same steps to deploy to shared hosting platforms like Cpanel, Plesk and more.

pre-requisites

  • Github Account.

  • AWS account.

  • Git repository with your application's code.

Creating our web server.

  • Log in to your AWS Management Console and navigate to the EC2 service.

  • Launch an EC2 instance, choosing the appropriate Amazon Machine Image (AMI) based on your requirements.

  • Configure security groups and access credentials to allow incoming traffic through ssh, http and https.

  • Here is our webserver

Image description

Configuring GitHub Environment:

  • Environments helps you to Identify the different environments needed for your application, such as development, staging, and production.

  • In your GitHub repository, navigate to the "Settings" tab and click on "Environments".

  • Create the desired environments, specifying a name and optional description for each. ( i have created the environment to be QA-mywebsite)

Image description

Configuring GitHub Secrets for our QA environment:

  • GitHub Actions allows you to securely store sensitive information like access keys or tokens using Secrets.

  • In the GitHub repository, go to "Settings" and click on Environments.

  • Navigate to your environment (QA environments)

  • Add secrets required for connecting to your AWS EC2 instance, such as access keys or SSH private keys..

Here is our environment secrets configuartion:

Image description

Configuring our workflow file.

  • In the root of your repository , create a folder called .github/workflows and create a file called deploy.yml

Here is our deploy.yml file.

name: Deploy to EC2

on:
  push:
    branches:
      - master


jobs:
  checkout_mycode:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: check out
        id: check
        run: |
          echo "checking out the code"


  deploy_to_qa:
    needs: checkout_mycode
    if: github.ref == 'refs/heads/master'
    runs-on: ubuntu-latest
    environment: 
      name: QA-mywebsite
      url: http://noble-mutuwa.com/

    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Set up SSH
        uses: webfactory/ssh-agent@v0.5.0
        with:
          ssh-private-key: ${{ secrets.SERVER_SSH_PRIVATE_KEY }}

      - name: Copy files to EC2
        run: scp -r -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null "${{ github.workspace }}" ec2-user@50.17.57.13:~/website/

      - name: SSH into EC2
        run: | 
          ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ec2-user@50.17.57.13 "sudo cp -r ~/website/mywebsite/* /var/www/html/"

  qa_send_notification:
    needs: deploy_to_qa
    if: needs.deploy_to_qa.result == 'success'
    runs-on: ubuntu-latest
    steps:
      - name: Send notification to repository owner
        run: |
          REPO_OWNER=$(jq --raw-output .repository.owner.login "${GITHUB_EVENT_PATH}")
          NOTIFICATION="Deployment qa completed."
          API_URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/notifications"
          RESPONSE=$(curl -sSL -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" -H "Content-Type: application/json" -X POST -d "{\"subject\":\"$NOTIFICATION\",\"repository\":\"$REPO_OWNER/$GITHUB_REPOSITORY\"}" "$API_URL")
          echo "Notification sent to repository owner: $REPO_OWNER"         


Enter fullscreen mode Exit fullscreen mode

Deploying to EC2

  • Save the deploy.yml file and push the code to your remote repository.

  • Go to your github repo to observe the pipeline.

  • Note that after checking the code ,the workflow will prompt you to review the changes and approve in order to deploy to your EC2 instance. (remember that we added reviewers when we created our QA-website environment)

Image description

  • We will review the deployment and approve.

  • After the deployment has been done, we wil get the end point (url) for our application that we defined in the deploy.yml file

Image description

  • We can now access our Application using the url

Image description

  • There is our beautiful flask(python) application with a Chatbot

  • In my next article , i will take you through on how you can build this chat bot , train it with data and how the chatbot can learn from different responses provided by the user

Article by Noble Mutuwa Mulaudzi

Top comments (0)