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.
In your repository, create a new file in the .github/workflows/ directory.
touch .github/workflows/pre-commit.ymlThe pre-commit.yml naming is not mandatory, you might name the file as you see fit.
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.0This workflow triggers on every push and pull request, setting up a Python environment and running pre-commit checks.
In your repository, create a new file in the .github/workflows/ directory.
touch .github/workflows/build-and-test.ymlSimilar 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: |
pytestThis 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.