Skip to content

The agent that grows with you

fix(ci): keep the setup-pm tools-cache key stable across jobs

Danil Zak806bf594e166
diff --git a/.github/actions/setup-pm/action.yml b/.github/actions/setup-pm/action.yml
index af86ab8643c..787a5bea0c5 100644
--- a/.github/actions/setup-pm/action.yml
+++ b/.github/actions/setup-pm/action.yml
@@ -112,14 +112,16 @@ runs:
# The key covers the PM code and this action, not only the lock: a
# provisioning change must re-run the cold path on every lane rather
- # than restore a store the previous code wrote.
+ # than restore a store the previous code wrote. The prepare step above
+ # imports pm and writes pm/__pycache__; that bytecode embeds the source
+ # mtime of this checkout, so it is excluded or no two jobs share a key.
- name: Cache verified PM tools
id: tools-cache
if: inputs.cache == 'true' && inputs.save-tools-cache == 'true'
uses: actions/cache@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ steps.prepare.outputs.store }}
- key: setup-pm-tools-v1-${{ steps.prepare.outputs.target }}-${{ inputs.toolchain }}${{ steps.prepare.outputs.extra-packages }}-${{ hashFiles('pm/**', '.github/actions/setup-pm/**', 'scripts/ci/setup_toolchain.py') }}-${{ inputs.cache-suffix }}
+ key: setup-pm-tools-v1-${{ steps.prepare.outputs.target }}-${{ inputs.toolchain }}${{ steps.prepare.outputs.extra-packages }}-${{ hashFiles('pm/**', '.github/actions/setup-pm/**', 'scripts/ci/setup_toolchain.py', '!**/__pycache__/**') }}-${{ inputs.cache-suffix }}
- name: Restore verified PM tools without saving
id: tools-cache-restore
@@ -127,7 +129,7 @@ runs:
uses: actions/cache/restore@55cc8345863c7cc4c66a329aec7e433d2d1c52a9 # v6.1.0
with:
path: ${{ steps.prepare.outputs.store }}
- key: setup-pm-tools-v1-${{ steps.prepare.outputs.target }}-${{ inputs.toolchain }}${{ steps.prepare.outputs.extra-packages }}-${{ hashFiles('pm/**', '.github/actions/setup-pm/**', 'scripts/ci/setup_toolchain.py') }}-${{ inputs.cache-suffix }}
+ key: setup-pm-tools-v1-${{ steps.prepare.outputs.target }}-${{ inputs.toolchain }}${{ steps.prepare.outputs.extra-packages }}-${{ hashFiles('pm/**', '.github/actions/setup-pm/**', 'scripts/ci/setup_toolchain.py', '!**/__pycache__/**') }}-${{ inputs.cache-suffix }}
- name: Preserve and retrieve the pinned toolchain
if: inputs.archive-inputs == 'true'
diff --git a/tests/ci/test_setup_pm_tools_cache_key.py b/tests/ci/test_setup_pm_tools_cache_key.py
new file mode 100644
index 00000000000..db5591afd4b
--- /dev/null
+++ b/tests/ci/test_setup_pm_tools_cache_key.py
@@ -0,0 +1,26 @@
+"""setup-pm's tools-cache key must not hash bytecode the prepare step writes."""
+
+import re
+from pathlib import Path
+
+from ruamel.yaml import YAML
+
+ROOT = Path(__file__).resolve().parents[2]
+
+
+def test_tools_cache_key_ignores_pycache_and_matches_restore_key():
+ """prepare imports pm, so pm/__pycache__ exists by the time hashFiles runs.
+
+ Its .pyc files embed per-checkout source mtimes, so hashing them gives every
+ job a new key. Restore-only jobs must look up the key saving jobs write.
+ """
+ action = YAML(typ="base").load(
+ (ROOT / ".github/actions/setup-pm/action.yml").read_text(encoding="utf-8")
+ )
+ steps = {step.get("id"): step for step in action["runs"]["steps"]}
+ save = steps["tools-cache"]["with"]["key"]
+ restore = steps["tools-cache-restore"]["with"]["key"]
+ assert save == restore
+ hashed = re.search(r"hashFiles\(([^)]*)\)", save)
+ assert hashed, save
+ assert "'!**/__pycache__/**'" in hashed.group(1), save