深入理解actions/cache项目中的缓存策略

深入理解actions/cache项目中的缓存策略

cache Cache dependencies and build outputs in GitHub Actions cache 项目地址: https://gitcode.com/gh_mirrors/cach/cache

前言

在现代软件开发流程中,持续集成(CI)已成为不可或缺的一环。而高效的缓存策略可以显著减少构建时间,提高开发效率。本文将深入探讨actions/cache项目中的各种缓存策略,帮助开发者根据不同的使用场景选择最合适的缓存方案。

缓存键(key)的选择策略

缓存键是缓存机制中的核心概念,它决定了何时创建新缓存以及何时重用现有缓存。以下是几种常见的缓存键策略:

1. 基于依赖文件哈希的缓存策略

当项目依赖发生变化时自动更新缓存是最常见的需求。我们可以使用依赖锁定文件(如package-lock.json)的哈希值作为缓存键:

- uses: actions/cache@v4
  with:
    path: |
      path/to/dependencies
      some/other/dependencies
    key: cache-${{ hashFiles('**/lockfiles') }}

这种策略确保只有当依赖文件内容变化时才会创建新缓存,否则重用现有缓存。

2. 使用恢复键(restore-keys)实现渐进式缓存

当精确匹配的缓存不存在时,可以使用恢复键来查找最接近的匹配:

- uses: actions/cache@v4
  with:
    path: |
      path/to/dependencies
      some/other/dependencies
    key: cache-npm-${{ hashFiles('**/lockfiles') }}
    restore-keys: |
      cache-npm-

恢复键作为前缀匹配,可以找到最近创建的类似缓存,只需下载少量新增依赖,大幅节省构建时间。

3. 按操作系统分离缓存

对于跨平台项目,应为不同操作系统维护独立的缓存:

- uses: actions/cache@v4
  with:
    path: |
      path/to/dependencies
      some/other/dependencies
    key: ${{ runner.os }}-cache

4. 短期缓存策略

对于临时性需求,可以使用工作流运行ID或提交SHA创建短期缓存:

key: cache-${{ github.run_id }}-${{ github.run_attempt }}
# 或
key: cache-${{ github.sha }}

5. 复合键策略

结合多个因素创建更精确的缓存键:

key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

缓存恢复的最佳实践

正确设置路径

路径设置是缓存恢复的关键,不同操作系统和环境的路径差异很大:

  • Ubuntu:

    • 家目录: /home/runner
    • 工作区: /home/runner/work/repo/repo
    • 临时目录: /home/runner/work/_temp
  • Windows:

    • 家目录: C:\Users\runneradmin
    • 工作区: D:\a\repo\repo
    • 临时目录: D:\a\_temp
  • macOS:

    • 家目录: /Users/runner
    • 工作区: /Users/runner/work/repo/repo
    • 临时目录: /Users/runner/work/_temp

只读缓存与集中式缓存

对于需要多任务共享的缓存,可以使用只读模式:

steps:
  - uses: actions/checkout@v4
  - uses: actions/cache/restore@v4
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
  - name: Install Dependencies
    if: steps.cache.outputs.cache-hit != 'true'
    run: /install.sh

精确匹配失败处理

当需要确保只有缓存命中时才继续执行时:

steps:
  - uses: actions/checkout@v4
  - uses: actions/cache/restore@v4
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
  - name: Check cache hit
    if: steps.cache.outputs.cache-hit != 'true'
    run: exit 1

缓存保存的高级技巧

重用恢复阶段的缓存键

避免重复计算缓存键:

- uses: actions/cache/restore@v4
  id: restore-cache
  with:
    path: |
      path/to/dependencies
      some/other/dependencies
    key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}
...
- uses: actions/cache/save@v4
  with:
    path: |
      path/to/dependencies
      some/other/dependencies
    key: ${{ steps.restore-cache.outputs.cache-primary-key }}

动态重新计算缓存键

当依赖文件在构建过程中生成时:

uses: actions/cache/save@v4
with:
    key: npm-cache-${{hashfiles(package-lock.json)}}

多工作流共享缓存

对于多模块项目,可以构建一次父模块并多次复用:

构建并保存父模块:

steps:
  - uses: actions/checkout@v4
  - name: Build
    run: ./build-parent-module.sh
  - uses: actions/cache/save@v4
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

子模块中恢复缓存:

steps:
  - uses: actions/checkout@v4
  - uses: actions/cache/restore@v4
    id: cache
    with:
      path: path/to/dependencies
      key: ${{ runner.os }}-${{ hashFiles('**/lockfiles') }}

总结

actions/cache提供了灵活强大的缓存机制,通过合理选择缓存键策略、正确设置路径以及巧妙运用恢复和保存技巧,可以显著优化CI/CD流程的性能。开发者应根据项目特点和需求,选择最适合的缓存策略组合,在缓存命中率和存储效率之间取得平衡。

cache Cache dependencies and build outputs in GitHub Actions cache 项目地址: https://gitcode.com/gh_mirrors/cach/cache

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

管旭韶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值