【问题标题】:How to pass Azure Auth when Deploying NuGet Package via Nuke.Common/NuGet.CommandLine通过 Nuke.Common/NuGet.CommandLine 部署 NuGet 包时如何通过 Azure Auth
【发布时间】:2020-10-08 18:45:23
【问题描述】:

我正在尝试通过 Azure DevOps 上的 Nuke 和 CI/CD 自动更新 NuGet 包。一切都构建得很好,但是在 PushNuGet 步骤中,该过程尝试通过弹出窗口向 Azure 进行身份验证,这显然从未在 [在 devops 中] 呈现或回答

class Build : NukeBuild
{
    /// Support plugins are available for:
    ///   - JetBrains ReSharper        https://nuke.build/resharper
    ///   - JetBrains Rider            https://nuke.build/rider
    ///   - Microsoft VisualStudio     https://nuke.build/visualstudio
    ///   - Microsoft VSCode           https://nuke.build/vscode

    public static int Main () => Execute<Build>(x => x.PushNuGet);

    [Parameter("Configuration to build - Default is 'Debug' (local) or 'Release' (server)")]
    readonly Configuration Configuration = IsLocalBuild ? Configuration.Debug : Configuration.Release;

    [Solution] readonly Solution Solution;
    [GitRepository] readonly GitRepository GitRepository;

    AbsolutePath SourceDirectory => RootDirectory / "src";
    AbsolutePath TestsDirectory => RootDirectory / "tests";
    AbsolutePath ArtifactsDirectory => RootDirectory / "artifacts";

    string VersionNumber = "1.0.2";

    Target Clean => _ => _
        .Executes(() =>
        {
            SourceDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
            TestsDirectory.GlobDirectories("**/bin", "**/obj").ForEach(DeleteDirectory);
            EnsureCleanDirectory(ArtifactsDirectory);
        });

    Target Restore => _ => _
        .DependsOn(Clean)
        .Executes(() =>
        {
            DotNetRestore(s => s
                .SetProjectFile(Solution));
        });

    Target Compile => _ => _
        .DependsOn(Restore)
        .Executes(() =>
        {
            DotNetBuild(s => s
                .SetProjectFile(Solution)
                .SetConfiguration(Configuration)
                .EnableNoRestore());
        });

    Target Pack => _ => _
        .DependsOn(Compile)
        .Executes(() =>
        {
            DotNetPack(s => s
                .SetProject(RootDirectory + "\\Fuze.Models\\Fuze.Models.csproj")
                .SetNoBuild(true)
                .SetConfiguration(Configuration)    
                .SetVersion(VersionNumber)
            );
        });

    Target AddSource => _ => _
       .DependsOn(Pack)
       .Executes(() =>
       {
            var sourceUrl = "https://pkgs.dev.azure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json";
            var sourceName = "DataFuzionHCM";
            var sources = NuGetTasks.NuGetSourcesList();
            if(sources.Any(source => source.Text.Contains(sourceName)))
            {
               NuGetTasks.NuGetSourcesRemove(s => s.SetName(sourceName));
            }
            NuGetTasks.NuGetSourcesAdd(s => s
                .SetName(sourceName)
                .SetSource(sourceUrl)
                .SetUserName("NuGet Feed Token")
                .SetPassword("fakepassword")
            );
       });

    Target PushNuGet => _ => _
        .DependsOn(AddSource)
        .Executes(() =>
        {
            NuGetTasks.NuGetPush(s => s
                .SetSource("DataFuzionHCM")
                .SetApiKey("az")
                .SetTargetPath(RootDirectory + $"/FUZE.Models/bin/debug/Fuze.Models.{VersionNumber}.nupkg")
            );
        });
}

在 Azure Build Pipeline 上,在最后一步中,可以在作业日志中看到它卡在了 azure 的某个身份验证窗口上。

Using credentials from config. UserName: NuGet Feed Token
    [CredentialProvider]Using the ADAL UI  flow for uri https://pkgs.dev.[hidden]ure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json. User sign-in required in a pop-up authentication window.
    [CredentialProvider]Using the ADAL UI  flow for uri https://pkgs.dev.[hidden]ure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json. User sign-in required in a pop-up authentication window.
    [CredentialProvider]Using the ADAL UI  flow for uri https://pkgs.dev.[hidden]ure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json. User sign-in required in a pop-up authentication window.
##[error]Unable to load the service index for source https://pkgs.dev.[hidden]ure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json.
##[error]  The HTTP request to 'GET https://pkgs.dev.[hidden]ure.com/DataFuzionHCM/_packaging/DataFuzionHCM/nuget/v3/index.json' has timed out after 100000ms.
##[error]Process 'NuGet.exe' exited with code 1.

是否有一种方法可以通过 Azure 以编程方式进行身份验证,使其不会在弹出验证时挂起和超时?

【问题讨论】:

  • 您好,您能否将代理设置检查到 Nuget.Config 文件中,然后在此处分享结果?有关详细信息,请参阅此链接:Nuget Config SectionNuget Proxy Settings。顺便问一下,如果你在本地运行会发生什么?
  • 当我在本地运行它时,会出现一个提示窗口让我验证到 azure。输入我的个人凭据允许请求通过 - 我在下面的答案中发布了解决方案。

标签: c# azure azure-devops nuget nuke-build


【解决方案1】:

事实证明,NuGet 提要的密码已过期,因此当身份验证失败时,它会提示进行身份验证,而不是引发身份验证错误。 我进入 DevOps 并更新了密码的到期日期,从那里开始一切正常。

我确实觉得奇怪的是,它没有引发身份验证错误,而是在单独的窗口中要求本地用户输入密码。也许在设计用于更新 NuGet 包的 API 时没有考虑自动化。

【讨论】:

  • 嗨@Chris Phillips,感谢您的分享,您可以接受您的回答。在这种情况下,其他人可以直接找到有用的解决方案。谢谢
猜你喜欢
  • 2020-06-18
  • 1970-01-01
  • 1970-01-01
  • 2021-05-29
  • 2023-01-23
  • 2015-05-28
  • 2023-02-14
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多