Azure devops 构建管道似乎两次恢复 nuget 包

问题描述

我有一个构建管道,我有一个使用 NuGetCommand 的 Nuget 还原步骤,效果很好。

但是在缺少 nuget 包时执行构建的下一步失败。

BuildOverview

似乎构建步骤试图再次恢复 nuget 包,这不起作用 (它没有这样做的凭据)

BuildLog

构建定义的yaml文件如下:

trigger:
- master

pool:
  vmImage: 'windows-latest'

steps:

- task: copyFiles@2
  displayName: 'copy sql scripts'
  inputs:
    SourceFolder: 'Resources/TestProject/Migrations/sql'
    Contents: '**'
    TargetFolder: '$(build.artifactstagingdirectory)/sqlScripts'

- task: NuGetCommand@2
  displayName: 'NuGet Restore'
  inputs:
    command: 'restore'
    restoreSolution: '**/*.sln'
    FeedsToUse: 'config'
    nugetConfigPath: './nuGet.config'
    externalFeedCredentials: 'TelerikFeed'

- task: DotNetCoreCLI@2
  displayName: 'Build ServiceHost projects'
  inputs:
    command: 'publish'
    publishWebProjects: false
    projects: '**/ServiceHosts/**/*.csproj'
    arguments: '-c Release -r win-x64 --self-contained false -o $(Build.ArtifactStagingDirectory) /p:SourceRevisionId=$(Build.sourceVersion)'
    zipAfterPublish: false

- task: PublishBuildArtifacts@1
  displayName: 'Publish artifacts'
  inputs:
    PathtoPublish: '$(Build.ArtifactStagingDirectory)'
    ArtifactName: 'drop'
    publishLocation: 'Container'

我还有一个 nuGet.config 文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!-- remove any machine-wide sources with <clear/> -->
    <clear />    
    <add key="MyFeed" value="https://pkgs.dev.azure.com/MyCompany/_packaging/NugetFeed/nuget/v3/index.json" />    
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="Telerik_NuGet" value="https://nuget.telerik.com/nuget" />
  </packageSources>
</configuration>

我不明白为什么构建步骤需要再次恢复 nuget 包。

有没有办法将 DotNetCoreCli 指向已经恢复的包?

编辑:

按照@Ibrahim 的建议,我将 --no-restore 添加到 DotNetCoreCLI 任务中。 然后我收到以下错误消息:

C:\程序 Files\dotnet\sdk\5.0.300\Sdks\Microsoft.NET.Sdk\targets\Microsoft.PackageDependencyResolution.targets(241,5): 错误 NETSDK1047:资产文件 'D:\a\1\s\ServiceHosts\TestProject\obj\project.assets.json' 没有 有一个“netcoreapp3.1/win-x64”的目标。确保恢复已运行 并且您已经在 TargetFrameworks 中包含了“netcoreapp3.1” 你的项目。您可能还需要在项目的 运行时标识符。

通过向 yaml 文件添加一个任务来安装最新版本的 nuget 使用

- task: NuGetToolInstaller@1
  inputs:
    versionSpec: 5.9.1

并将 RuntimeIdentifier win-x64 添加到 csproj 文件中。

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <AssemblyName>TestProject</AssemblyName>
    <RootNamespace>TestProject</RootNamespace>
    <RuntimeIdentifier>win-x64</RuntimeIdentifier>
  </PropertyGroup>

解决方法

来自 dotnet documentation

您不必运行 dotnet restore,因为它由所有需要恢复的命令隐式运行,例如 dotnet new、dotnet build、dotnet run、dotnet test、dotnet publish 和 dotnet pack。要禁用隐式还原,请使用 --no-restore 选项。

dotnet restore 命令在某些显式还原有意义的场景中仍然有用,例如 Azure DevOps Services 中的持续集成构建或需要显式控制何时发生还原的构建系统。

您应该在 --no-restore 中使用 dotnet publish 或删除 dotnet restore 并让包由 dotnet publish 隐式恢复

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...