无法让 Pipeline 将自定义 NuGet 服务器与 Azure DevOps 构建一起使用

Can't get Pipeline to use custom NuGet server with Azure DevOps build

我的解决方案使用来自官方 NuGet 服务器和私有 NuGet 服务器的包。我正在尝试配置我的构建管道以从两个位置恢复包,但不断出现 NuGet 恢复构建错误,看起来它正在尝试从 public NuGet 服务器恢复我的私有包,因此失败是可以理解的。

我不知道我还应该做什么。似乎 Azure DevOps 中没有可以为 NuGet 还原步骤进行的设置,因为看起来现在所有这些都在 YAML 文件中配置。任何关于我可能做错了什么或我还可以尝试什么的建议都将不胜感激。

我的 NuGet.config 在我的解决方案中:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageRestore>
    <!-- Allow NuGet to download missing packages -->
    <add key="enabled" value="True" />
    <!-- Automatically check for missing packages during build in Visual Studio -->
    <add key="automatic" value="True" />
  </packageRestore>
  <packageSources>
    <add key="NuGet official package source" value="https://api.nuget.org/v3/index.json" />
    <add key="Private" value="http://privatenuget.net:8080/nuget" />
  </packageSources>
  <activePackageSource>
    <add key="All" value="(Aggregate source)" />
  </activePackageSource>
</configuration>

Pipelines 使用的我的 YAML 文件:

# ASP.NET
# Build and test ASP.NET projects.
# Add steps that publish symbols, save build artifacts, deploy, and more:
# https://docs.microsoft.com/azure/devops/pipelines/apps/aspnet/build-aspnet-4

pool:
  vmImage: 'VS2017-Win2016'

variables:
  solution: 'MyProject.sln'
  buildPlatform: 'Any CPU'
  buildConfiguration: 'Release'

steps:
- task: NuGetToolInstaller@0

- task: NuGetCommand@2
  inputs:
    nugetConfigPath: 'MyProject\NuGet.config'
    restoreSolution: '$(solution)'

- task: VSBuild@1
  inputs:
    solution: '$(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
  inputs:
    platform: '$(buildPlatform)'
    configuration: '$(buildConfiguration)'

我在构建的 NuGetCommand 步骤中遇到的错误:

The nuget command failed with exit code(1) and error(Errors in packages.config projects
    NU1000: Unable to find version '1.1.5' of package 'MyPackage'.
      https://api.nuget.org/v3/index.json: Package 'MyPackage' is not found on source 'https://api.nuget.org/v3/index.json'.)
Packages failed to restore

您可以从一个空作业开始,而不使用现有的 YAML。然后你可以设置你的 agents/tasks(nuget restore 等)来构建你的应用程序。

我刚刚 运行 解决了这个问题,但我的项目中没有 NuGet.config 文件可供使用,也不想添加一个。使用 NuGet.config 需要您以明文形式存储个人访问令牌 (PAT),至少可以说这不太理想,尤其是当它与您的项目一起提交到您的回购时。

经过大量研究,我找到了一个近乎完美的解决方案。使用 Azure DevOps 中的变量组,您可以添加可用于所有管道的变量(和机密)。我突然想到,我可以将整个 NuGet.config 放入一个秘密中(连同 PAT),然后将其作为管道的一部分通过管道传输到实际的 NuGet.config 文件中。

您已经有了一个 NuGet.config 文件,但如果其他人着陆并从头开始,您需要以下内容:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
    <add key="MyPrivateFeed" value="*** PRIVATE FEED URL HERE ***" />
  </packageSources>
  <packageSourceCredentials>
    <MyPrivateFeed>
      <add key="Username" value="anything" />      
      <add key="ClearTextPassword" value="*** PAT HERE ***" />
    </MyPrivateFeed>
  </packageSourceCredentials>
</configuration>

填写您的提要 URL 和 PAT,然后将其全部复制并粘贴到变量组中名为 "NuGet.config" 的变量中。单击变量上的锁定图标以使其成为秘密。该变量可以随意命名,但如果您使用不同的名称,则需要在下面的代码中对其进行更新。

然后,您只需要包含您的变量组:

variables:
  - group: my-variable-group

并将以下内容添加到您的管道 yaml 中,在将使用私有提要的任何其他步骤之前(例如 dotnet build)。

- bash: echo -e '$(NuGet.config)' > NuGet.config
  displayName: Create NuGet.config

您的 YML 文件不正确,您必须添加 feedsToUse: config

- task: NuGetCommand@2
  displayName: 'Nuget Restore'
  inputs:
    restoreSolution: '$(solution)'
    feedsToUse: config
    nugetConfigPath: nuget.config