GitHub Actions Cache 终极指南10个命令行缓存管理技巧【免费下载链接】cacheCache dependencies and build outputs in GitHub Actions项目地址: https://gitcode.com/gh_mirrors/cach/cacheGitHub Actions Cache 是加速 CI/CD 流程的关键工具通过缓存依赖和构建输出显著减少工作流运行时间。本文将分享 10 个实用的命令行缓存管理技巧帮助新手用户轻松掌握缓存配置与优化方法提升 GitHub Actions 工作效率。1. 基础缓存配置快速上手核心命令使用actions/cache动作是实现缓存的基础。最简洁的配置只需指定缓存路径和唯一键- uses: actions/cachev5 with: path: ~/.npm key: ${{ runner.os }}-node-${{ hashFiles(**/package-lock.json) }}核心参数说明path指定需要缓存的目录如~/.npm、node_moduleskey缓存的唯一标识建议包含操作系统和依赖文件哈希如package-lock.json2. 智能缓存键设计避免冗余与冲突有效的缓存键应平衡稳定性和精确性。推荐模式key: ${{ runner.os }}-python-${{ hashFiles(requirements.txt) }} restore-keys: | ${{ runner.os }}-python-设计原则包含操作系统runner.os确保跨平台兼容性使用文件哈希hashFiles跟踪依赖变化通过restore-keys实现模糊匹配优先使用最近缓存3. 分段缓存超时控制解决下载卡死问题缓存下载可能因网络问题卡住v3.0.8 版本支持超时控制- uses: actions/cachev5 with: path: ~/.cache/pip key: ${{ runner.os }}-pip-${{ hashFiles(requirements.txt) }} env: SEGMENT_DOWNLOAD_TIMEOUT_MINS: 5 # 5分钟超时此设置可避免工作流因缓存下载失败而无限挂起。4. 跨分支缓存共享突破默认作用域限制默认缓存仅在当前分支可用通过以下技巧实现跨分支共享- uses: actions/cachev5 with: path: node_modules key: ${{ runner.os }}-node-${{ github.ref refs/heads/main hashFiles(package-lock.json) || }} restore-keys: | ${{ runner.os }}-node-实现原理仅在主分支生成完整缓存键其他分支通过restore-keys复用主分支缓存。5. 跨操作系统缓存一次缓存多平台复用v3.2.3 支持跨 OS 缓存需启用enableCrossOsArchive- uses: actions/cachev5 with: path: ~/.m2/repository key: maven-${{ hashFiles(**/pom.xml) }} enableCrossOsArchive: true注意事项仅缓存平台无关文件如 Java JAR、Python 轮子避免二进制可执行文件。6. 缓存清理自动化释放存储空间使用 GitHub CLI 清理冗余缓存# 列出特定分支缓存 gh cache list --ref feature-branch --limit 100 # 删除指定缓存ID gh cache delete cache-id自动化清理工作流name: Cleanup Caches on: workflow_dispatch: jobs: cleanup: runs-on: ubuntu-latest steps: - run: | cacheKeys$(gh cache list --ref ${{ github.ref_name }} --json id --jq .[].id) for id in $cacheKeys; do gh cache delete $id; done env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}7. 条件式缓存保存失败时也保留缓存默认仅成功构建才保存缓存如需失败时也保存- name: Save cache even on failure uses: actions/cache/savev5 if: always() with: path: ~/.cache key: ${{ runner.os }}-cache-${{ github.sha }}通过if: always()确保缓存操作不受前序步骤失败影响。8. 多路径缓存配置一次缓存多个目录同时缓存多个独立目录- uses: actions/cachev5 with: path: | ~/.npm ~/.cache/pip **/node_modules key: ${{ runner.os }}-multi-cache-${{ hashFiles(**/package-lock.json, **/requirements.txt) }}路径格式使用|分隔多个路径支持通配符**表示递归目录9. 缓存命中判断优化工作流步骤利用cache-hit输出决定是否跳过步骤- id: cache uses: actions/cachev5 with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles(package-lock.json) }} - name: Install dependencies if: steps.cache.outputs.cache-hit ! true run: npm install通过条件判断避免重复安装已缓存的依赖。10. 高级缓存策略分层缓存与版本控制实现多级缓存策略- uses: actions/cachev5 with: path: node_modules key: ${{ runner.os }}-node-${{ hashFiles(package-lock.json) }} restore-keys: | ${{ runner.os }}-node-v1- ${{ runner.os }}-node-版本化缓存在restore-keys中加入版本前缀如v1-便于整体更新缓存策略。总结提升 CI/CD 效率的缓存最佳实践GitHub Actions Cache 是优化工作流性能的利器通过合理设计缓存键、控制缓存作用域、自动化清理等技巧可显著减少构建时间。建议定期检查缓存使用情况结合项目特点调整策略平衡缓存命中率与存储空间占用。掌握这些命令行缓存管理技巧后你的 GitHub Actions 工作流将更加高效、稳定为开发团队节省宝贵时间。【免费下载链接】cacheCache dependencies and build outputs in GitHub Actions项目地址: https://gitcode.com/gh_mirrors/cach/cache创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考