diff --git a/actions/restore-assets/action.yml b/actions/restore-assets/action.yml index a59eb81..db058a6 100644 --- a/actions/restore-assets/action.yml +++ b/actions/restore-assets/action.yml @@ -5,12 +5,15 @@ inputs: frontend-path: description: 'Path to frontend directory' required: true + github-token: + description: 'GitHub token for API access (used to compute cache key from remote SHAs)' + required: true use-artifacts: description: 'Whether to download from artifacts (requires cache job to have run first)' required: false default: 'true' cache-version: - description: 'Cache version (must match sync-assets for cache hits)' + description: 'Cache version fallback when remote SHA lookup fails' required: false default: 'v1' @@ -25,15 +28,33 @@ outputs: runs: using: 'composite' steps: + - name: Compute asset cache key from remote SHAs + id: asset-hash + shell: bash + env: + FALLBACK_VERSION: ${{ inputs.cache-version }} + run: | + AUTH_HEADER="Authorization: Bearer ${{ inputs.github-token }}" + LOGOS_SHA=$(curl -sf -H "$AUTH_HEADER" -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/mempool/mining-pool-logos/commits/HEAD" 2>/dev/null | jq -r '.sha // empty') || true + PROMO_SHA=$(curl -sf -H "$AUTH_HEADER" -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/mempool/mempool-promo/commits/HEAD" 2>/dev/null | jq -r '.sha // empty') || true + if [[ -n "$LOGOS_SHA" && -n "$PROMO_SHA" ]]; then + HASH=$(echo -n "${LOGOS_SHA}${PROMO_SHA}" | sha256sum | cut -c1-12) + else + HASH="$FALLBACK_VERSION" + fi + echo "hash=$HASH" >> $GITHUB_OUTPUT + - name: Restore cached mining pool assets continue-on-error: true id: cache-mining-pool-restore uses: actions/cache/restore@v4 with: path: mining-pool-assets.zip - key: mining-pool-assets-${{ inputs.cache-version }} + key: mining-pool-assets-${{ steps.asset-hash.outputs.hash }} restore-keys: | - mining-pool-assets-${{ inputs.cache-version }}- + mining-pool-assets-${{ steps.asset-hash.outputs.hash }}- - name: Restore cached promo video assets continue-on-error: true @@ -41,9 +62,9 @@ runs: uses: actions/cache/restore@v4 with: path: promo-video-assets.zip - key: promo-video-assets-${{ inputs.cache-version }} + key: promo-video-assets-${{ steps.asset-hash.outputs.hash }} restore-keys: | - promo-video-assets-${{ inputs.cache-version }}- + promo-video-assets-${{ steps.asset-hash.outputs.hash }}- - name: Download mining pool artifact if: fromJSON(inputs.use-artifacts) diff --git a/actions/sync-assets/action.yml b/actions/sync-assets/action.yml index 1b25e5b..9a604f5 100644 --- a/actions/sync-assets/action.yml +++ b/actions/sync-assets/action.yml @@ -13,22 +13,45 @@ inputs: required: false default: 'sync-assets-dev' cache-version: - description: 'Cache version for manual invalidation (bump to force fresh sync)' + description: 'Cache version fallback when remote SHA lookup fails' required: false default: 'v1' +outputs: + cache-key-hash: + description: 'Hash used for cache keys (from remote SHAs or cache-version fallback)' + value: ${{ steps.asset-hash.outputs.hash }} + runs: using: 'composite' steps: + - name: Compute asset cache key from remote SHAs + id: asset-hash + shell: bash + env: + FALLBACK_VERSION: ${{ inputs.cache-version }} + run: | + AUTH_HEADER="Authorization: Bearer ${{ inputs.github-token }}" + LOGOS_SHA=$(curl -sf -H "$AUTH_HEADER" -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/mempool/mining-pool-logos/commits/HEAD" 2>/dev/null | jq -r '.sha // empty') || true + PROMO_SHA=$(curl -sf -H "$AUTH_HEADER" -H "X-GitHub-Api-Version: 2022-11-28" \ + "https://api.github.com/repos/mempool/mempool-promo/commits/HEAD" 2>/dev/null | jq -r '.sha // empty') || true + if [[ -n "$LOGOS_SHA" && -n "$PROMO_SHA" ]]; then + HASH=$(echo -n "${LOGOS_SHA}${PROMO_SHA}" | sha256sum | cut -c1-12) + else + HASH="$FALLBACK_VERSION" + fi + echo "hash=$HASH" >> $GITHUB_OUTPUT + # Cache miss is success with cache-hit=false; only real errors (network, permissions) fail the step. - name: Restore cached mining pool assets id: cache-mining-pool-restore uses: actions/cache/restore@v4 with: path: mining-pool-assets.zip - key: mining-pool-assets-${{ inputs.cache-version }} + key: mining-pool-assets-${{ steps.asset-hash.outputs.hash }} restore-keys: | - mining-pool-assets-${{ inputs.cache-version }}- + mining-pool-assets-${{ steps.asset-hash.outputs.hash }}- # Cache miss is success with cache-hit=false; only real errors (network, permissions) fail the step. - name: Restore cached promo video assets @@ -36,9 +59,9 @@ runs: uses: actions/cache/restore@v4 with: path: promo-video-assets.zip - key: promo-video-assets-${{ inputs.cache-version }} + key: promo-video-assets-${{ steps.asset-hash.outputs.hash }} restore-keys: | - promo-video-assets-${{ inputs.cache-version }}- + promo-video-assets-${{ steps.asset-hash.outputs.hash }}- - name: Unzip mining pool assets before sync continue-on-error: true @@ -100,11 +123,11 @@ runs: uses: actions/cache/save@v4 with: path: mining-pool-assets.zip - key: mining-pool-assets-${{ inputs.cache-version }} + key: mining-pool-assets-${{ steps.asset-hash.outputs.hash }} - name: Save promo video assets cache if: steps.cache-promo-video-restore.outputs.cache-hit != 'true' uses: actions/cache/save@v4 with: path: promo-video-assets.zip - key: promo-video-assets-${{ inputs.cache-version }} + key: promo-video-assets-${{ steps.asset-hash.outputs.hash }}