我有一个 .NET 项目,它使用托管在 Azure DevOps 中的私有 Nuget 包。我有一个 Github Actions 工作流程,它使用个人访问令牌注册私有源,并恢复解决方案中的包。 此操作失败并显示
error NU1301: Unable to load the service index for source
我已确保已启用源,并且个人访问令牌具有 Azure Artifact 的读取权限。
name: tests
on:
workflow_dispatch:
workflow_call:
inputs:
projectName:
description: "Project name"
required: true
type: string
secrets:
PAT:
required: true
USER:
required: true
env:
DOTNET_VERSION: "7.0.x"
jobs:
unit-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Setup dotnet
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Run tests
working-directory: ./Solution
run: |
dotnet nuget update source private-source-name --source ${{ vars.PACKAGE_URL }} --username ${{ secrets.USER }} --password ${{ secrets.PAT }} --store-password-in-clear-text
dotnet restore
dotnet test Solution.Test.${{ inputs.projectName }}
我尝试按照 dotnet 文档注册源并进行恢复,无论是从解决方案级别的 NuGet.Config 还是顶级 NuGet.Config,并确保配置正确设置:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="private-source-name" value="private-source" />
</packageSources>
<packageSourceCredentials>
<private-source-name>
<add key="Username" value="<username>" />
<add key="ClearTextPassword" value="<clear-text-PAT>" />
</private-source-name>
</packageSourceCredentials>
</configuration>
解决方案中的 随后为
dotnet restore
。我还使用指定配置文件中的 nuget restore
完成了此操作。
根据
error NU1301: Unable to load the service index for source
,Nuget.Config 无法包含正确的内容。您可以在 cat Nuget.Config
命令后添加 dotnet nuget update source ...
命令来检查内容,确保替换的值正确,特别是对于变量传递。
- name: Run tests
working-directory: ./Solution
run: |
dotnet nuget update source private-source-name --source ${{ vars.PACKAGE_URL }} --username ${{ secrets.USER }} --password ${{ secrets.PAT }} --store-password-in-clear-text
cat NuGet.Config
dotnet restore
我遵循您的 NuGet.Config,并修复了一些语法:
之前
dotnet nuget update source
命令:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<add key="nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="private-source-name" value="private-source" />
</packageSources>
<packageSourceCredentials>
<private-source-name>
<add key="Username" value="username" />
<add key="ClearTextPassword" value="%PAT%" />
</private-source-name>
</packageSourceCredentials>
</configuration>
确认包裹已恢复:编辑:使用 github 操作屏幕截图进行更新。