代码之家  ›  专栏  ›  技术社区  ›  Kevin Smith

如何使用cake从私有vsts提要还原私有nuget包

  •  0
  • Kevin Smith  · 技术社区  · 6 年前

    我有一个任务是为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
        });
    });
    

    但是,在VST上运行时,会出现以下错误:

    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]
    

    如何授权生成代理访问我们的专用VST?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Aman    6 年前

    我也有同样的问题,显然,vsts中的构建代理无法在没有访问令牌的情况下访问您的私有vsts提要,因此您必须在vsts中创建一个个人访问令牌,并将其提供给内置cake方法,以便将经过身份验证的vsts nuget提要添加为源之一。在这里,我用我自己的便利蛋糕方法包装它,该方法检查包提要是否已经存在,如果不存在,则添加它:

    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"); 
    });
    

    就我个人而言,我更喜欢让pats远离源代码管理,所以在这里我从env vars中阅读。在VSTS中,可以在CI生成配置的“变量”选项卡下创建环境变量。

    enter image description here

    希望这有帮助!这里是一个 link 蛋糕的文件。