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

一笑奈何 提交于 2019-11-27 04:36:55

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.

There you can pass the location of your private repo as argument to 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, e.g.

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. So to get NuGet configuration to for the build, I had to use the following path instead:

  • 64-bit Windows C:\Windows\SysWOW64\config\systemprofile\AppData\Roaming\NuGet\NuGet.Config
  • 32-bit 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.

Note: I have no solution for using the Local Service account. The above only works for the Local System (or an actual user) account.

Add a NuGet.config to your project that specifies an alternate package location. 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. Run this task before you run 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

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. That's all!

  1. Specify your custom NuGet feed URL’s in the solution’s nuget.config file. 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. Variables can be encrypted and will not be displayed in any of the build log outputs. Here I'll create MyCompanyNugetUser and MyCompanyNugetPwd variables.

  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. Below is the code snippet from my inline Powershell script:

    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

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

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. In this task you can pass the argument -ConfigFile path/to/nuget.config which contains your repository path.

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. If the case is wrong, then (for UWP only) our build server fails the build.

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!