From acf931c4e0e658c15978895a05d562b04babf566 Mon Sep 17 00:00:00 2001 From: Enrico Minack Date: Thu, 16 Mar 2023 19:15:09 +0100 Subject: [PATCH] Split cache step into two, hash composite requirements.txt (#422) --- composite/action.yml | 15 ++++++++++++--- python/test/test_action_yml.py | 13 +++++++++++++ 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/composite/action.yml b/composite/action.yml index 1cc72b06..54bf884f 100644 --- a/composite/action.yml +++ b/composite/action.yml @@ -175,12 +175,13 @@ runs: esac shell: bash - - name: Cache PIP Packages - uses: actions/cache@v3 + - name: Restore PIP packages cache + uses: actions/cache/restore@v3 + id: cache continue-on-error: true with: path: ${{ steps.os.outputs.pip-cache }} - key: enricomi-publish-action-${{ runner.os }}-${{ runner.arch }}-pip-${{ steps.python.outputs.version }}-${{ hashFiles('**/requirements.txt', 'composite/action.yml') }} + key: enricomi-publish-action-${{ runner.os }}-${{ runner.arch }}-pip-${{ steps.python.outputs.version }}-f3f2c295046c91ed612b4efb6c9fb352 - name: Install Python dependencies run: | @@ -238,6 +239,14 @@ runs: LOG_LEVEL: ${{ inputs.log_level }} shell: bash + - name: Save PIP packages cache + uses: actions/cache/save@v3 + if: ( success() || failure() ) && ! steps.cache.outputs.cache-hit + continue-on-error: true + with: + path: ${{ steps.os.outputs.pip-cache }} + key: ${{ steps.cache.outputs.cache-primary-key }} + branding: icon: 'check-circle' color: 'green' diff --git a/python/test/test_action_yml.py b/python/test/test_action_yml.py index b8fc9cec..d700f27b 100644 --- a/python/test/test_action_yml.py +++ b/python/test/test_action_yml.py @@ -1,4 +1,6 @@ +import hashlib import pathlib +import sys import unittest import yaml @@ -28,6 +30,17 @@ def test_composite_action(self): self.assertEqual(dockerfile_action_wo_runs, composite_action_wo_runs) self.assertIn(('using', 'composite'), composite_action.get('runs', {}).items()) + # check cache key hash is up-to-date in composite action + # this md5 is linux-based (on Windows, git uses different newlines, which changes the hash) + if sys.platform != 'win32': + with open(project_root / 'python' / 'requirements.txt', mode='rb') as r: + expected_hash = hashlib.md5(r.read()).hexdigest() + cache_hash = next(step.get('with', {}).get('key', '').split('-')[-1] + for step in composite_action.get('runs', {}).get('steps', []) + if step.get('uses', '').startswith('actions/cache/restore@')) + self.assertEqual(expected_hash, cache_hash, msg='Changing python/requirements.txt requires ' + 'to update the MD5 hash in composite/action.yaml') + def test_composite_inputs(self): with open(project_root / 'composite/action.yml', encoding='utf-8') as r: action = yaml.safe_load(r)