Clear cache in GitHub Actions

﹥>﹥吖頭↗ 提交于 2021-01-21 07:17:33

问题


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 clear all cache. How can I do that?


A part of GHA Workflow I use:
on: push

name: R-CMD-check

jobs:
  R-CMD-check:
    runs-on: ${{ matrix.config.os }}

    name: ${{ matrix.config.os }} (${{ matrix.config.r }})

    strategy:
      fail-fast: false
      matrix:
        config:
          # - {os: windows-latest, r: 'devel'}
          - {os: macOS-latest,   r: 'release'}

    env:
      R_REMOTES_NO_ERRORS_FROM_WARNINGS: true
      RSPM: ${{ matrix.config.rspm }}
      GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

    steps:
      - uses: actions/checkout@v2

      - uses: r-lib/actions/setup-r@master

      - name: Query dependencies
        run: |
          repos <- c("https://r-hyperspec.github.io/hySpc.pkgs/", getOption("repos"))
          saveRDS("remotes::dev_package_deps(dependencies = TRUE)", ".github/depends.Rds", version = 2)
          writeLines(sprintf("R-%i.%i", getRversion()$major, getRversion()$minor), ".github/R-version")
        shell: Rscript {0}

      - name: Cache R packages
        if: runner.os != 'Windows'
        uses: actions/cache@v1
        with:
          path: ${{ env.R_LIBS_USER }}
          key: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-${{ hashFiles('.github/depends.Rds') }}
          restore-keys: ${{ runner.os }}-${{ hashFiles('.github/R-version') }}-1-

      - name: Install dependencies
        run:   remotes::install_deps(dependencies = TRUE)
        shell: Rscript {0}

      - name: Session info
        run: |
          options(width = 100)
          pkgs <- installed.packages()[, "Package"]
          sessioninfo::session_info(pkgs, include_base = TRUE)
        shell: Rscript {0}

回答1:


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, e.g., by adding a version number:

key: ${{ runner.os }}-mycache-v1

Or, if you prefer using the UI, you can abuse secrets:

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



回答2:


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.



回答3:


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



来源:https://stackoverflow.com/questions/63521430/clear-cache-in-github-actions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!