Skip to content

Commit f433c2a

Browse files
authored
Harden action cache key (#3545)
1 parent f877a58 commit f433c2a

1 file changed

Lines changed: 34 additions & 6 deletions

File tree

action.yml

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,19 +49,22 @@ runs:
4949
with:
5050
python-version: '3.14'
5151

52-
- uses: actions/cache@2c8a9bd7457de244a408f35966fab2fb45fda9c8 # v6.0.0
53-
with:
54-
path: ~/.cache/pip
55-
key: datamodel-codegen-${{ runner.os }}-python-3.14
56-
57-
- name: Install datamodel-code-generator
52+
- name: Resolve install package
53+
id: install-package
5854
env:
5955
ACTION_REF: ${{ github.action_ref }}
6056
INPUT_EXTRAS: ${{ inputs.extras }}
6157
INPUT_VERSION: ${{ inputs.version }}
6258
run: |
6359
set -euo pipefail
6460
61+
case "$INPUT_VERSION" in
62+
*$'\n'*|*$'\r'*)
63+
echo "::error::Version must be a single line"
64+
exit 1
65+
;;
66+
esac
67+
6568
EXTRAS=""
6669
if [ -n "$INPUT_EXTRAS" ]; then
6770
IFS="," read -r -a REQUESTED_EXTRAS <<< "$INPUT_EXTRAS"
@@ -91,6 +94,31 @@ runs:
9194
PACKAGE="${PACKAGE}==${VERSION}"
9295
fi
9396
97+
CACHE_KEY="$(PACKAGE="$PACKAGE" python3 - <<'PY'
98+
import hashlib
99+
import os
100+
101+
print(hashlib.sha256(os.environ["PACKAGE"].encode()).hexdigest())
102+
PY
103+
)"
104+
105+
{
106+
printf 'cache-key=%s\n' "$CACHE_KEY"
107+
printf 'package=%s\n' "$PACKAGE"
108+
} >> "$GITHUB_OUTPUT"
109+
shell: bash
110+
111+
- uses: actions/cache@2c8a9bd7457de244a408f35966fab2fb45fda9c8 # v6.0.0
112+
with:
113+
path: ~/.cache/pip
114+
key: datamodel-codegen-${{ runner.os }}-python-3.14-${{ steps.install-package.outputs.cache-key }}
115+
116+
- name: Install datamodel-code-generator
117+
env:
118+
PACKAGE: ${{ steps.install-package.outputs.package }}
119+
run: |
120+
set -euo pipefail
121+
94122
pip install "$PACKAGE"
95123
shell: bash
96124

0 commit comments

Comments
 (0)