从构建脚本推送到工件的 nuget 无法进行身份验证

nuget push to artifacts from build script fails to authenticate

我有一个用 FAKE 编写的构建脚本,我想 运行 在 devops 管道下。 该构建适用于我的笔记本电脑。 我有 YAML 来触发和 运行 构建,它构建但未能将工件推送到 nuget 存储库中。

我已经生成了一个 PAT,并在 NuGetPublish 调用中使用了它(现在明确地)。

  NuGet.NuGetPublish 
    (fun p ->
      {
        p with
          Project = name
          Version = version
          PublishUrl = "https://...../nuget/v3/index.json"
          WorkingDir = "."
          OutputPath = path
          AccessKey = "bla bla bla" 
      }))

这适用于我的笔记本电脑,但在 Azure 上它只是重试并重新连接...

NugetPublish from..
NugetPublish from..D:\a\s\US.2018r2.000.024.Schema\bin\Release\US.2018r2.000.024.Schema.1.1.2.nupkg
name=US.2018r2.000.024.Schema
version=1.1.2
path=D:\a\s\US.2018r2.000.024.Schema\bin\Release
Starting task 'NuGet-Push': US.2018r2.000.024.Schema.1.1.2.nupkg
D:\a\s\tools\NuGet\nuget.exe push "D:\a\s\US.2018r2.000.024.Schema\bin\Release\US.2018r2.000.024.Schema.1.1.2.nupkg" -ApiKey <NuGetKey> -Source https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json -Timeout 300 in WorkingDir: D:\a\s Trials left: 5
.> "D:\a\s\tools\NuGet\nuget.exe" push "D:\a\s\US.2018r2.000.024.Schema\bin\Release\US.2018r2.000.024.Schema.1.1.2.nupkg" -ApiKey <NuGetKey> -Source https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json -Timeout 300 (In: false, Out: false, Err: false)
CredentialProvider.VSS: Getting new credentials for source:https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json, scope:vso.packaging_write vso.drop_write
CredentialProvider.VSS: Getting new credentials for source:https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json, scope:vso.packaging_write vso.drop_write
CredentialProvider.VSS: Getting new credentials for source:https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json, scope:vso.packaging_write vso.drop_write
D:\a\s\tools\NuGet\nuget.exe push "D:\a\s\US.2018r2.000.024.Schema\bin\Release\US.2018r2.000.024.Schema.1.1.2.nupkg" -ApiKey <NuGetKey> -Source https://pkgs.dev.azure.com/Kookerella2/_packaging/Kookerella2/nuget/v3/index.json -Timeout 300 in WorkingDir: D:\a\s Trials left: 4

“正在获取新凭据”等等等等

我知道您可以从管道任务中发布,但我只是想从 Jenkins 中提取和移植,我非常喜欢构建脚本做很多事情,而管道要做的事情不是很多.

----修改---

进一步研究后发现 ApiKey 几乎毫无意义,您需要使用诸如...

nuget sources add -name "Kookerella2" -source https://pkgs.dev.azure.com/..../index.json -username anything -password [PAT]

现在只是通过使用由 yaml 驱动的“脚本”明确地尝试它。

---- 有效,见下文(我明天才能将其标记为答案)------

唯一突出的问题是......将 PAT 密钥放入 YAML 中并不好......我应该怎么做?

我试过了

  - script: nuget sources add -name "Kookerella2" -source https://pkgs.dev.azure.com/..../index.json -username anything -password %SYSTEM_ACCESSTOKEN%
    env:
      SYSTEM_ACCESSTOKEN: $(System.AccessToken)    

如果我尝试“回显 %SYSTEM_ACCESSTOKEN%”,我只会得到 ***....

所以答案是忽略 API 键,它什么都不做,您需要授予存储库对 Nuget 的访问权限。

我通过从 devops 创建一个新的 PAT 密钥来使用 PAT 密钥

https://docs.microsoft.com/en-us/azure/devops/organizations/accounts/use-personal-access-tokens-to-authenticate?view=azure-devops&tabs=preview-page

然后在我的 yaml 中包含一个脚本..显然这个例子是一个假的 PAT。

name: $(Rev:r)

trigger:
- master

jobs:
- job: Windows
  pool:
    vmImage: 'windows-2019'
  steps:
  - task: NuGetToolInstaller@1
  - task: UseDotNet@2
    inputs:
      packageType: 'sdk'
      version: '3.1.201'        
  - script: dotnet tool restore
    displayName: Install FAKE
  - script: nuget sources add -name "ACME" -source https://pkgs.dev.azure.com/ACME/_packaging/ACME/nuget/v3/index.json -username anything -password 5xxxxxxxxxxxq
    displayName: nuget add source
  - script: dotnet fake build
    displayName: Run Build

这行得通!....但是...我认为在 YAML 中使用 PAT 密钥不是一个好的做法。

错误指示 NuGet 在连接到 Azure DevOps 包源时不断提示输入凭据。

这意味着您登录的帐户没有 NuGet 包的权限。

授予访问权限后,您应该可以登录,然后它就会按预期工作。

but...I think having the PAT key in the YAML is not good practice.

是的,这不是一种安全和推荐的方式。相反,您可以使用变量、变量组或 Azure Keyvault 来保护您的 PAT 并在管道中使用它

详细官方文档如何使用variable group供参考,干脆保密。

此外,正如Krzysztof Madej评论的那样,您也可以直接传递OAuth令牌,而无需维护单独的PAT。

更多详情请看这里blog