Skip to content

Commit 16d2175

Browse files
authored
feat(run-pre-commit): Enable caching, support pre-commit pinning (#39)
* feat(run-pre-commit): Enable pip caching * chore(run-pre-commit): Use pre-commit config file as cache hash * feat(run-pre-commit): Add support for pre-commit version pinning * feat(run-pre-commit): Add Rust toolchain caching * chore(run-pre-commit): Improve Hadolint install step * chore(run-pre-commit): Add comment to explain cache-dependency-path * fix(run-pre-commit): Replace commas with underscores in cache key
1 parent 819ff15 commit 16d2175

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

run-pre-commit/action.yml

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ inputs:
88
python-version:
99
description: Python version to install
1010
default: "3.12"
11+
pre-commit-version:
12+
description: Pre-commit version to install
13+
default: 4.2.0
1114
rust:
1215
description: Whether to install the Rust toolchain (and which version to use)
1316
rust-components:
@@ -30,17 +33,61 @@ runs:
3033
uses: actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
3134
with:
3235
python-version: ${{ inputs.python-version }}
36+
# It doesn't make a whole lot of sense to use the pre-commit config file
37+
# as the dependency file, but the setup-python action looks for
38+
# requirements.txt and pyproject.toml by default, which are both not
39+
# present (in most cases). To override these two defaults, we need to
40+
# specify a different file. Specifying the pre-commit config at least
41+
# guarantees that the cache is invalidated if the config changes.
42+
# Ideally we want to invalidate when the pre-commit or Python version
43+
# changes, but that's not easily possible.
44+
cache-dependency-path: .pre-commit-config.yaml
45+
cache: 'pip'
46+
47+
- name: Install pre-commit (${{ env.PRE_COMMIT_VERSION }})
48+
shell: bash
49+
env:
50+
PRE_COMMIT_VERSION: ${{ inputs.pre-commit-version }}
51+
run: |
52+
python -m pip install "pre-commit==$PRE_COMMIT_VERSION"
53+
54+
# This caches downloaded pre-commit hook artifacts and results in faster
55+
# workflow runs after an initial hydration run with the exact same hooks
56+
- name: Setup pre-commit Cache
57+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
58+
with:
59+
path: ~/.cache/pre-commit
60+
key: pre-commit-${{ inputs.pre-commit-version }}-python${{ inputs.python-version }}-${{ hashFiles('.pre-commit-config.yaml') }}
61+
62+
- name: Format Rust Toolchain Cache Key
63+
if: ${{ inputs.rust }}
64+
shell: bash
65+
env:
66+
RUST_COMPONENTS: ${{ inputs.rust-components }}
67+
run: |
68+
RUST_COMPONENTS=${RUST_COMPONENTS//,/_}
69+
echo "RUST_COMPONENTS=$RUST_COMPONENTS" | tee -a "$GITHUB_ENV"
70+
71+
- name: Setup Rust Toolchain Cache
72+
id: rust-toolchain-cache
73+
if: ${{ inputs.rust }}
74+
uses: actions/cache@5a3ec84eff668545956fd18022155c47e93e2684 # v4.2.3
75+
with:
76+
path: ~/.rustup/toolchains
77+
key: rust-toolchains-${{ inputs.rust }}-components-${{ env.RUST_COMPONENTS }}
3378

3479
- name: Setup Rust Toolchain
3580
uses: dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c
36-
if: ${{ inputs.rust }}
81+
if: ${{ inputs.rust && steps.rust-toolchain-cache.outputs.cache-hit != 'true' }}
3782
with:
3883
toolchain: ${{ inputs.rust }}
3984
components: ${{ inputs.rust-components }}
4085

41-
- name: Setup Hadolint
86+
- name: Install Hadolint
4287
if: ${{ inputs.hadolint }}
4388
shell: bash
89+
env:
90+
HADOLINT_VERSION: ${{ inputs.hadolint }}
4491
run: |
4592
set -euo pipefail
4693
@@ -51,8 +98,8 @@ runs:
5198
ARCH=$(uname -m)
5299
53100
mkdir -p "$LOCATION_DIR"
54-
curl -sL -o "${LOCATION_BIN}" "https://github.com/hadolint/hadolint/releases/download/${{ inputs.hadolint }}/hadolint-$SYSTEM-$ARCH"
55-
chmod 700 "${LOCATION_BIN}"
101+
curl -sL -o "$LOCATION_BIN" "https://github.com/hadolint/hadolint/releases/download/${HADOLINT_VERSION}/hadolint-$SYSTEM-$ARCH"
102+
chmod 700 "$LOCATION_BIN"
56103
57104
echo "$LOCATION_DIR" | tee -a "$GITHUB_PATH"
58105
@@ -70,6 +117,10 @@ runs:
70117
github_access_token: ${{ inputs.nix-github-token }}
71118
install_url: https://releases.nixos.org/nix/nix-${{ inputs.nix }}/install
72119

73-
- uses: pre-commit/action@2c7b3805fd2a0fd8c1884dcaebf91fc102a13ecd # v3.0.1
74-
with:
75-
extra_args: "--from-ref '${{ github.event.pull_request.base.sha }}' --to-ref '${{ github.event.pull_request.head.sha }}'"
120+
- name: Run pre-commit
121+
shell: bash
122+
env:
123+
BASE_SHA: ${{ github.event.pull_request.base.sha }}
124+
HEAD_SHA: ${{ github.event.pull_request.head.sha }}
125+
run: |
126+
pre-commit run --show-diff-on-failure --color always --from-ref "$BASE_SHA" --to-ref "$HEAD_SHA"

0 commit comments

Comments
 (0)