Thursday, September 21, 2023

A Simple GitHub CI/CD Demo

Introduction

In the world of software development, continuous integration and continuous deployment (CI/CD) have become essential practices. They enable developers to automate the building, testing, and deployment of their code, resulting in faster development cycles and more reliable software. GitHub, one of the most popular version control platforms, provides robust support for CI/CD workflows through its GitHub Actions feature. In this article, we'll take a hands-on approach to explore CI/CD on GitHub by creating a simple Python project and setting up a GitHub Actions workflow.

About CI/CD

Before we dive into the practical demonstration, let's briefly discuss what CI/CD is and why it's important. CI/CD stands for Continuous Integration and Continuous Deployment. It's a set of practices and tools that allow developers to automate the process of integrating code changes into a shared repository (Continuous Integration) and deploying those changes to production or other environments (Continuous Deployment). CI/CD promotes collaboration, reduces manual errors, and accelerates software delivery.

Our Main Goal

Each time we push the changes to the repository on Github, we will run a test program to check if the newly uploaded program is free of any bugs or errors.

Creating a Simple Python Program

Our journey begins with the creation of a repository on GitHub, I named it ci_cd_demo then we create a straightforward Python program called app.py. This program will serve as our application under development.

1
2
3
4
5
6
def add(a, b):
    return a + b

if __name__ == "__main__":
    result = add(4, 5)
    print(f"Result: {result}")

Creating a Unit Test for app.py

To ensure the correctness of our code, we'll create a unit test for app.py. We'll call it test_app.py.

1
2
3
4
5
6
import app

def test_add():
    assert app.add(3, 5) == 8
    assert app.add(0, 0) == 0
    assert app.add(-1, 1) == 0


Setting Up GitHub Workflow

The heart of our CI/CD demo is the GitHub Actions workflow. First we create a directory on our GitHub repository called .github/workflows .Then  we will create a YAML file called ci-cd.yml. This file defines the steps that GitHub should follow when certain events occur, in our case, whenever a push event is detected, it will check first if the main branch has chhanges to it, if it does, it will confirm the changes again and run the test_app.py.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
name: Push Code and Run Tests

on:
  push:
    branches:
      - main  # Change this to your main branch name

jobs:
  push-code-and-test:
    name: Push code and run tests
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Check for Changes
        run: |
          # Check if there are changes in the working directory
          if [ -n "$(git status --porcelain)" ]; then
            git config user.name "bclme"
            git config user.email "blas_lomibao@yahoo.com"           
            git add .
            git commit -m "Pushing latest code"
          else
            echo "No changes to commit."
          fi

      - name: Debug Repository Contents
        run: |
          ls -R

      - name: Run Tests
        run: |
          # Run your tests here
          python test_app.py

Checking the Workflow

To check if our workflow is functioning correctly, we navigate to the "Actions" menu in our GitHub repository. We should see our workflow listed there, and if everything is set up correctly, the workflow logs should indicate that the setup has no errors.



Creating an Error for Testing

As a final step, let's introduce an error intentionally. We'll edit app.py to have a syntax error and push it to the repository. This will trigger our CI/CD workflow, and we can observe how it handles the error in the logs.

Conclusion

CI/CD is a powerful practice that can streamline your development process and enhance the quality of your software. With GitHub Actions, setting up CI/CD pipelines has become more accessible than ever. In this demo, we've seen how to create a simple Python project, write unit tests, and set up a basic CI/CD workflow on GitHub. By embracing these practices, you'll be well on your way to delivering software more efficiently and reliably.

For a more detailed article about this topic, please become my patron.

No comments:

Post a Comment