Skip to content

Latest commit

 

History

History
106 lines (73 loc) · 2.89 KB

File metadata and controls

106 lines (73 loc) · 2.89 KB

Section 5: Integrating GitHub Actions with Pre-Commit and Pytest

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.

1. Setting pre-commit

Create a Workflow File

In your repository, create a new file in the .github/workflows/ directory.

touch .github/workflows/pre-commit.yml

The pre-commit.yml naming is not mandatory, you might name the file as you see fit.

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.


2. Setting Up GitHub Actions for Pytest

Create a Workflow File

In your repository, create a new file in the .github/workflows/ directory.

touch .github/workflows/build-and-test.yml

Configure the 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.