我有一个任务为我们的 dotnet 核心应用程序恢复 NuGet 包:
Task("Restore-Packages")
.Does(() =>
{
DotNetCoreRestore(sln, new DotNetCoreRestoreSettings {
Sources = new[] {"https://my-team.pkgs.visualstudio.com/_packaging/my-feed/nuget/v3/index.json"},
Verbosity = DotNetCoreVerbosity.Detailed
});
});
但是,当在 VSTS 上运行时,会出现以下错误:
2018-06-14T15:10:53.3857512Z C:\Program Files\dotnet\sdk\2.1.300\NuGet.targets(114,5): error : Unable to load the service index for source https://my-team.pkgs.visualstudio.com/_packaging/my-feed/nuget/v3/index.json. [D:\a\1\s\BitCoinMiner.sln]
2018-06-14T15:10:53.3857956Z C:\Program Files\dotnet\sdk\2.1.300\NuGet.targets(114,5): error : Response status code does not indicate success: 401 (Unauthorized). [D:\a\1\s\BitCoinMiner.sln]
如何授权构建代理访问我们的私有(private) VSTS?
最佳答案
我确实遇到了同样的问题,显然 VSTS 中的构建代理无法在没有访问 token 的情况下访问您的私有(private) VSTS feed,因此您必须在 VSTS 中创建个人访问 token 并将其提供给构建的在 Cake 方法中添加经过身份验证的 VSTS Nuget feed 作为源之一。在这里,我将其包装在我自己的方便 Cake 方法中,该方法检查包提要是否已经存在,如果不存在,则添加它:
void SetUpNuget()
{
var feed = new
{
Name = "<feedname>",
Source = "https://<your-vsts-account>.pkgs.visualstudio.com/_packaging/<yournugetfeed>/nuget/v3/index.json"
};
if (!NuGetHasSource(source:feed.Source))
{
var nugetSourceSettings = new NuGetSourcesSettings
{
UserName = "<any-odd-string>",
Password = EnvironmentVariable("NUGET_PAT"),
Verbosity = NuGetVerbosity.Detailed
};
NuGetAddSource(
name:feed.Name,
source:feed.Source,
settings:nugetSourceSettings);
}
}
然后我从“恢复”任务中调用它:
Task("Restore")
.Does(() => {
SetUpNuget();
DotNetCoreRestore("./<solution-name>.sln");
});
就我个人而言,我更喜欢让 PAT 远离源代码管理,因此我在这里读取环境变量。在 VSTS 中,您可以在 CI 构建配置的“变量”选项卡下创建一个环境变量。
希望这有帮助!这是link到 Cake 的文档。
关于.net-core - 如何使用 Cake 从私有(private) VSTS 源恢复私有(private) NuGet 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50861641/