我有一个在自托管构建代理上运行的 Azure DevOps 管道,该管道在恢复步骤中开始失败。该解决方案有一个 Nuget.config 文件,其中引用了我自己的 Artifacts feed,以及包凭据中的占位符,该占位符在第一步中由管道替换为 PAT 令牌。这是管道运行时的错误:
NuGet.Protocol.Core.Types.FatalProtocolException: Unable to load the service index for source https://pkgs.dev.azure.com/myOrganisation/_packaging/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/nuget/v3/index.json.
---> System.Net.Http.HttpRequestException: Response status code does not indicate success: 403 (Forbidden - User 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' lacks permission to complete this action. You need to have 'ReadPackages'. (DevOps Activity ID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx)).
给定的用户 ID 与我的 Azure AD 租户或我的组织中的任何 ID 均不匹配,当我尝试将新用户添加到 Artifacts Feed 时,它不会出现在用户列表中,并且我尚未找到可以识别它的 CLI 命令。
我已经尝试过:
如果您的私有 Artifacts Feed 与管道位于同一 Azure DevOps 组织中,您可以使用
System.AccessToken
代替 PAT 进行授权。
转到“项目设置”(和“组织设置”)>“管道”部分下的“设置”,确保禁用以下两个选项(关闭)。
Limit job authorization scope to current project for non-release pipelines
Limit job authorization scope to current project for release pipelines
打开工件源并转到“源设置”>“权限”。确保至少为以下两个构建标识分配了角色“
Feed and Upstream Reader (Collaborator)
”,以便管道可以从源中读取包。如果您还想从管道将新包发布到提要,则至少应该分配“Feed Publisher (Contributor)
”角色。请参阅“作业访问令牌”。
Project Collection Build Service ({Organization Name})
{Project Name} Build Service ({Organization Name})
在
nuget.config
文件中,您无需在“<packageSourceCredentials>
”部分下配置 Feed 的凭据。
...
<packageSources>
<!-- Add nuget.org as a package source. -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<!-- Add the private Artifacts feed as a package source. -->
<add key="myPrivateFeed" value="{URL to connect to the feed}" />
</packageSources>
. . .
在管道中,您可以配置恢复任务,如下所示。这里我以 dotnet Restore 为例。可以使用 DotNetCoreCLI@2 任务来执行 dotnet 恢复。
steps:
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '{path to the csproj or sln file(s)}'
feedsToUse: config
nugetConfigPath: 'path/to/nuget.config'
使用此配置,由于
nuget.config
文件中设置的私有 feed 与管道位于同一组织中,因此管道将使用“System.AccessToken
”来访问 feed。
如果私有源位于管道所在的当前组织之外,您有两种配置方法。
转到“项目设置”>“服务连接”,创建 NuGet 服务连接以连接到外部源。确保服务连接上设置的用户名和密码/PAT 具有访问外部源中的包的角色权限。
在
nuget.config
文件中,您无需在“<packageSourceCredentials>
”部分下配置 Feed 的凭据。
...
<packageSources>
<!-- Add nuget.org as a package source. -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<!-- Add the private Artifacts feeds as package sources. -->
<add key="ExternalFeed01" value="{URL to connect to the feed}" />
<add key="ExternalFeed02" value="{URL to connect to the feed}" />
</packageSources>
. . .
在管道中,您可以配置恢复任务,如下所示。
steps:
- task: DotNetCoreCLI@2
displayName: 'dotnet restore'
inputs:
command: restore
projects: '{path to the csproj or sln file(s)}'
feedsToUse: config
nugetConfigPath: 'path/to/nuget.config'
externalFeedCredentials: 'ExternalNugetFeed01, ExternalNugetFeed02'
在
nuget.config
文件中,您需要在“<packageSourceCredentials>
”部分下配置源的凭据。您可以在文件中使用环境变量 (%ENV_VAR_NAME%
),而不是显式地将敏感信息(例如密码/PAT)填充到文件中。请参阅“nuget.config
参考”。
...
<packageSources>
<!-- Add nuget.org as a package source. -->
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" />
<!-- Add the private Artifacts feeds as package sources. -->
<add key="ExternalFeed01" value="{URL to connect to the feed}" />
<add key="ExternalFeed02" value="{URL to connect to the feed}" />
</packageSources>
<packageSourceCredentials>
<ExternalFeed01>
<add key="Username" value="%FEED01_USERNAME%" />
<add key="ClearTextPassword" value="%FEED01_PASSWORD%" />
<add key="ValidAuthenticationTypes" value="basic" />
</ExternalFeed01>
<ExternalFeed02>
<add key="Username" value="%FEED02_USERNAME%" />
<add key="ClearTextPassword" value="%FEED02_PASSWORD%" />
<add key="ValidAuthenticationTypes" value="basic" />
</ExternalFeed02>
</packageSourceCredentials>
. . .
将敏感信息作为秘密变量添加到管道中。
在管道中,您可以使用脚本任务(例如Bash、PowerShell或CmdLine)来执行'
dotnet restore
'命令,并使用'任务上的“env
”键可将秘密变量映射为环境变量。
steps:
- task: Bash@3
displayName: 'dotnet restore'
env:
FEED01_USERNAME: $(feed01_username)
FEED01_PASSWORD: $(feed01_password)
FEED02_USERNAME: $(feed02_username)
FEED02_PASSWORD: $(feed02_password)
inputs:
targetType: inline
script: dotnet restore '{path to the csproj or sln file(s)}' --configfile 'path/to/nuget.config'