We faced this same issue in upgrading to .NET 7 on our self hosted agents, and despite adding the UseDotNet@2 as described by silent, we still ended up with failures referencing various 6.0.x SDK targets.
The issue ended up being resolved through this answer. It was due to a previous install of .NET 6 and the variable MSBuildSDKsPath. If you have self hosted agents, check the "Capabilities" of the agent to show the variable itself.
Failing that, check the value of that through the pipeline itself with a step like;
- script: set
displayName: show all env vars
An example snippet of our ci-pipeline.yaml that worked is as follows. Important notes are MSBuildSDKsPath overriding whatever environment variable is set on the agent to reflect the location of the newly installed .NET 7 SDK;
trigger:
branches:
include:
- '*'
exclude:
- develop
- main
variables:
- name: solution
value: '**/*.sln'
- name: BuildConfiguration
value: 'Release'
- name: MSBuildSDKsPath
value: C:\agent\_work\_tool\dotnet\sdk\7.0.102\Sdks
jobs:
- job: Build
pool:
vmImage: 'windows-latest'
name: 'Modern'
workspace:
clean: all
steps:
- script: set
displayName: show all env vars
- checkout: self
persistCredentials: true
- task: UseDotNet@2
displayName: 'Use .NET 7.x'
inputs:
packageType: 'sdk'
version: 7.x
performMultiLevelLookup: true
includePreviewVersions: false
installationPath: $(Agent.ToolsDirectory)/dotnet