Continuous Integration & Testing using GitHub Actions
Implement Continuous Integration & Continuous Testing using GitHub Actions
Repository
Overview
Continuous Integration means all modifications to the codebase, regardless of their impact, must be tested immediately. Assume you’ve an assistant sitting next to you, instruct him to check that nothing is broken. That assistant will take care of everything for you. We can instruct it to do whatever we want, and it will do the job for us. When I’ll make changes in the codebase and push it to the cloud, the assistant will always be on the lookout, and test the changes to guarantee that the “codebase is flawless”. If I make a mistake or break anything that isn’t meant to be broken, the assistant will immediately warn us right away that your codebase isn’t correctly integrating with the existing project, please check it out.
This assistant is none other than “GitHub Actions”.
Let’s implement a simple GitHub Action
- Let’s first create a virtual environment (venv) for our sample project.
Step 1. Move to the location where you want to create venv.
Step 2. Check whether python is installed or not in your system.
Step 3. Check whether venv is installed or not, if not run :python -m pip install virtualenv
Step 4. Run:python -m virtualenv env_name
to create a venv.
Step 5. Once created activate it using cmd:.\env_name\Scripts\activate
Step 6. Once it’s activated, venv’s name will mentioned before the path ex.(env_name) D:\Pycharm\MLOps>
- Once venv is created, let’s create a simple program to calculate BMI.
- Now let’s create user-defined exceptions.
- Now let’s write unit tests for our program. Here I’m using
pytest
framework.
- Once we’re done with the codebase, let’s create
requirements.txt
file. We can either usepip freeze > requirements.txt
cmd or any third-party library to generate refinedrequirements.txt
. Let’s move ahead!
- To write the CI workflow we need to create
.github/workflows
dir.
Run the cmd:mkdir -p .github/workflows
Now create.yaml
file and write the workflow.
- Now push the code on Git Hub, and it’ll automatically run the action as we’ve set it for any push to the main branch. For in-depth details, refer the official doc.
Added snapshot of final output for quick reference.
The live project is available here. Just Fork it, do some experimentation with it, and see the results.
Conclusion
Let’s wrap up our exploration of CI using GitHub actions, knowing that the journey of learning never ends. If this blog has sparked your curiosity or ignited new ideas, follow me on Medium, connect on LinkedIn, and let’s keep the curiosity alive.
Your questions, feedback, and perspectives are not just welcomed but celebrated. Feel free to reach out with any queries or share your thoughts.
Thank you 🙌