.net-core - 如何使用 Cake 从私有(private) VSTS 源恢复私有(private) NuGet 包

标签 .net-core nuget azure-devops cakebuild

我有一个任务为我们的 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 构建配置的“变量”选项卡下创建一个环境变量。

enter image description here

希望这有帮助!这是link到 Cake 的文档。

关于.net-core - 如何使用 Cake 从私有(private) VSTS 源恢复私有(private) NuGet 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50861641/

相关文章:

asp.net-core - dotnet 发布在开发计算机上成功,构建代理失败,asp.net Netcoreapp2.1/win-x64

c# - EF Core 过滤器枚举存储为带有 LIKE 运算符的字符串

c# - 程序集从哪里加载?

azure - 如何在 Azure DevOps yaml 管道中设置环境状态

asp.net-core - 在 ASP.NET Core 2.0 中哪里可以找到 System.DirectoryServices?

c# - 使用 dotnet pack 创建带有外部 .exe 文件的 NuGet 包

.net - 从 Azure Build Pipeline 中的 FxCop 代码分析器(Roslyn)生成报告

asp.net-core - 迁移到 .Net Core 3 后 JsonPatchDocument 为空

c# - 定义要在构建事件中使用的自定义宏

jquery - 如何在 Razor Pages 中序列化表单并发送 Ajax 请求