Skip to content

Commit

Permalink
Split cache step into two, hash composite requirements.txt (#422)
Browse files Browse the repository at this point in the history
  • Loading branch information
EnricoMi authored Mar 16, 2023
1 parent 2c22e15 commit acf931c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
15 changes: 12 additions & 3 deletions composite/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down Expand Up @@ -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'
13 changes: 13 additions & 0 deletions python/test/test_action_yml.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import hashlib
import pathlib
import sys
import unittest

import yaml
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit acf931c

Please sign in to comment.