简体   繁体   English

如何获取TFS2015 Build(Build.vnext)和NuGet包还原以使用自定义包源

[英]How to get TFS2015 Build (Build.vnext) and NuGet package restore to use custom package sources

I'm trying to get our TFS2015 Build ( TFSBuild vnext ) going along with VS Team Services. 我正在尝试将我们的TFS2015 Build( TFSBuild vnext )与VS Team Services一起使用。

Thus far most blogs and documentation has been helpful, except for when trying to build a project that uses custom package sources for NuGet automatic package restore. 到目前为止,大多数博客和文档都很有用,除了在尝试构建使用自定义包源来进行NuGet自动包恢复的项目时。

I've successfully deployed an a Team Services Build Agent (the new mechanism for builds) and all seemed to go well until the build task to Restore NuGet packages failed. 我已经成功部署了一个Team Services Build Agent(新的构建机制),并且在恢复NuGet包的构建任务失败之前似乎一切顺利。

The failure was due to custom/private package source not being picked up from the installed VS configuration, so those packages could not be found. 失败的原因是未从已安装的VS配置中获取自定义/私有包源,因此无法找到这些包。 The traditional packages from the default NuGet package feed did restore without issue. 默认NuGet包Feed中的传统包确实可以正常恢复。

How do I specify the additional package sources for NuGet to use when using an agent? 如何在使用代理时指定NuGet使用的其他包源?

EDIT: vcastro asked about the NuGet Installer build step and defining paths in the configuration of that build step. 编辑:vcastro询问了NuGet Installer构建步骤,并在该构建步骤的配置中定义了路径。 The above is using the Visual Studio Build step and the integrated option to have NuGet restore packages. 以上是使用Visual Studio Build步骤和集成选项来使NuGet恢复包。 Refer this image for reference: 参考此图片以供参考: http://i.imgur.com/Hrqhhk2.png

Alternatively you could also add a NuGet Installer build step before the Visual Studio Build step in your build configuration to restore all NuGet packages. 或者,您也可以在构建配置中的Visual Studio Build步骤之前添加NuGet Installer构建步骤,以还原所有NuGet包。

There you can pass the location of your private repo as argument to nuget.exe : 在那里,您可以将私有nuget.exe的位置作为参数传递给nuget.exe

-source "https://www.nuget.org/api/v2/;http://mynugetserver"

I've scrounged the web with little success, but after fiddling the following will help: 我在网上搜索得不太成功,但在摆弄以下内容后会有所帮助:

OK It seems that the package sources configured for NuGet.config is stored per user account, eg OK似乎为NuGet.config配置的包源是按用户帐户存储的,例如

c:\\Users\\<<username>>\\AppData\\Roaming\\NuGet\\NuGet.config

My issue was harder to resolve, because the build agent was running as a Windows Service under the Local System account. 我的问题难以解决,因为构建代理在Local System帐户下作为Windows Service运行。 So to get NuGet configuration to for the build, I had to use the following path instead: 因此,要为构建获取NuGet配置,我必须使用以下路径:

  • 64-bit Windows C:\\Windows\\SysWOW64\\config\\systemprofile\\AppData\\Roaming\\NuGet\\NuGet.Config 64位Windows C:\\Windows\\SysWOW64\\config\\systemprofile\\AppData\\Roaming\\NuGet\\NuGet.Config
  • 32-bit Windows C:\\Windows\\System32\\config\\systemprofile\\AppData\\Roaming\\NuGet\\NuGet.Config 32位Windows C:\\Windows\\System32\\config\\systemprofile\\AppData\\Roaming\\NuGet\\NuGet.Config

You may need to have elevated permissions in order to create the NuGet subfolder and NuGet.Config file. 您可能需要具有提升的权限才能创建NuGet子文件夹和NuGet.Config文件。

Note: I have no solution for using the Local Service account. 注意:我没有使用Local Service帐户的解决方案。 The above only works for the Local System (or an actual user) account. 以上仅适用于Local System (或实际用户)帐户。

Add a NuGet.config to your project that specifies an alternate package location. 将NuGet.config添加到项目中,以指定备用包位置。 The rules for resolution are well-defined and explained in the official documentation . 解决规则的定义很明确,并在官方文件中进行了解释。

There is a new VSTS Task called "NuGet Installer" this allows you to check in your NuGet.config file and specify the different package sources. 有一个名为“NuGet Installer”的新VSTS任务,它允许您签入NuGet.config文件并指定不同的包源。 Run this task before you run MSBuild. 在运行MSBuild之前运行此任务。

If you are using the VSTS NuGet Feed you will need to add the build service account to the feed to enable downloading of packages https://www.visualstudio.com/get-started/package/use/common-identities 如果您使用的是VSTS NuGet Feed,则需要将构建服务帐户添加到源以启用软件包下载https://www.visualstudio.com/get-started/package/use/common-identities

在此输入图像描述

One solution (works for me) is change account for tfs 2015 build agent service (on my build machine VSO Agent tsf.Agent-PC) to tfsagent, for example, and add Nuget.config to the C:\\Users\\tfsagent\\AppData\\Roaming\\Nuget. 一个解决方案(适用于我)是将tfs 2015构建代理服务(在我的构建机器VSO代理tsf.Agent-PC上)更改为tfsagent,并将Nuget.config添加到C:\\ Users \\ tfsagent \\ AppData \\漫游\\的NuGet。 That's all! 就这样!

  1. Specify your custom NuGet feed URL's in the solution's nuget.config file. 在解决方案的nuget.config文件中指定自定义NuGet供稿URL。 Do not store any usernames & passwords in this file. 不要在此文件中存储任何用户名和密码。

     <add key="nuget.org" value="https://www.nuget.org/api/v2/" /> <add key="MyCompany" value="https://nuget.mycompany.com:443/nuget" /> 
  2. Create username & password variables in your build definition in VSTS. 在VSTS中的构建定义中创建用户名和密码变量。 Variables can be encrypted and will not be displayed in any of the build log outputs. 变量可以加密,不会显示在任何构建日志输出中。 Here I'll create MyCompanyNugetUser and MyCompanyNugetPwd variables. 在这里,我将创建MyCompanyNugetUserMyCompanyNugetPwd变量。

  3. In our build steps, we add a Powershell script as the first action, this will read the username & password variables and update the user level nuget.config file on the build machine. 在我们的构建步骤中,我们添加一个Powershell脚本作为第一个操作,这将读取用户名和密码变量并更新构建计算机上的用户级nuget.config文件。 Below is the code snippet from my inline Powershell script: 以下是我的内联Powershell脚本中的代码段:

    Arguments: 参数:

     $(MyCompanyNugetUser) $(MyCompanyNugetPwd) 

    Script: 脚本:

     param($user, $pwd) $nugetFile = "$ENV:AGENT_HOMEDIRECTORY\\agent\\worker\\tools\\nuget.exe" Write-Output "Looking for nuget.exe in $nugetFile" if (-not (Test-Path $nugetFile)) { Write-Error "nuget.exe could not be located." return } Write-Output "nuget.exe located" $cmd = "$nugetFile sources add -name MyCompany -source https://nuget.mycompany.com:443/nuget -username $user -password $pwd -StorePasswordInClearText" Write-Output $cmd iex $cmd 
  4. Next, we just continue to execute the default NuGet Restore step from Microsoft's templates 接下来,我们继续从Microsoft的模板执行默认的NuGet Restore步骤

More here: https://codingcase.com/2016/07/27/vsts-build-setup-custom-nuget-feeds-with-authentication/ 更多信息: https//codingcase.com/2016/07/27/vsts-build-setup-custom-nuget-feeds-with-authentication/

HTH HTH

In the RTM of Team Foundation Server 2015 you have to add a build step of the type "NuGet Installer", and restore the packages of the Solution file before you run the actual build process. 在Team Foundation Server 2015的RTM中,您必须添加“NuGet Installer”类型的构建步骤,并在运行实际构建过程之前还原Solution文件的包。 In this task you can pass the argument -ConfigFile path/to/nuget.config which contains your repository path. 在此任务中,您可以将参数-ConfigFile path/to/nuget.config传递-ConfigFile path/to/nuget.config包含存储库路径的-ConfigFile path/to/nuget.config

For Example: 例如:

<configuration>
  <packageSources>
    <add key="Internal Nuget" value="\\srv-nuget\Repo" />
  </packageSources>
</configuration>

在此输入图像描述

If you are having trouble getting this to work on UWP ONLY, then ensure that you have the CASE of the package name spelt correctly. 如果您无法在UWP上进行此操作,请确保您正确拼写了包名称的CASE。 If the case is wrong, then (for UWP only) our build server fails the build. 如果情况有误,那么(仅适用于UWP)我们的构建服务器无法构建。

for instance, if you have a package called Com.Company.Components and update the package using "install-package com.company.components " (note the case of the initial letter) then the UWP build on the build server may fail to find the package in your local store. 例如,如果您有一个名为Com.Company.Components的包并使用“install-package com.company.components ”更新包(请注意首字母的大小写),那么构建服务器上的UWP构建可能无法找到您当地商店的包裹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM