Azure 构建管道 NuGet 还原错误 NETSDK1045

Posted

技术标签:

【中文标题】Azure 构建管道 NuGet 还原错误 NETSDK1045【英文标题】:Azure build pipeline NuGet Restore error NETSDK1045 【发布时间】:2020-05-27 18:02:28 【问题描述】:

我通过 Visual Studio 2019 创建了一个 Azure CI 管道。项目目标框架是 .net core 3.1。但是,在 NuGet 还原期间它会失败。 我的计算机上安装了 .Net Core 2.2.2017 和 3.1.101,我的 Visual Studio 2019 社区版本是 16.4。我的环境变量路径已经到位。

我还尝试手动创建一个使用 .net core 3.1 LTS 堆栈的应用服务,但我仍然遇到同样的错误。

    ##[error]The nuget command failed with exit code(1) and error(C:\Program Files\dotnet\sdk\2.2.110\Sdks\Microsoft.NET.Sdk\targets\Microsoft.NET.TargetFrameworkInference.targets(137,5):

    error NETSDK1045: The current .NET SDK does not support targeting .NET Core 3.1.  Either target .NET Core 2.2 or lower, or use a version of the .NET SDK that supports .NET Core 3.1. 

这是 Visual Studio 生成的 YAML 文件

pool:
  name: Hosted VS2017
  demands:
  - msbuild
  - visualstudio
  - vstest

steps:
- task: NuGetToolInstaller@1
  displayName: 'Use NuGet 5.0.0'
  inputs:
    versionSpec: 5.0.0

- task: NuGetCommand@2
  displayName: 'NuGet restore'
  inputs:
    restoreSolution: '$(Parameters.solution)'

- task: VSBuild@1
  displayName: 'Build solution'
  inputs:
    solution: '$(Parameters.solution)'
    msbuildArgs: '/p:DeployOnBuild=true /p:WebPublishMethod=Package /p:PackageAsSingleFile=true /p:SkipInvalidConfigurations=true /p:PackageLocation="$(build.artifactstagingdirectory)\\"'
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- task: VSTest@2
  displayName: 'Test Assemblies'
  inputs:
    testAssemblyVer2: |
     **\$(BuildConfiguration)\*test*.dll
     !**\obj\**
    platform: '$(BuildPlatform)'
    configuration: '$(BuildConfiguration)'

- task: PublishSymbols@1
  displayName: 'Publish symbols path'
  inputs:
    SearchPattern: '**\bin\**\*.pdb'
  continueOnError: true

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
    ArtifactName: '$(Parameters.ArtifactName)'


【问题讨论】:

【参考方案1】:

我相信,你不需要安装 NuGetToolInstaller 和 vsbuild,你应该先安装 DotNetCoreCLI 之后你可以使用它. Here is manual你的 yaml 文件应该是什么样子。

【讨论】:

我不明白为什么 Visual Studio 提供的模板不适用于构建管道。但是从 IDE 发布并不痛苦,但缺点是它不是自动化的。【参考方案2】:

通过 IDE 发布没有痛苦,因为它使用您在机器上安装的工具,在管道中您需要指定要使用的工具,这就是痛苦。

对于那些不使用基于 YAML 的新管道而是在 devops 上使用经典编辑器的用户,您可能需要添加 dot net core add task 并将 nuget restore 更改为 dotnet restore task

【讨论】:

【参考方案3】:

当我从 Azure Devops UI 创建管道时,我的 YAML 如下所示。它使用DotNetCoreCLI 而不是VSBuild。请注意,它使用Restore 命令,而不是与 Nuget 相关的任何内容。

pool:
  name: Azure Pipelines
#Your build pipeline references an undefined variable named ‘Parameters.RestoreBuildProjects’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
#Your build pipeline references an undefined variable named ‘Parameters.RestoreBuildProjects’. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab. See https://go.microsoft.com/fwlink/?linkid=865972
#Your build pipeline references the ‘BuildConfiguration’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971
#Your build pipeline references the ‘BuildConfiguration’ variable, which you’ve selected to be settable at queue time. Create or edit the build pipeline for this YAML file, define the variable on the Variables tab, and then select the option to make it settable at queue time. See https://go.microsoft.com/fwlink/?linkid=865971

steps:
- task: DotNetCoreCLI@2
  displayName: Restore
  inputs:
    command: restore
    projects: '$(Parameters.RestoreBuildProjects)'

- task: DotNetCoreCLI@2
  displayName: Build
  inputs:
    projects: '$(Parameters.RestoreBuildProjects)'
    arguments: '--configuration $(BuildConfiguration)'

- task: DotNetCoreCLI@2
  displayName: Publish
  inputs:
    command: publish
    publishWebProjects: True
    arguments: '--configuration $(BuildConfiguration) --output $(build.artifactstagingdirectory)'
    zipAfterPublish: True

- task: PublishBuildArtifacts@1
  displayName: 'Publish Artifact'
  inputs:
    PathtoPublish: '$(build.artifactstagingdirectory)'
  condition: succeededOrFailed()

【讨论】:

以上是关于Azure 构建管道 NuGet 还原错误 NETSDK1045的主要内容,如果未能解决你的问题,请参考以下文章

Azure Pipelines Nuget 还原失败 MSB4226

Azure DevOps 管道中的问题在 .net 6 中恢复 AutoMapper NuGet 包

Azure Devops 找不到恢复的 nuget 包

Azure DevOps 项目管道无法从不同项目中的工件源访问 NuGet 包

构建 .NET 5.0 项目 Azure DevOps 管道

如何修复不匹配的 Azure 管道 yaml asp.net sdk 版本