清除 GitHub 操作中的缓存

Geg*_*naV 26 caching github-actions

我正在开发 R 包并使用 GitHub Action (GHA) 作为持续集成 (CI) 提供程序。我使用actions/cache. 现在我想清除所有缓存。我怎样才能做到这一点?


我使用的 GHA 工作流程的一部分:
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}
Run Code Online (Sandbox Code Playgroud)

bea*_*u13 29

正如相应问题中所指出的,目前没有清除缓存的本机解决方案。

但是,有两种实用的解决方法可以使用新缓存。这与清除当前缓存并不完全相同,但它可以完成这项工作。

为此,您必须更改缓存key(以及任何restore-keys)。因为如果键不同,这被认为是缓存未命中,您可以从一个新的开始。

您可以通过直接修改工作流文件来更改缓存键,例如,通过添加版本号:

key: ${{ runner.os }}-mycache-v1-${{ hashFiles(...) }}
Run Code Online (Sandbox Code Playgroud)

如果你现在想使用一个新的缓存,你所要做的就是提交一个不同的版本号:

key: ${{ runner.os }}-mycache-v2-${{ hashFiles(...) }}
Run Code Online (Sandbox Code Playgroud)

如果您不想修改工作流文件而更喜欢使用 UI,则可以滥用secrets

key: ${{ runner.os }}-mycache-${{ secrets.CACHE_VERSION }}-${{ hashFiles(...) }}
Run Code Online (Sandbox Code Playgroud)

每当秘密更改时,将使用新的缓存。

  • @beatngu13 这是一个巧妙地利用秘密来破坏缓存的方法!谢谢你! (2认同)

luk*_*kee 19

这也可以通过 GH Actions 来完成。

name: Clear cache

on:
  workflow_dispatch:

permissions:
  actions: write

jobs:
  clear-cache:
    runs-on: ubuntu-latest
    steps:
      - name: Clear cache
        uses: actions/github-script@v6
        with:
          script: |
            console.log("About to clear")
            const caches = await github.rest.actions.getActionsCacheList({
              owner: context.repo.owner,
              repo: context.repo.repo,
            })
            for (const cache of caches.data.actions_caches) {
              console.log(cache)
              github.rest.actions.deleteActionsCacheById({
                owner: context.repo.owner,
                repo: context.repo.repo,
                cache_id: cache.id,
              })
            }
            console.log("Clear completed")
Run Code Online (Sandbox Code Playgroud)

确保它已合并到 HEAD 分支并从操作手动触发它或添加自定义触发器。


Inf*_*ion 11

基于 @beatngu13答案,我创建了 Makefile 目标,从 Github Actions 中删除所有缓存:

GH_REPO ?= ALLATRA-IT/ephyr  # replace with your `org/repo`

# Clear Github Acions usage cache.
#
# Need to install github cli first [https://cli.github.com/] and authorize
#
# Usage:
#   make gh.clear

gh.clear:
    gh api --paginate -H "Accept: application/vnd.github+json" \
        /repos/$(GH_REPO)/actions/caches \
        | for ID in `jq '.actions_caches[].id'`; \
          do echo "Deleting $$ID"; \
             gh api --method DELETE /repos/$(GH_REPO)/actions/caches/$$ID | echo; done
Run Code Online (Sandbox Code Playgroud)

需要先安装Github CLIjq tool工具。安装 Github CLI 后,您需要授权:

gh auth login
Run Code Online (Sandbox Code Playgroud)


Edw*_*ero 8

您目前无法强制清除缓存,目前似乎有一个开放的功能请求https://github.com/actions/cache/issues/2。如果我是你,我也会在那里发布请求,以便他们知道更多人希望实现该功能。

操作中需要注意的几点:

动作中没有参数,甚至在构建此动作的工具包包中也没有。

深入了解工具包代码,他们使用缓存 api url 来做所有好事。这意味着我们甚至不知道该 api 是否支持它,假设我们尝试测试它并通过直接点击它来查看它提供的其他内容。这是 api 调用的行,其基本 url 取自 env ACTIONS_CACHE_URL

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

npm 包作为参考https://www.npmjs.com/package/@actions/cache

如果我们退后一步,回到 github 文档,因为我们已经深入研究了 action/cache 代码及其工作原理,

根据 github 文档https://docs.github.com/en/actions/configuring-and-managing-workflows/caching-dependencies-to-speed-up-workflows

有两点需要注意,

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.
Run Code Online (Sandbox Code Playgroud)
GitHub will remove any cache entries that have not been accessed in over 7 days.
Run Code Online (Sandbox Code Playgroud)


And*_*nar 7

删除操作缓存的另一种简单直接的方法是清除GH 操作缓存。

此操作允许根据上次使用情况和配置的 TTL(默认 - 7 天)自动清除 GitHub Actions 的缓存。

例子:

name: Clear Cache

on:
  schedule:
    - cron: '0 0 * * *' # Runs once a day (https://crontab.guru/once-a-day)
jobs:
  clear:
    name: Clear cache
    runs-on: ubuntu-latest
    steps:
    - uses: MyAlbum/purge-cache@v1
      with:
      max-age: 604800 # Cache max 7 days since last use (this is the default)
Run Code Online (Sandbox Code Playgroud)

上面的工作流程将每天运行一次,并删除 7 天前最后使用的所有缓存。