Home > Net >  How to cache dotnet installation in GitHub Actions
How to cache dotnet installation in GitHub Actions

Time:01-23

I have two steps in my CI pipeline. one is to cache the installation path of dotnet and the other is dotnet installation. and using windows-2019 image. but the system never recognizes that .net 7 is available it always has .net 6.0 installed. The cache also shows that 200MB is cached but maybe some PATH variable needs tweaking. Can someone help me with this?

    runs-on: windows-2019

    env:
      DOTNET_INSTALL_DIR: '.\.dotnet'
      DOTNET_ROOT: '.\.dotnet'

My cache step is:

      - 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') }}

and my dotnet installation step is

      - 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

Remember once the cache is saved the 2nd run of the job will show you that .net 6.0 is installed whereas 7.0 should be there.

Here's the complete workflow:

name: dotnet-pipeline
on: [push]

jobs:
  build:
    name: check-dotnet-version
    runs-on: windows-2019 # Win 10 IoT En is not available on github actions

    env:
      DOTNET_INSTALL_DIR: '.\.dotnet'
      DOTNET_ROOT: '.\.dotnet'

    steps:      
      # maintain cache of dotnet installation
      - name: Cache dotnet
        id: cache-dotnet-core
        uses: actions/cache@v3
        with:
          path: '.\.dotnet'
          key: ${{ runner.os }}-dotnet-7
          restore-keys: ${{ runner.os }}-dotnet-7

      # dotnet will only be installed if the files are not found in cache
      - name: Install Dotnet
        if: steps.cache-dotnet-core.outputs.cache-hit != 'true' 
        uses: actions/setup-dotnet@v3
        with:
          dotnet-version: 7.0.x
          
      # validate version number
      - name: validate dotnet version
        run: 'dotnet --version'

CodePudding user response:

Verified that the dotnet command needs to be prefixed with the absolute path where the installed version is cached. Using DOTNET_INSTALL_DIR throughout for installation, cache, and wherever the dotnet command is invoked should work just fine.

Here's the complete workflow (Run

See Environment variables of the actions/setup-dotnet for more details.


Apart from that, from this issue, it looks like there was caching in the https://github.com/actions/setup-dotnet action itself, and then it was disabled. You might want to keep track of this, just in case.

  • Related