From fb0b883a6fdd358b67b2dc8d3dacd95bb0d6fb15 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 12:32:25 +0200 Subject: [PATCH 1/7] feat(run-pre-commit): Enable pip caching --- run-pre-commit/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index 18184fd..bbcb873 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -30,6 +30,7 @@ runs: uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0 with: python-version: ${{ inputs.python-version }} + cache: 'pip' - name: Setup Rust Toolchain uses: dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c From 60faf3452fbfd943f1749354ff81011285f74f04 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 12:35:25 +0200 Subject: [PATCH 2/7] chore(run-pre-commit): Use pre-commit config file as cache hash --- run-pre-commit/action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index bbcb873..397ea9a 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -30,6 +30,7 @@ runs: uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0 with: python-version: ${{ inputs.python-version }} + cache-dependency-path: .pre-commit-config.yaml cache: 'pip' - name: Setup Rust Toolchain From 05fed9ddaebee68ff483e0a7b0ff98b9dfcded87 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 14:21:46 +0200 Subject: [PATCH 3/7] feat(run-pre-commit): Add support for pre-commit version pinning --- run-pre-commit/action.yml | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index 397ea9a..59fc475 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -8,6 +8,9 @@ inputs: python-version: description: Python version to install default: "3.12" + pre-commit-version: + description: Pre-commit version to install + default: 4.2.0 rust: description: Whether to install the Rust toolchain (and which version to use) rust-components: @@ -33,6 +36,21 @@ runs: cache-dependency-path: .pre-commit-config.yaml cache: 'pip' + - name: Install pre-commit (${{ env.PRE_COMMIT_VERSION }}) + shell: bash + env: + PRE_COMMIT_VERSION: ${{ inputs.pre-commit-version }} + run: | + python -m pip install "pre-commit==$PRE_COMMIT_VERSION" + + # This caches downloaded pre-commit hook artifacts and results in faster + # workflow runs after an initial hydration run with the exact same hooks + - name: Setup pre-commit Cache + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 + with: + path: ~/.cache/pre-commit + key: pre-commit-${{ inputs.pre-commit-version }}-python${{ inputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }} + - name: Setup Rust Toolchain uses: dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c if: ${{ inputs.rust }} @@ -72,6 +90,10 @@ runs: github_access_token: ${{ inputs.nix-github-token }} install_url: https://releases.nixos.org/nix/nix-${{ inputs.nix }}/install - - uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1 - with: - extra_args: "--from-ref '${{ github.event.pull_request.base.sha }}' --to-ref '${{ github.event.pull_request.head.sha }}'" + - name: Run pre-commit + shell: bash + env: + BASE_SHA: ${{ github.event.pull_request.base.sha }} + HEAD_SHA: ${{ github.event.pull_request.head.sha }} + run: | + pre-commit run --show-diff-on-failure --color always --from-ref "$BASE_SHA" --to-ref "$HEAD_SHA" From f8d51e80ef476783869420d02162a8d69919475c Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 14:22:53 +0200 Subject: [PATCH 4/7] feat(run-pre-commit): Add Rust toolchain caching --- run-pre-commit/action.yml | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index 59fc475..48f048b 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -51,9 +51,17 @@ runs: path: ~/.cache/pre-commit key: pre-commit-${{ inputs.pre-commit-version }}-python${{ inputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }} + - name: Setup Rust Toolchain Cache + id: rust-toolchain-cache + if: ${{ inputs.rust }} + uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 + with: + path: ~/.rustup/toolchains + key: rust-toolchains-${{ inputs.rust }}-components-${{ inputs.rust-components }} + - name: Setup Rust Toolchain uses: dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c - if: ${{ inputs.rust }} + if: ${{ inputs.rust && steps.rust-toolchain-cache.outputs.cache-hit != 'true' }} with: toolchain: ${{ inputs.rust }} components: ${{ inputs.rust-components }} From 701c7195ed2ee65db5fe27090e49e219dc7c621a Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 14:23:30 +0200 Subject: [PATCH 5/7] chore(run-pre-commit): Improve Hadolint install step --- run-pre-commit/action.yml | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index 48f048b..d59039e 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -66,9 +66,11 @@ runs: toolchain: ${{ inputs.rust }} components: ${{ inputs.rust-components }} - - name: Setup Hadolint + - name: Install Hadolint if: ${{ inputs.hadolint }} shell: bash + env: + HADOLINT_VERSION: ${{ inputs.hadolint }} run: | set -euo pipefail @@ -79,8 +81,8 @@ runs: ARCH=$(uname -m) mkdir -p "$LOCATION_DIR" - curl -sL -o "${LOCATION_BIN}" "https://github.com/hadolint/hadolint/releases/download/${{ inputs.hadolint }}/hadolint-$SYSTEM-$ARCH" - chmod 700 "${LOCATION_BIN}" + curl -sL -o "$LOCATION_BIN" "https://github.com/hadolint/hadolint/releases/download/${HADOLINT_VERSION}/hadolint-$SYSTEM-$ARCH" + chmod 700 "$LOCATION_BIN" echo "$LOCATION_DIR" | tee -a "$GITHUB_PATH" From 5504c939a7b0725e1c688fd8db4e7f3231438606 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 14:24:04 +0200 Subject: [PATCH 6/7] chore(run-pre-commit): Add comment to explain cache-dependency-path --- run-pre-commit/action.yml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index d59039e..4a823be 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -33,6 +33,14 @@ runs: uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0 with: python-version: ${{ inputs.python-version }} + # It doesn't make a whole lot of sense to use the pre-commit config file + # as the dependency file, but the setup-python action looks for + # requirements.txt and pyproject.toml by default, which are both not + # present (in most cases). To override these two defaults, we need to + # specify a different file. Specifying the pre-commit config at least + # guarantees that the cache is invalidated if the config changes. + # Ideally we want to invalidate when the pre-commit or Python version + # changes, but that's not easily possible. cache-dependency-path: .pre-commit-config.yaml cache: 'pip' From fe22813444d062d8e92e1412519787dd58084e71 Mon Sep 17 00:00:00 2001 From: Techassi Date: Thu, 17 Apr 2025 14:35:24 +0200 Subject: [PATCH 7/7] fix(run-pre-commit): Replace commas with underscores in cache key --- run-pre-commit/action.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/run-pre-commit/action.yml b/run-pre-commit/action.yml index 4a823be..9db5ea3 100644 --- a/run-pre-commit/action.yml +++ b/run-pre-commit/action.yml @@ -59,13 +59,22 @@ runs: path: ~/.cache/pre-commit key: pre-commit-${{ inputs.pre-commit-version }}-python${{ inputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }} + - name: Format Rust Toolchain Cache Key + if: ${{ inputs.rust }} + shell: bash + env: + RUST_COMPONENTS: ${{ inputs.rust-components }} + run: | + RUST_COMPONENTS=${RUST_COMPONENTS//,/_} + echo "RUST_COMPONENTS=$RUST_COMPONENTS" | tee -a "$GITHUB_ENV" + - name: Setup Rust Toolchain Cache id: rust-toolchain-cache if: ${{ inputs.rust }} uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3 with: path: ~/.rustup/toolchains - key: rust-toolchains-${{ inputs.rust }}-components-${{ inputs.rust-components }} + key: rust-toolchains-${{ inputs.rust }}-components-${{ env.RUST_COMPONENTS }} - name: Setup Rust Toolchain uses: dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c