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 |