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

将NuGet包发布至GitHub私有仓库时路径找不到的问题求助

私有NuGet包推送失败:找不到生成目录的问题分析与修复

问题现象

构建流程正常,但NuGet推送环节报错,提示找不到包的生成目录:

Run dotnet nuget push "${GITHUB_WORKSPACE}/bin/Release/MyLibrary.Core.1.3.1.nupkg" --source "github"
shell: /usr/bin/bash -e {0}
env: DOTNET_ROOT: /usr/share/dotnet
error: Could not find a part of the path '/home/runner/work/my-core-library/my-core-library/bin/Release'.

执行ls -l查看仓库根目录的结果:

ls -l
  shell: /usr/bin/bash -e {0}
  env:
    DOTNET_ROOT: /usr/share/dotnet
total 12
drwxr-xr-x 10 runner docker 4096 Mar 27 18:37 MyLibrary.Core
-rw-r--r--  1 runner docker 1114 Mar 27 18:37 MyLibrary.Core.sln
-rw-r--r--  1 runner docker  269 Mar 27 18:37 README.md

对应的GitHub Action配置如下:

name: Publish MyLibrary.Core NuGet to GitHub Packages

on:
  pull_request:
    branches: [ "master" ]

jobs:
  build-pack-n-ship:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore
    - name: Build
      run: dotnet build --no-restore
    - name: Create NuGet package
      run: dotnet pack --configuration Release
    - name: Add Package Source
      run: dotnet nuget add source --username my_github_username --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/my_company_account/index.json"
    - name: Publish to GitHub Packages
      run: dotnet nuget push "${GITHUB_WORKSPACE}/bin/Release/MyLibrary.Core.${{ steps.get-version.outputs.version }}.nupkg" --source "github"

问题原因

  1. 项目路径偏差:从ls结果可见,类库项目文件不在仓库根目录,而是在MyLibrary.Core子文件夹下。执行dotnet pack时未指定项目路径,默认在根目录寻找项目文件(仅存在解决方案文件),导致包要么未生成,要么生成在MyLibrary.Core/bin/Release子目录,而非推送命令指定的根目录bin/Release。
  2. 版本变量未定义:推送命令中使用了${{ steps.get-version.outputs.version }},但配置里没有名为get-version的步骤,变量为空会导致路径拼接错误。

修复方案

步骤1:修正dotnet pack的执行路径

在Create NuGet package步骤中,指定项目文件路径并强制输出到目标目录,确保路径与推送命令匹配:

- name: Create NuGet package
  run: dotnet pack MyLibrary.Core/MyLibrary.Core.csproj --configuration Release --output "${GITHUB_WORKSPACE}/bin/Release"

步骤2:补充版本获取步骤

添加步骤读取项目文件中的版本号,为推送命令提供有效版本变量:

- name: Get project version
  id: get-version
  run: |
    VERSION=$(dotnet msbuild MyLibrary.Core/MyLibrary.Core.csproj /t:GetAssemblyVersion /p:Configuration=Release /nologo /v:quiet | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
    echo "version=$VERSION" >> $GITHUB_OUTPUT

将此步骤放在Create NuGet package之前。

步骤3:可选验证:添加目录检查

在推送前添加步骤确认包已生成在目标目录,方便排查:

- name: Verify package exists
  run: ls -l "${GITHUB_WORKSPACE}/bin/Release"

完整修正后的配置示例

name: Publish MyLibrary.Core NuGet to GitHub Packages

on:
  pull_request:
    branches: [ "master" ]

jobs:
  build-pack-n-ship:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v3
    - name: Setup .NET
      uses: actions/setup-dotnet@v3
      with:
        dotnet-version: 6.0.x
    - name: Restore dependencies
      run: dotnet restore MyLibrary.Core/MyLibrary.Core.csproj
    - name: Build
      run: dotnet build MyLibrary.Core/MyLibrary.Core.csproj --no-restore --configuration Release
    - name: Get project version
      id: get-version
      run: |
        VERSION=$(dotnet msbuild MyLibrary.Core/MyLibrary.Core.csproj /t:GetAssemblyVersion /p:Configuration=Release /nologo /v:quiet | grep -Eo '[0-9]+\.[0-9]+\.[0-9]+')
        echo "version=$VERSION" >> $GITHUB_OUTPUT
    - name: Create NuGet package
      run: dotnet pack MyLibrary.Core/MyLibrary.Core.csproj --configuration Release --output "${GITHUB_WORKSPACE}/bin/Release"
    - name: Add Package Source
      run: dotnet nuget add source --username my_github_username --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/my_company_account/index.json"
    - name: Verify package exists
      run: ls -l "${GITHUB_WORKSPACE}/bin/Release"
    - name: Publish to GitHub Packages
      run: dotnet nuget push "${GITHUB_WORKSPACE}/bin/Release/MyLibrary.Core.${{ steps.get-version.outputs.version }}.nupkg" --source "github"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.02 09:05:14