[tutorial_ml:05] Integrating GitHub Actions with Pre-Commit and Pytest
Section 005
GitHub Actions is a powerful CI/CD platform that allows you to automate your workflow directly from your GitHub repository. By integrating GitHub Actions with pre-commit and pytest, you can ensure that every code change pushed to your repository adheres to quality standards and passes all tests.
Setting pre-commit
Create a Workflow File: In your repository, create a new file in the .github/workflows/
directory.
Configure the Workflow: Define the steps in your pre-commit.yml file to install and run pre-commit hooks on every push and pull request. Here’s an example configuration:
name: Pre-Commit
on: [push, pull_request]jobs:
pre-commit:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
with:
python-version: '3.x'
- uses: pre-commit/action@v3.0.0
This workflow triggers on every push and pull request, setting up a Python environment and running pre-commit checks.
Setting Up GitHub Actions for Pytest
Create a Workflow File: In your repository, create a new file in the .github/workflows/
directory.
Configure Pytest Workflow: Similar to the pre-commit setup, define steps to install Python, dependencies, and run pytest. Example configuration:
name: Python Tests
on: [push, pull_request]jobs:
build: runs-on: ubuntu-latest
environment: staging steps:
- uses: actions/checkout@v3 - name: Set up Python 3.X
uses: actions/setup-python@v3
with:
python-version: "3.X" - name: Installing tutorial package
run: |
python -m pip install -e . - name: Install dependencies
run: |
python -m pip install --upgrade pip
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi - name: Running pytests
run: |
pytest
This setup ensures that pytest is run in an isolated environment on GitHub’s servers, testing your code in a clean state.
This two simple steps on github actions guarantee the following:
Automated Quality Checks: With every push and pull request, your code is automatically checked for formatting issues and tested.
Immediate Feedback: Developers get immediate feedback on their commits and pull requests, enhancing the code review process.
Consistency: This setup enforces coding standards and testing requirements, ensuring consistency across all contributions.
Integrating GitHub Actions with pre-commit and pytest adds a robust layer of automation to your workflow. It not only streamlines the development process but also reinforces the quality and reliability of your code base.
Previous Section 004: Leveraging pre-commit for Code Quality Assurance
Next Section 006: Containerizing with Docker for an Integrated Development Environment
Code base
Access the entire code on github.com/rfelixmg/tutorial_ml
Remember to fork and star if you like it!