deepwzh的技术笔记
  • Golang
  • Python
  • Cpp
杂项
前端开发
  • 计算机网络
  • 操作系统
  • 数据库
云原生
Devops
AIGC
关于作者
  • Golang
  • Python
  • Cpp
杂项
前端开发
  • 计算机网络
  • 操作系统
  • 数据库
云原生
Devops
AIGC
关于作者
  • Devops
  • Tailscale
  • Clash
  • Certbot
  • Prometheus
  • Cloudflare SSL 完全模式配置指南
  • Gitlab

    • Gitlab
    • Release 功能
    • 流水线

      • 流水线
      • 组件
  • Jenkins

    • Jenkins
    • Node
  • 对象存储

    • 对象存储
    • web 工具
    • 常用工具
  • Github

    • Github
    • Pull Request
    • Fork
    • 加速
    • Github Action

      • Github Action
      • 触发方式
      • 缓存 Cache
  • Ioc基础设施即代码

    • Ioc 基础设施即代码
    • gitops
    • Terraform
    • Ansible

缓存 Cache

概述

要缓存作业的依赖项,可以使用 GitHub 的 cache 操作。 该操作创建和还原由唯一键标识的缓存。

详细文档见:https://docs.github.com/zh/actions/writing-workflows/choosing-what-your-workflow-does/caching-dependencies-to-speed-up-workflows#matching-a-cache-key

用法

缓存主要由 cache 操作实现的,还有两个专门用还原和保存缓存的操作。

cache

此作允许缓存依赖项和构建输出,以缩短工作流程执行时间。

  • path: 要缓存的路径,!表示排除
  • key: 缓存的 key

cache/restore

restore 作将还原缓存。它的工作方式与 cache 作类似,不同之处在于它没有用于保存缓存的 post 步骤。此作提供了无需保存缓存即可恢复缓存的精细功能。它接受与 cache 作相同的输入集。

详细文档见:https://github.com/actions/cache/blob/main/restore/README.md

cache/store

save 作保存缓存。它的工作方式与 cache 作类似,不同之处在于它不会先执行还原。此作提供了精细的功能,无需恢复缓存即可保存缓存,或者在工作流作业的任何阶段执行保存 -- 而不仅仅是在后阶段。

详细文档见:https://github.com/actions/cache/blob/main/save/README.md

示例

restore-save 流程

在任务开始载入缓存, 结束后保存缓存

jobs:
  deploy-gh-pages:
    runs-on: ubuntu-latest
    steps:
      # 加载已经存在的缓存
      - name: 加载缓存
        uses: actions/cache/restore@v4
        id: restore-cache
        with:
          path: |
            .
            !.vuepress/dist
            !node_modules
          key: ${{ runner.os }}-project-notes

      # 执行一个具体的任务
      # ...
      
      # 如果缓存没命中,就根据当前内容生成缓存   
      - name: 保存缓存
        if: always() && steps.restore-cache.outputs.cache-hit != 'true'
        uses: actions/cache/save@v4
        with:
          path: |
            .
            !.vuepress/dist
            !node_modules
          key: ${{ steps.restore-cache.outputs.cache-primary-key }}
Prev
触发方式