Implementing GitHub Actions CI Workflow for Tailor4Job
Inderpreet Singh Parmar
Posted on November 21, 2024
Continuous Integration (CI) has revolutionized the way developers manage and maintain their codebases. In this blog post, I’ll share my experience setting up a GitHub Actions CI workflow for my project, Tailor4Job
. I’ll also reflect on writing tests for a peer’s repository in collaboration with Fahad Ali Khan, share insights on CI, and discuss additional challenges tackled during the process.
Setting Up the GitHub Actions CI Workflow
To automate testing and ensure code quality for Tailor4Job
, I used GitHub Actions to set up a CI workflow. Here's how I approached it:
Define the Workflow
The workflow is defined in .github/workflows/python-app.yml
. It runs on every push or pull request to the main
branch and executes the following steps:
- Checks out the code.
- Sets up Python 3.9.
- Installs dependencies from
requirements.txt
. - Installs
wkhtmltopdf
, required for PDF generation tests. - Runs all tests using
pytest
.
name: Python CI
on:
push:
branches:
- main
pull_request:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: '3.9'
- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
- name: Install wkhtmltopdf
run: |
sudo apt-get update
sudo apt-get install -y wkhtmltopdf
- name: Run Tests
run: |
pytest -v --maxfail=1 --durations=10
Challenges Faced
-
Dependency Issues: I encountered errors related to the
wkhtmltopdf
dependency missing in the CI environment. Installing it resolved issues with PDF generation. - Environment Variables: Setting up API keys securely as GitHub secrets required careful attention to avoid exposing sensitive data.
Outcome
The workflow successfully ran all tests, providing immediate feedback on code changes. This streamlined the process of maintaining code quality and improved development efficiency.
Writing Tests for a Partner’s Repository
As part of the lab, I collaborated with Fahad Ali Khan to contribute tests to his repository. This was an enriching experience, as it involved adapting to a different codebase and setup.
Test Case Added
I added the following test case to verify error handling in the save_file
method of the eng_format
class:
TEST(EngFormatTests, SaveFileThrowsOnInvalidFile) {
// Set dummy environment variables for testing
setenv("API_KEY", "dummy_key", 1);
setenv("API_URL", "https://dummy.url", 1);
eng_format formatter;
// Use an invalid file path (e.g., a directory) to force an error
std::string invalidFilePath = "/invalid_path/test_output.txt";
std::string testContent = "This content should fail to save.";
// Ensure that the save_file method throws an exception for the invalid path
EXPECT_THROW(formatter.save_file(invalidFilePath, testContent), std::runtime_error);
}
Purpose
This test ensures robust error handling when saving files, verifying that the save_file
method throws a std::runtime_error
for invalid file paths.
Challenges and Learnings
- Understanding a New Codebase: Writing tests for a new repository required me to carefully analyze the code and understand its structure and logic.
- Collaboration: Communicating with Fahad to clarify ambiguities and align my tests with his expectations was vital.
- Test Integration: Ensuring the new test fit seamlessly into the existing suite required attention to compatibility and coverage.
Reflections on CI
Implementing CI for Tailor4Job
and contributing tests to Fahad’s repository has given me a new appreciation for CI's role in modern development.
Benefits of CI
- Error Prevention: CI catches bugs early in the development process, preventing them from reaching production.
- Efficiency: Automated testing saves time and ensures consistency.
- Collaboration: CI fosters a collaborative environment by ensuring code changes are rigorously tested before merging.
Future Plans
I plan to integrate CI into all future projects, particularly those involving collaboration or open-source contributions.
Conclusion
This lab was a rewarding experience that highlighted the importance of CI and testing in software development. Collaborating with Fahad Ali Khan and contributing to his repository improved my collaborative and technical skills. CI is now an integral part of my workflow, ensuring code quality and streamlining development processes.
Posted on November 21, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.