build-push-action icon indicating copy to clipboard operation
build-push-action copied to clipboard

Disable cache-to for pull requests

Open felipecrs opened this issue 2 years ago • 7 comments

Hi,

I would like to disable cache publishing for pull requests, something like:

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          cache-from: type=gha
          cache-to: type=gha,mode=max,enable=${{ github.event_name == 'push' }}

But enable flags aren't apparently supported for cache-to according to the docs.

Is there any chance it can be supported?

felipecrs avatar Mar 23 '23 12:03 felipecrs

Is there any chance it can be supported?

I don't think we are going to add this kind of attribute but I'm curious about your use case to disable cache on PR?

crazy-max avatar Apr 07 '23 14:04 crazy-max

I have a project that produces big images, and I would not like PRs to consume all the cache quota from merged commits. If they do, they would start deleting the cache from merged commits which means later PRs won't have cache to consume from.

felipecrs avatar Apr 07 '23 20:04 felipecrs

I'm running into the same issue, PR's are producing several layers combined +1GB which is taking all the Cache quota

gaby avatar Apr 17 '23 01:04 gaby

@felipecrs Have you found a solution for this yet?

gaby avatar Apr 17 '23 03:04 gaby

One dumb thing you can do is to repeat the build-push-action call, but add if conditions to both to ensure one will run for pull requests and another for push or the rest.

felipecrs avatar Apr 17 '23 03:04 felipecrs

@felipecrs Clever, I will use that in the meantime!

gaby avatar Apr 17 '23 03:04 gaby

The expressions return the truthy value. Caching-to is disabled when it's empty.

Expression example executed in a JavaScript console:

>> true && 'type=gha,mode=max' || ''
<< "type=gha,mode=max"
>> false && 'type=gha,mode=max' || ''
<< "" 

So the following yml works. I use something similar.

      - name: Build and push
        uses: docker/build-push-action@v4
        with:
          context: .
          cache-from: type=gha
          cache-to: ${{ github.event_name == 'push' && 'type=gha,mode=max' || '' }}

robkorv avatar Apr 18 '23 07:04 robkorv