【问题标题】:Can't get Pipeline to use custom NuGet server with Azure DevOps build无法让 Pipeline 将自定义 NuGet 服务器与 Azure DevOps 构建一起使用
【发布时间】:2018-10-29 19:42:01
【问题描述】:

我的解决方案使用来自官方 NuGet 服务器和私有 NuGet 服务器的包。我正在尝试配置我的构建管道以从两个位置恢复包,但不断收到 NuGet 恢复构建错误,看起来它正在尝试从公共 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

【问题讨论】:

    标签: nuget azure-devops nuget-package-restore


    【解决方案1】:

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

    【讨论】:

    • 谢谢,在使用旧界面后,我查看了它生成的 YAML 文件。看来我原来的 YAML 的问题是我在 nugetConfigPath 设置中的斜线方向错误。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      我刚遇到这个问题,但我的项目中没有 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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2020-08-28
        • 2012-05-25
        • 2021-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-04-23
        • 1970-01-01
        相关资源
        最近更新 更多