From 5f257b9a84f7b126449629e3096e67d4c94c2a88 Mon Sep 17 00:00:00 2001 From: bo0tzz <git@bo0tzz.me> Date: Tue, 18 Feb 2025 12:47:20 +0100 Subject: [PATCH] fix: don't write cache on fork PRs (#16189) --- .github/workflows/docker.yml | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/.github/workflows/docker.yml b/.github/workflows/docker.yml index f2a7afbac2..5429d8671d 100644 --- a/.github/workflows/docker.yml +++ b/.github/workflows/docker.yml @@ -145,7 +145,6 @@ jobs: - name: Login to GitHub Container Registry uses: docker/login-action@v3 - # Skip when PR from a fork if: ${{ !github.event.pull_request.head.repo.fork }} with: registry: ghcr.io @@ -160,6 +159,16 @@ jobs: echo "CACHE_KEY_SUFFIX=$(echo ${{ github.ref_name }} | sed 's/[^a-zA-Z0-9]/-/g')" >> $GITHUB_ENV fi + - name: Generate cache target + id: cache-target + run: | + if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then + # Essentially just ignore the cache output (forks can't write to registry cache) + echo "cache-to=type=local,dest=/tmp/discard,ignore-error=true" >> $GITHUB_OUTPUT + else + echo "cache-to=type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ matrix.device }}-${{ env.CACHE_KEY_SUFFIX }},mode=max,compression=zstd" >> $GITHUB_OUTPUT + fi + - name: Build and push image id: build uses: docker/build-push-action@v6.13.0 @@ -168,7 +177,7 @@ jobs: file: ${{ env.file }} platforms: ${{ matrix.platforms }} labels: ${{ steps.metadata.outputs.labels }} - cache-to: type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ matrix.device }}-${{ env.CACHE_KEY_SUFFIX }},mode=max,compression=zstd + cache-to: ${{ steps.cache-target.outputs.cache-to }} cache-from: | type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ matrix.device }}-${{ env.CACHE_KEY_SUFFIX }} type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ matrix.device }}-main @@ -297,7 +306,6 @@ jobs: - name: Login to GitHub Container Registry uses: docker/login-action@v3 - # Skip when PR from a fork if: ${{ !github.event.pull_request.head.repo.fork }} with: registry: ghcr.io @@ -312,6 +320,16 @@ jobs: echo "CACHE_KEY_SUFFIX=$(echo ${{ github.ref_name }} | sed 's/[^a-zA-Z0-9]/-/g')" >> $GITHUB_ENV fi + - name: Generate cache target + id: cache-target + run: | + if [[ "${{ github.event.pull_request.head.repo.fork }}" == "true" ]]; then + # Essentially just ignore the cache output (forks can't write to registry cache) + echo "cache-to=type=local,dest=/tmp/discard,ignore-error=true" >> $GITHUB_OUTPUT + else + echo "cache-to=type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ matrix.device }}-${{ env.CACHE_KEY_SUFFIX }},mode=max,compression=zstd" >> $GITHUB_OUTPUT + fi + - name: Build and push image id: build uses: docker/build-push-action@v6.13.0 @@ -320,7 +338,7 @@ jobs: file: ${{ env.file }} platforms: ${{ matrix.platform }} labels: ${{ steps.metadata.outputs.labels }} - cache-to: type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ env.CACHE_KEY_SUFFIX }},mode=max,compression=zstd + cache-to: ${{ steps.cache-target.outputs.cache-to }} cache-from: | type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-${{ env.CACHE_KEY_SUFFIX }} type=registry,ref=${{ env.GHCR_REPO }}-build-cache:${{ env.PLATFORM_PAIR }}-main