You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用dotnet restore时Github Action无法识别已添加的内部NuGet源

GitHub Action中NuGet内部源添加成功但restore提示不存在的问题

问题描述

我在创建GitHub Action时需要使用内部NuGet包,执行以下步骤添加内部源:

- name: "Setup Internal Packages"
  run: |
    dotnet nuget add source ${{ secrets.artifactory_url }} \
    -n myinternalpack \
    -u ${{ secrets.artifactory_user }} \
    -p ${{ secrets.artifactory_token }} \
    --store-password-in-clear-text

执行后提示:Package source with Name: myinternalpack added successfully.

随后我通过dotnet nuget list source确认源已存在:

Run dotnet nuget list source
Registered Sources:
1.  nuget.org [Enabled]
  https://api.nuget.org/v3/index.json
2.  myinternalpack  [Enabled]
    ***

但执行restore命令时:

dotnet restore ./sample-app --source myinternalpack --source https://api.nuget.org/v3/index.json

系统提示该源不存在。

解决方法

1. 排查源名称的空格问题

从list source的输出能看到,myinternalpack名称后带有多余空格(myinternalpack [Enabled]),这会导致restore时用无空格的名称匹配失败。

  • 临时解决:restore时用引号包裹带空格的源名称
    dotnet restore ./sample-app --source "myinternalpack  " --source https://api.nuget.org/v3/index.json
    
  • 彻底解决:重新添加源时确保名称无多余空格(检查-n myinternalpack行末尾是否有空格)

2. 直接使用源URL替代名称

如果名称匹配问题不好排查,restore时直接用内部源的URL,绕开名称匹配环节:

dotnet restore ./sample-app --source ${{ secrets.artifactory_url }} --source https://api.nuget.org/v3/index.json

3. 指定NuGet配置文件路径

部分环境下,用户级配置文件可能因权限问题未正确保存修改,可手动指定配置文件路径:

  • 添加源时指定配置文件:
    - name: "Setup Internal Packages"
      run: |
        dotnet nuget add source ${{ secrets.artifactory_url }} \
        -n myinternalpack \
        -u ${{ secrets.artifactory_user }} \
        -p ${{ secrets.artifactory_token }} \
        --store-password-in-clear-text \
        --configfile ./NuGet.Config
    
  • restore时指定该配置文件:
    dotnet restore ./sample-app --configfile ./NuGet.Config
    

内容的提问来源于stack exchange,提问作者Rafa

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 22:35:38