如何在 GitHub Actions 中缓存 dotnet 安装

Abu*_*iaz 0 .net continuous-integration caching github-actions

我的 CI 管道中有两个步骤。一是缓存dotnet的安装路径,二是dotnet安装。并使用 windows-2019 图像。但系统永远无法识别 .net 7 可用,它始终安装了 .net 6.0。缓存还显示缓存了 200MB,但可能某些 PATH 变量需要调整。有人可以帮我弄这个吗?

    runs-on: windows-2019

    env:
      DOTNET_INSTALL_DIR: '.\.dotnet'
      DOTNET_ROOT: '.\.dotnet'
Run Code Online (Sandbox Code Playgroud)

我的缓存步骤是:

      - name: Cache dotnet
        id: cache-dotnet-core # id used in cache-hit condition
        uses: actions/cache@v3
        with:
          path: '.\.dotnet' #~/.dotnet
          key: ${{ runner.os }}-dotnet-${{ hashFiles('**/project.assets.json') }}
          restore-keys: ${{ runner.os }}-dotnet-${{ hashFiles('**/project.assets.json') }}
Run Code Online (Sandbox Code Playgroud)

我的 dotnet 安装步骤是

      - name: Install Dotnet
        #if: steps.cache-dotnet-core.outputs.cache-hit != 'true' # condition to check if old installation is available in cache
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x
Run Code Online (Sandbox Code Playgroud)

请记住,保存缓存后,第二次运行作业将显示 .net 6.0 已安装,而 7.0 应该在那里。

这是完整的工作流程:

      - name: Install Dotnet
        #if: steps.cache-dotnet-core.outputs.cache-hit != 'true' # condition to check if old installation is available in cache
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x
Run Code Online (Sandbox Code Playgroud)

Aze*_*eem 5

一种解决方案可能是使用dotnet安装和缓存可执行文件的绝对路径,即DOTNET_INSTALL_DIR在安装、缓存以及dotnet调用命令的任何位置使用前缀全文。

或者,如果要在没有前缀的情况下使用它,可以在使用命令之前DOTNET_INSTALL_DIR添加,即:GITHUB_PATHdotnet

echo "${{ env.DOTNET_INSTALL_DIR }}" | Out-File -FilePath $ENV:GITHUB_PATH -Encoding utf8 -Append
Run Code Online (Sandbox Code Playgroud)

这是完整的工作流程GITHUB_PATH经过测试):

echo "${{ env.DOTNET_INSTALL_DIR }}" | Out-File -FilePath $ENV:GITHUB_PATH -Encoding utf8 -Append
Run Code Online (Sandbox Code Playgroud)

输出

跑步

有关更多详细信息,请参阅环境变量。actions/setup-dotnet


除此之外,从这个问题来看, https://github.com/actions/setup-dotnet操作本身似乎存在缓存,然后被禁用。您可能想要跟踪这一点,以防万一。