Azure DevOps 管道故障“用户‘1a5add63-xxxxxxx’缺乏完成此操作的权限。您需要拥有‘ReadPackages’。”

问题描述 投票:0回答:1

我有一个在自托管构建代理上运行的 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 命令。

我已经尝试过:

  • 生成要注入到 nuget.config 中的新 PAT,并具有包读取权限
  • 重新安装构建代理
  • 使用 Azure 托管的构建代理
  • 添加可作为 Artifact feed 读者的所有用户
  • 通过远程桌面在我的构建代理上运行 VS,恢复/重建解决方案工作正常
  • 运行此命令只是挂起
azure-devops azure-pipelines azure-artifacts
1个回答
0
投票

如果您的私有 Artifacts Feed 与管道位于同一 Azure DevOps 组织中,您可以使用

System.AccessToken
代替 PAT 进行授权。

  1. 转到“项目设置”(和“组织设置”)>“管道”部分下的“设置”,确保禁用以下两个选项(关闭)。

    • Limit job authorization scope to current project for non-release pipelines
    • Limit job authorization scope to current project for release pipelines

  2. 打开工件源并转到“源设置”>“权限”。确保至少为以下两个构建标识分配了角色“

    Feed and Upstream Reader (Collaborator)
    ”,以便管道可以从源中读取包。如果您还想从管道将新包发布到提要,则至少应该分配“
    Feed Publisher (Contributor)
    ”角色。请参阅“作业访问令牌”。

    • Project Collection Build Service ({Organization Name})
    • {Project Name} Build Service ({Organization Name})

  3. 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>
    . . .
    
  4. 在管道中,您可以配置恢复任务,如下所示。这里我以 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 服务连接 访问外部源:
  1. 转到“项目设置”>“服务连接”,创建 NuGet 服务连接以连接到外部源。确保服务连接上设置的用户名和密码/PAT 具有访问外部源中的包的角色权限。

  2. 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>
    . . .
    
  3. 在管道中,您可以配置恢复任务,如下所示。

    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'
    
直接使用密码/PAT,无需管道中的 NuGet 服务连接来访问外部源。
  1. 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>
    . . .
    
  2. 将敏感信息作为秘密变量添加到管道中。

  3. 在管道中,您可以使用脚本任务(例如BashPowerShellCmdLine)来执行'

    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'
    

© www.soinside.com 2019 - 2024. All rights reserved.