From f9cb8e0fb51f8104f103c33d6ba5080aab98e364 Mon Sep 17 00:00:00 2001 From: Daniel Bast <2790401+dbast@users.noreply.github.com> Date: Thu, 6 Feb 2025 14:42:03 +0100 Subject: [PATCH] Make pre-commit super fast via caching + avoiding updating miniconda pre-commit spends most of its time setting up the hooks (=cloning their repos and installing each hook into its own virtual env). Caching helps to give almost instant feedback on new PRs. The cache invalidates when the python version, pre-commit version or pre-commit config changes. Also avoiding the setup-miniconda action as that anyways uses the miniconda already present on the runners to save time. Also adding black hook to pre-commit and limit to buildscripts. Note: This uses https://github.com/psf/black-pre-commit-mirror and not https://github.com/psf/black to avoid cloning the entire black repo (=faster). --- .github/workflows/pre-commit.yml | 27 ++++++++++++++++----------- .pre-commit-config.yaml | 5 +++++ 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/.github/workflows/pre-commit.yml b/.github/workflows/pre-commit.yml index a27723ca8..1d93ed2dc 100644 --- a/.github/workflows/pre-commit.yml +++ b/.github/workflows/pre-commit.yml @@ -10,19 +10,24 @@ on: jobs: pre-commit: - runs-on: ubuntu-latest - defaults: - run: - shell: bash -el {0} + runs-on: ubuntu-24.04 steps: - name: Checkout uses: actions/checkout@v4 - - name: Setup Miniconda - uses: conda-incubator/setup-miniconda@v3 - with: - auto-update-conda: true - auto-activate-base: false - name: Install pre-commit - run: conda install -c conda-forge pre-commit + run: | + source "${CONDA}/etc/profile.d/conda.sh" + # avoid future conflicts with base environment + conda create --name pre-commit -c conda-forge pre-commit + conda activate pre-commit + echo "versions='$(python --version)-$(pre-commit --version)'" | tee -a "${GITHUB_ENV}" + # inspired by https://github.com/pre-commit/action/blob/main/action.yml + - uses: actions/cache@v4 + with: + path: ~/.cache/pre-commit + key: pre-commit|${{ env.versions }}|${{ hashFiles('.pre-commit-config.yaml') }} - name: Lint code with pre-commit - run: pre-commit run --verbose --all-files + run: | + source "${CONDA}/etc/profile.d/conda.sh" + conda activate pre-commit + pre-commit run --verbose --all-files --show-diff-on-failure --color=always diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 31946c91e..218c560ab 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -13,6 +13,11 @@ repos: hooks: - id: clang-format types_or: [c++, c, c#, cuda, metal] + - repo: https://github.com/psf/black-pre-commit-mirror + rev: 25.1.0 + hooks: + - id: black + files: buildscripts/.*\.py$ - repo: https://github.com/PyCQA/flake8 rev: 7.1.1 hooks: