Before we dive into the setup, let’s briefly cover why CI/CD is so critical:
In ReadmeGenie, we leveraged GitHub Actions as our CI/CD tool. It integrates smoothly with GitHub repositories and offers flexibility and automation through YAML configuration files.
Our CI/CD pipeline includes the following automated steps:
The CI workflow is defined in .github/workflows/python-app.yml. Here’s a breakdown of what each part of the workflow does:
The workflow runs on every push and pull request to the main branch. This ensures that all code changes undergo validation before merging into production.
name: Python application on: push: branches: ["main"] pull_request: branches: ["main"]
We configure GitHub Actions to use Python 3.12.x, ensuring consistency with our local development environment. This step installs the specific Python version and prepares the environment for dependency installation.
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python 3.12.x uses: actions/setup-python@v3 with: python-version: "3.12.x"
The next step is to install project dependencies. Here, we upgrade pip and install requirements.txt file, it will install additional dependencies specified there.
- name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
Linting is a crucial part of our workflow, ensuring the code adheres to specified quality standards. We run flake8 with options to flag syntax errors, undefined names, and complexity issues.
name: Python application on: push: branches: ["main"] pull_request: branches: ["main"]
For unit tests, we use pytest to run all test cases. Additionally, we use coverage to track which lines of code are tested, ensuring that our test suite meets the defined coverage threshold of 75%.
The following commands run the tests and generate a coverage report, highlighting any gaps in test coverage. This is essential for quality assurance, as untested code is a potential source of future bugs.
jobs: build: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Set up Python 3.12.x uses: actions/setup-python@v3 with: python-version: "3.12.x"
This coverage check ensures a high standard of code quality by enforcing that at least 75% of the codebase is covered by tests. If coverage falls below this threshold, the commit will not be allowed.
In addition to CI/CD, we set up pre-commit hooks to enforce code quality locally before any changes are pushed to the repository. These hooks:
Here’s how we added the coverage check as a pre-commit hook in .pre-commit-config.yaml:
- name: Install dependencies run: | python -m pip install --upgrade pip pip install flake8 pytest if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
Setting up CI/CD required a deep understanding of how different tools (flake8, pytest, coverage) interact within GitHub Actions. Here are some challenges we faced and the solutions we implemented:
We encountered issues with environment variable conflicts, especially in testing API integration and configuration handling. Using @patch.dict and other mocking techniques in unittest allowed us to simulate the environment effectively.
The biggest challenge was ensuring adequate test coverage. Using coverage.py with --fail-under=75 in both GitHub Actions and pre-commit hooks helped enforce this standard.
To make the CI/CD pipeline more robust, we plan to:
Through this project, I realized the importance of establishing robust testing and CI/CD practices early on. If I were to start again, I’d focus on writing comprehensive tests from the beginning and incrementally expanding and improving them as the project progresses. This approach would prevent missing branches or untested areas and ensure that all new code integrates smoothly into a well-covered codebase.
The above is the detailed content of Implementing CI/CD for ReadmeGenie. For more information, please follow other related articles on the PHP Chinese website!