How do you ensure that the correct version of .NET Core SDK is installed for your Azure Pipeline builds? You could install it manually on your build agents, but wouldn’t it be better to automate it?

The .NET Core Tool Installer Task can be used for this, and if you don’t change the version of the SDK that you require frequently, very often that’s enough.

When you’re developing .NET Core applications, you can indicate which version of the SDK you require to build with by using a global.json file.

Unfortunately, the Installer Task doesn’t currently know about global.json, so you might feel like you’re doubling up - specifying the required version not only in that file but also in the configuration of the Installer Task. Don’t do that!

With a bit of PowerShell, that can be done.. Create a PowerShell Task that runs before the Installer task and use it to set a variable that can then be used by the following task(s).

If you’re using YAML, the task definitions would look something like this:


steps:
- powershell: |
    $PathToGlobalJson = Join-Path -Path $Env:BUILD_SOURCESDIRECTORY -ChildPath "global.json"
    $GlobalJson = Get-Content -Raw -Path  $PathToGlobalJson | ConvertFrom-Json
    $Version= $GlobalJson.sdk.version

    Write-Host ("##vso[task.setvariable variable=SdkVersion;]$Version")
  displayName: 'Parse global.json'

- task: DotNetCoreInstaller@0
  displayName: 'Use .NET Core sdk $(SdkVersion)'
  inputs:
    version: '$(SdkVersion)'