8
8
python-version :
9
9
description : Python version to install
10
10
default : " 3.12"
11
+ pre-commit-version :
12
+ description : Pre-commit version to install
13
+ default : 4.2.0
11
14
rust :
12
15
description : Whether to install the Rust toolchain (and which version to use)
13
16
rust-components :
@@ -30,17 +33,61 @@ runs:
30
33
uses : actions/setup-python@42375524e23c412d93fb67b49958b491fce71c38 # v5.4.0
31
34
with :
32
35
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 }}
33
78
34
79
- name : Setup Rust Toolchain
35
80
uses : dtolnay/rust-toolchain@c5a29ddb4d9d194e7c84ec8c3fba61b1c31fee8c
36
- if : ${{ inputs.rust }}
81
+ if : ${{ inputs.rust && steps.rust-toolchain-cache.outputs.cache-hit != 'true' }}
37
82
with :
38
83
toolchain : ${{ inputs.rust }}
39
84
components : ${{ inputs.rust-components }}
40
85
41
- - name : Setup Hadolint
86
+ - name : Install Hadolint
42
87
if : ${{ inputs.hadolint }}
43
88
shell : bash
89
+ env :
90
+ HADOLINT_VERSION : ${{ inputs.hadolint }}
44
91
run : |
45
92
set -euo pipefail
46
93
51
98
ARCH=$(uname -m)
52
99
53
100
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"
56
103
57
104
echo "$LOCATION_DIR" | tee -a "$GITHUB_PATH"
58
105
@@ -70,6 +117,10 @@ runs:
70
117
github_access_token : ${{ inputs.nix-github-token }}
71
118
install_url : https://releases.nixos.org/nix/nix-${{ inputs.nix }}/install
72
119
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