如何在GitHub Action中缓存docfx安装以提速?
在GitHub Action中缓存docfx无需生成docfx.json
完全可以实现缓存,不需要额外生成docfx.json这类配置文件,核心是缓存docfx的安装文件本身,以下是两种可行方案:
方案1:缓存dotnet全局安装的docfx
如果通过dotnet tool install -g docfx安装docfx,可直接缓存dotnet全局工具目录,步骤如下:
- 配置缓存步骤,指定dotnet工具的存储路径,用操作系统+docfx版本号作为缓存键,确保缓存的兼容性和有效性
- 仅当缓存未命中时,执行安装命令
示例Action代码:
steps: - name: Cache docfx global tool id: cache-docfx uses: actions/cache@v3 with: path: | ~/.dotnet/tools # Linux/macOS路径 C:\Users\runneradmin\.dotnet\tools # Windows路径(actions/cache会自动匹配对应系统路径) key: ${{ runner.os }}-docfx-v2.76.3 - name: Install docfx if cache miss if: steps.cache-docfx.outputs.cache-hit != 'true' run: dotnet tool install -g docfx --version 2.76.3
方案2:缓存预下载的docfx二进制包
若不想用dotnet工具安装,可直接下载docfx二进制文件并缓存:
- 缓存下载解压后的二进制目录,同样用操作系统+版本号做缓存键
- 缓存未命中时,下载对应版本的压缩包并解压
- 将docfx所在目录添加到系统PATH,方便后续调用
示例Action代码:
steps: - name: Cache docfx binary id: cache-docfx-bin uses: actions/cache@v3 with: path: ./docfx-bin key: ${{ runner.os }}-docfx-bin-v2.76.3 - name: Download & extract docfx if cache miss if: steps.cache-docfx-bin.outputs.cache-hit != 'true' run: | # Linux/macOS curl -L https://github.com/dotnet/docfx/releases/download/v2.76.3/docfx-linux-x64.zip -o docfx.zip unzip docfx.zip -d ./docfx-bin # Windows(可单独用if条件区分系统) # Invoke-WebRequest -Uri https://github.com/dotnet/docfx/releases/download/v2.76.3/docfx-win-x64.zip -OutFile docfx.zip # Expand-Archive -Path docfx.zip -DestinationPath ./docfx-bin - name: Add docfx to PATH run: echo "${{ github.workspace }}/docfx-bin" >> $GITHUB_PATH
核心注意事项
- 缓存键必须包含操作系统标识,避免跨系统缓存不兼容
- 固定docfx版本号能让缓存长期有效,若使用最新版,可将安装后提取的版本号加入缓存键(但首次安装仍需耗时)
- 两种方案都不需要生成任何docfx配置文件,仅缓存安装后的可执行文件
内容的提问来源于stack exchange,提问作者CaseyHofland
相关产品推荐
相关产品推荐

