falcondev-oss / github-actions-cache-server

Self-hosted GitHub Actions cache server implementation. Compatible with official 'actions/cache' action
https://gha-cache-server.falcondev.io
MIT License
92 stars 4 forks source link

Save actions always uploading the whole cache, even if cache was hit #19

Closed oprypkhantc closed 4 months ago

oprypkhantc commented 4 months ago

Hey.

Whenever you run actions/cache@v4 on GitHub's runners, it'll skip the "save" (upload) part of the action if the cache was initially hit. When using a self-hosted runner with this server, it always uploads the whole cache, regardless of whether it was hit or not.

This is a job I used to test this:

test_job:
  runs-on: ubuntu-latest
  container:
    image: composer:2.6.4
    env:
      ACTIONS_CACHE_URL: ${{ env.ACTIONS_CACHE_URL }}
  steps:
    - uses: actions/checkout@v4
    - uses: actions/cache@v4
      with:
        key: ${{ github.event.repository.name }}/php-cs-fixer-staging-v2
        path: |
          backend/.php_cs.cache
    - name: Test
      run: |
        mkdir -p backend
        cat backend/.php_cs.cache || touch backend/.php_cs.cache

If you run it on ubuntu-latest with debug logging, you get these logs/results:

// Restore action

##[debug]Cache Result:
##[debug]{"scope":"refs/heads/test-runner-cache","cacheKey":"repo/php-cs-fixer-staging-v2","cacheVersion":"b831c729558a3d855f049cb5d8ce4768e04b87664c7502a4682019027646a620","creationTime":"2024-03-20T13:15:51.0033333Z","archiveLocation":"***"}
Cache restored successfully
Cache restored from key: repo/php-cs-fixer-staging-v2
##[debug]Save intra-action state CACHE_KEY = repo/php-cs-fixer-staging-v2
##[debug]Save intra-action state CACHE_RESULT = repo/php-cs-fixer-staging-v2
##[debug]Set output cache-hit = true

// Save action

##[debug]Cache state/key: repo/php-cs-fixer-staging-v2
Cache hit occurred on the primary key repo/php-cs-fixer-staging-v2, not saving cache.

If you instead run on a self-hosted runner, you'll get these:

// Restore action

##[debug]Cache Result:
##[debug]{"archiveLocation":"***","cacheKey":"4889745308"}
Cache restored successfully
Cache restored from key: 4889745308
##[debug]Save intra-action state CACHE_KEY = repo/php-cs-fixer-staging-v2
##[debug]Save intra-action state CACHE_RESULT = 4889745308
##[debug]Set output cache-hit = false

// Save action

##[debug]Cache state/key: 4889745308
Cache saved successfully
Cache saved with key: repo/php-cs-fixer-staging-v2

As you can see, the returned cache key is different, which makes actions/cache think the cache was not hit and re-upload the same cache again.

oprypkhantc commented 4 months ago

It turns out there are master and dev branches, and there's a commit which sounds like it should fix the issue: https://github.com/falcondev-oss/github-actions-cache-server/commit/09daaff3318cbfc69f3e70c4819b9cdf3f855046

Should I wait for the next release or is this not related?

LouisHaftmann commented 4 months ago

Thanks for reporting this issue! We are aware that our current key matching algorithm does not 100% reflect how github's cache server handles keys. The commit you mentioned will be part of the 1.0.0 release which should fix your issue. I'll try on my local setup whether our new key matching algorithm fixes your issue or we still need to improve it.

LouisHaftmann commented 4 months ago

I just tested it locally and it should now work as expected.

I also published a pre-release: ghcr.io/falcondev-oss/github-actions-cache-server:1.0.0-rc1 If you want to try it out keep in mind that you might need to update your setup (it should actually be simpler than before). See our WIP docs: https://dev.github-actions-cache-server.pages.dev/getting-started

oprypkhantc commented 4 months ago

Got it. Iʼll try the pre-release. Thank you for this awesome repo 💛