Clear cache in GitHub Actions

后端 未结 3 2316
悲哀的现实
悲哀的现实 2021-02-19 22:05

I am working on an R package and using GitHub Action (GHA) as a Continuous Integration (CI) provider. I cache R packages (dependencies) by using actions/cache. And now I want to

相关标签:
3条回答
  • 2021-02-19 22:32

    @GegznaV you can use something like tmate, and manually clear cache by sshing into a runner.

    0 讨论(0)
  • 2021-02-19 22:34

    As pointed out in the corresponding issue, there is currently no native solution to clear the cache.

    However, there are two practical workarounds to use a new cache. This is not exactly the same as clearing the current cache, but it does the job.

    In order to do so, you have to change the cache key (and any restore-keys). Because if the key(s) is/are different, this is considered a cache miss and you start with a new one.

    You can change the cache key either by modifying the workflow file directly, e.g., by adding a version number:

    key: ${{ runner.os }}-mycache-v1-${{ hashFiles(...) }}
    

    If you now want to use a new cache, all you have to do is to commit a different version number:

    key: ${{ runner.os }}-mycache-v2-${{ hashFiles(...) }}
    

    If you don't want to modify the workflow file and prefer using the UI, you can abuse secrets:

    key: ${{ runner.os }}-mycache-${{ secrets.CACHE_VERSION }}-${{ hashFiles(...) }}
    

    Whenever the secret changes, a new cache will be used.

    0 讨论(0)
  • 2021-02-19 22:35

    You cannot force a clear cache currently and it seems there is an open feature request for it at the moment https://github.com/actions/cache/issues/2. If I were you, I would post the request there as well so that they know that more people want the feature implemented.

    A few things to note about the action:

    There are not parameters in the action and not even in the toolkit package that this action is build on top of.

    Getting deep into the toolkit code they use a cache api url to do all the good stuff. This means we don't even know if that api supports it with the assumption that we try to test it out and see what else it provides by hitting it directly. Here is the line for the api call for which the base url is taken from env ACTIONS_CACHE_URL

    https://github.com/actions/toolkit/blob/c2bc747506bf562195a02bd4fdb1ff2a95d8b7ed/packages/cache/src/internal/cacheHttpClient.ts#L44

    npm package as reference https://www.npmjs.com/package/@actions/cache

    If we take a step back for a moment and go back to the github docs now that we have looked deep into the action/cache code and how it works,

    Per the github docs https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows

    There are two things to note,

    Once you create a cache, you cannot change the contents of an existing 
    cache but you can create a new cache with a new key.
    
    GitHub will remove any cache entries that have not been accessed in over 7 days.
    
    0 讨论(0)
提交回复
热议问题