AzureDevOps 管道:DotNetCoreCLI 'restore' 忽略 Directory.Build.props 文件

AzureDevOps pipeline: DotNetCoreCLI 'restore' ignores Directory.Build.props file

我们正在从 NetFramework 项目切换到具有多个 TargetFrameworks 的 NetCore 项目。如果 TargetFramework 包含为 net40 或 net35 编写的代码,则这需要一个 Directory.Build.Props 文件。 Directory.Build.Props 中提到的 NuGet 包来自 NuGet.org.

由于 NuGetCommand 是为 NetFramework 构建的,我们正尝试在 yaml 管道中从使用 NuGetCommand 切换到 DotNetCoreCLI。但是,DotNetCoreCLI 'restore' 似乎不包含 Directory.Build.props 中的 NuGet 包。恢复任务有效,但构建任务失败并出现以下错误:

  Determining projects to restore...
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
  Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net20.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net20/1.0.0/microsoft.netframework.referenceassemblies.net20.1.0.0.nupkg'.
  Response status code does not indicate success: 401 (Unauthorized).
##[error]C:\Program Files\dotnet\sdk.0.202\NuGet.targets(131,5): Error : Failed to download package 'Microsoft.NETFramework.ReferenceAssemblies.net40.1.0.0' from 'https://pkgs.dev.azure.com/organizationName/34aabd4f-e198-4449-8f63-4bc7afc96f97/_packaging/a5bd5a70-fb61-4e6a-b586-6828474df9df/nuget/v3/flat2/microsoft.netframework.referenceassemblies.net40/1.0.0/microsoft.netframework.referenceassemblies.net40.1.0.0.nupkg'.
Response status code does not indicate success: 401 (Unauthorized).

NuGetCommand:

- task: NuGetCommand@2
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

DotNetCoreCLI

- task: DotNetCoreCLI@2
  displayName: DotNetCoreCLI NuGet restore
  inputs:
    command: 'restore'
    projects: '**/*.csproj'
    feedsToUse: 'config'
    nugetConfigPath: 'NuGet.config'

NuGet.Config包括以下内容

  <packageSources>
    ...
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json"/>
  </packageSources>

Directory.Build.props

<Project>
  <PropertyGroup>
    <LangVersion>latest</LangVersion>
  </PropertyGroup>
  <ItemGroup Condition="'$(TargetFrameworkIdentifier)' == '.NETFramework'">
    <PackageReference Include="Microsoft.NETFramework.ReferenceAssemblies" Version="1.0.0" PrivateAssets="all" />
    <PackageReference Include="jnm2.ReferenceAssemblies.net35" Version="1.0.1" PrivateAssets="all" />
  </ItemGroup>
</Project>

请在您的 dotnet 构建任务中使用 --no-restore 选项来禁用隐式还原。

这里是 document:dotnet restore 命令在某些显式恢复有意义的场景中仍然有用,例如 Azure DevOps 服务中的持续集成构建或需要显式控制何时恢复的构建系统发生恢复。

此外,dotnet restore 内部使用了与 .NET Core SDK 打包在一起的 NuGet.exe 版本。 dotnet restore 只能恢复 .NET Core 项目 .csproj 文件中指定的包。如果您的解决方案中还有 Microsoft .NET Framework 项目或使用 package.json 指定依赖项,则还必须使用 NuGet 任务来恢复这些依赖项。 请参考this document.