如何使用Cake从私有VSTS源恢复私有NuGet包

时间:2018-06-14 16:01:02

标签: .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]

如何授权构建代理访问我们的私有VSTS?

2 个答案:

答案 0 :(得分:1)

我确实遇到了同样的问题,显然VSTS中的构建代理无法访问您的私有VSTS源而没有访问令牌,因此您将不得不在VSTS中创建个人访问令牌并将其提供给构建在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远离源代码控制,所以我在这里读取env vars。在VSTS中,您可以在CI构建配置的“变量”选项卡下创建环境变量。

Firebase use query result outside function

希望这有帮助!这是Cake的enter image description here文档。

答案 1 :(得分:0)

正如@KevinSmith和@NickTurner所指出的那样,一种更好的访问VSTS提要的方法是使用预定义的系统变量System.AccessToken,而不是使用有限的有效性,手动创建且麻烦的PAT。该变量在构建代理上可用,供当前构建使用。更多信息here

在Cake脚本中使用此令牌的一种方法如下:

首先,在azure-pipelines.yml

中将系统变量作为Cake任务的环境变量公开。
steps:
- task: cake-build.cake.cake-build-task.Cake@0
  displayName: 'Cake '
  inputs:
    target: Pack
  env:
    SYSTEM_ACCESSTOKEN: $(System.AccessToken)

然后在Cake中,您可以像访问任何环境变量一样访问它,因此在我的情况下:

if (!NuGetHasSource(source:feed.Source))
{
    Information($"Nuget feed {feed.Source} not found, adding...");      

    var nugetSourceSettings = new NuGetSourcesSettings
                            {
                                UserName = "whoosywhatsit",
                                Password = EnvironmentVariable("SYSTEM_ACCESSTOKEN"),
                                Verbosity = NuGetVerbosity.Detailed
                            };      

    NuGetAddSource(
        name:feed.Name,
        source:feed.Source,
        settings:nugetSourceSettings);
}

这似乎有效!如果有更好的方法在Cake中访问此变量,请告诉我。 请注意,在我的情况下,我仅使用它从VSTS feed中恢复软件包,而不是将其推送到其中。我是通过YML中的DotNetCoreCLI@2任务来完成的,就像这样:

- task: DotNetCoreCLI@2
  displayName: 'dotnet nuget push'
  inputs:
    command: push
    packagesToPush: 'artifacts/package.nupkg'
    publishVstsFeed: '<id of my VSTS feed>'

Azure管道处理其余部分。

相关问题