From 44e7c34892eb2551db708f9b08f364d75872a476 Mon Sep 17 00:00:00 2001 From: Damien Retzinger Date: Sat, 9 May 2026 15:37:28 -0400 Subject: [PATCH] refactor(cache-magento): extract key computation into a script (#245) --- .../workflows/_internal-cache-magento.yaml | 23 +++++++++++ cache-magento/action.yml | 12 +++++- cache-magento/compute-cache-keys.sh | 7 ++++ cache-magento/test.sh | 39 +++++++++++++++++++ 4 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 .github/workflows/_internal-cache-magento.yaml create mode 100644 cache-magento/compute-cache-keys.sh create mode 100644 cache-magento/test.sh diff --git a/.github/workflows/_internal-cache-magento.yaml b/.github/workflows/_internal-cache-magento.yaml new file mode 100644 index 0000000..0be6074 --- /dev/null +++ b/.github/workflows/_internal-cache-magento.yaml @@ -0,0 +1,23 @@ +name: Cache Magento Test + +on: + push: + branches: [main] + paths: + - "cache-magento/**" + - ".github/workflows/_internal-cache-magento.yaml" + - "!(**/*.md)" + pull_request: + branches: [main] + paths: + - "cache-magento/**" + - ".github/workflows/_internal-cache-magento.yaml" + - "!(**/*.md)" + +jobs: + unit: + if: "!startsWith(github.head_ref, 'release-please')" + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v6 + - run: bash cache-magento/test.sh diff --git a/cache-magento/action.yml b/cache-magento/action.yml index 575849c..5e7f395 100644 --- a/cache-magento/action.yml +++ b/cache-magento/action.yml @@ -31,11 +31,21 @@ runs: name: Compute Composer Version id: cache-magento-get-composer-version + - name: Compute cache keys + id: cache-magento-keys + shell: bash + run: | + bash "${{ github.action_path }}/compute-cache-keys.sh" \ + "${{ inputs.composer_cache_key }}" \ + "${{ steps.cache-magento-get-php-version.outputs.version }}" \ + "${{ steps.cache-magento-get-composer-version.outputs.version }}" \ + >> $GITHUB_OUTPUT + - name: "Cache Composer Packages" uses: actions/cache@v5 id: cache-magento-cache with: - key: "composer | v5.8 | ${{ inputs.composer_cache_key }} | ${{ steps.cache-magento-get-composer-version.outputs.version }} | ${{ steps.cache-magento-get-php-version.outputs.version }}" + key: ${{ steps.cache-magento-keys.outputs.download-key }} path: ${{ steps.cache-magento-composer-cache.outputs.dir }} branding: diff --git a/cache-magento/compute-cache-keys.sh b/cache-magento/compute-cache-keys.sh new file mode 100644 index 0000000..38ad49f --- /dev/null +++ b/cache-magento/compute-cache-keys.sh @@ -0,0 +1,7 @@ +#!/usr/bin/env bash +# Args: composer_cache_key php_version composer_version +COMPOSER_CACHE_KEY="$1" +PHP_VERSION="$2" +COMPOSER_VERSION="$3" + +echo "download-key=composer | v5.8 | ${COMPOSER_CACHE_KEY} | ${COMPOSER_VERSION} | ${PHP_VERSION}" diff --git a/cache-magento/test.sh b/cache-magento/test.sh new file mode 100644 index 0000000..c8a2e3d --- /dev/null +++ b/cache-magento/test.sh @@ -0,0 +1,39 @@ +#!/usr/bin/env bash + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +SCRIPT="$SCRIPT_DIR/compute-cache-keys.sh" +PASS=0 +FAIL=0 + +assert_eq() { + local label="$1" expected="$2" actual="$3" + if [ "$expected" = "$actual" ]; then + echo "PASS: $label" + PASS=$((PASS + 1)) + else + echo "FAIL: $label" + echo " expected: $expected" + echo " actual: $actual" + FAIL=$((FAIL + 1)) + fi +} + +field() { + echo "$1" | grep "^${2}=" | cut -d= -f2- +} + +# Default cache key +OUT=$(bash "$SCRIPT" "_mageos" "8.3.0" "2.2.6") +assert_eq "default: download-key" \ + "composer | v5.8 | _mageos | 2.2.6 | 8.3.0" \ + "$(field "$OUT" download-key)" + +# Custom composer_cache_key +OUT=$(bash "$SCRIPT" "custom-v2" "8.1.5" "2.4.2") +assert_eq "custom key: download-key" \ + "composer | v5.8 | custom-v2 | 2.4.2 | 8.1.5" \ + "$(field "$OUT" download-key)" + +echo "" +echo "$PASS passed, $FAIL failed" +[ "$FAIL" -eq 0 ]