Skip to content

[CI/CD] Add Pre-commit Validation as First Step of Testing Pipeline #29

@Sinitca-Aleksandr

Description

@Sinitca-Aleksandr

Issue: [CI/CD] Add Pre-commit Validation as First Step of Testing Pipeline

Summary

Implement GitHub Actions workflow to run pre-commit validation as the first gate in the CI/CD pipeline, preventing style/lint issues from entering the codebase and ensuring consistency across contributions.

Related: Pre-commit configuration already exists in repository (.pre-commit-config.yaml)
Reference Implementation: FluctuationAnalysisTools repository CI/CD pipeline


Motivation

  • Catch code style, formatting, and linting issues before running expensive tests
  • Ensure all contributions follow project standards (black, isort, YAML/JSON validation, etc.)
  • Reduce PR review time by automating mechanical checks
  • Align with sister project FluctuationAnalysisTools CI/CD patterns for maintainability

🔧 Technical Requirements

1. Create Reusable Pre-commit Validation Workflow

Create .github/workflows/reusable_pre-commit-validation.yml based on the reference implementation:

# .github/workflows/reusable_pre-commit-validation.yml
name: Reusable - Pre-commit Validation

on:
  workflow_call:
    outputs:
      cache-key:
        description: 'The cache key used for pip dependencies'
        value: ${{ jobs.pre-commit-validation.outputs.cache-key }}

permissions:
  contents: read

jobs:
  pre-commit-validation:
    runs-on: ubuntu-latest
    outputs:
      cache-key: ${{ steps.cache-pip.outputs.key || 'pip-precommit-' }}

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Set up Python
        uses: actions/setup-python@v4
        with:
          python-version: "3.11"  # Match pyproject.toml requirement

      - name: Cache pip dependencies
        id: cache-pip
        uses: actions/cache@v3
        with:
          path: ~/.cache/pip
          key: pip-precommit-${{ hashFiles('**/pyproject.toml', '**/.pre-commit-config.yaml') }}
          restore-keys: |
            pip-precommit-

      - name: Install pre-commit
        shell: bash
        run: |
          python -m pip install --upgrade pip
          pip install pre-commit

      - name: Run pre-commit validation
        shell: bash
        run: |
          pre-commit run --all-files

2. Update/Create Main CI Workflow

Create or update .github/workflows/test-and-release.yml to use the reusable workflow:

# .github/workflows/test-and-release.yml (skeleton)
name: Test and Release

on:
  push:
    branches: [master, main]
    tags:
      - 'release/*'
  pull_request:
    branches: [master, main]
  workflow_dispatch:  # Allow manual triggering

permissions:
  contents: read

jobs:
  #  FIRST STEP: Pre-commit validation
  pre-commit-validation:
    uses: ./.github/workflows/reusable_pre-commit-validation.yml

  # Subsequent jobs depend on pre-commit passing
  test:
    needs: pre-commit-validation
    # ... existing test configuration ...

  build:
    needs: pre-commit-validation
    # ... existing build configuration ...

3. Verify Pre-commit Configuration Compatibility

Current .pre-commit-config.yaml uses [[10]]:

repos:
  - repo: https://github.com/pre-commit/pre-commit-hooks
    rev: v3.2.0  #  Consider updating to v6.0.0 (as in FluctuationAnalysisTools)
  - repo: https://github.com/pycqa/isort
    rev: 6.0.0
  - repo: https://github.com/psf/black-pre-commit-mirror
    rev: 25.1.0

Recommended updates:

  • Upgrade pre-commit-hooks to v6.0.0 for latest security patches [[1]]
  • Ensure black language_version matches project Python requirement (currently python3.10 in bayes_model vs python3 in reference)

✅ Acceptance Criteria

  • Pre-commit validation runs automatically on:
    • pull_request events (all branches)
    • push to master/main
    • Manual trigger via workflow_dispatch
  • Workflow fails fast if pre-commit checks fail (no downstream jobs execute)
  • Pip dependencies are cached to reduce CI runtime
  • Workflow uses same Python version as defined in pyproject.toml
  • Documentation updated in CONTRIBUTION.md to mention CI pre-commit requirement
  • Local developers can run same checks via pre-commit run --all-files

Implementation Steps

  1. Create reusable workflow file:

    mkdir -p .github/workflows
    touch .github/workflows/reusable_pre-commit-validation.yml
  2. Copy/adapt workflow content from FluctuationAnalysisTools reference [[1]]

  3. Update main CI workflow to call reusable workflow with needs: dependency

  4. Test locally:

    # Install pre-commit hooks for local development
    pre-commit install
    # Run validation manually
    pre-commit run --all-files
  5. Validate in PR: Create draft PR to trigger GitHub Actions and verify workflow behavior


Testing Strategy

Local Testing

# Install pre-commit
pip install pre-commit

# Install git hooks
pre-commit install

# Test all files
pre-commit run --all-files

# Test specific hook
pre-commit run black --all-files

CI Validation Checklist

  • Workflow triggers on PR creation
  • Pre-commit job appears first in Actions tab
  • Cache hit/miss behavior works correctly
  • Failure in pre-commit blocks downstream jobs
  • Success allows test/build jobs to proceed

References


ℹ️ Note for Contributors:

  1. This workflow should be non-breaking for existing PRs — consider adding continue-on-error: true temporarily during rollout if needed.
  2. Ensure all existing code passes pre-commit hooks before merging this CI change (run pre-commit run --all-files locally first).
  3. Coordinate with maintainers before updating hook versions in .pre-commit-config.yaml to avoid disrupting active development.
  4. Follow existing repository conventions: ruff, black, isort configuration in pyproject.toml.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request
    No fields configured for Feature.

    Projects

    Status
    Available Items

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions