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

使用GitHub Actions推送NuGet包到GitHub Packages遇问题求助

解决GitHub Actions推送NuGet包到GitHub Packages的报错问题

问题原因

你原有的dotnet nuget push命令错误使用了--username参数,该参数属于dotnet nuget add source命令的选项,而非push命令,因此触发"Unrecognized option '--username'"报错。

正确推送方案

你提供的release.yml已拆分添加源和推送步骤,只需调整细节即可正常运行:

1. 修正NuGet源添加步骤

将YOUR_GITHUB_USERNAME替换为你的GitHub用户名,YOUR_GITHUB_ACCOUNT替换为你的个人/组织账户名:

dotnet nuget add source --username YOUR_GITHUB_USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/YOUR_GITHUB_ACCOUNT/index.json"

2. 正确的推送命令

推送时直接指定已添加的源名称即可,无需重复输入认证信息,建议添加--skip-duplicate避免同一版本重复推送报错:

dotnet nuget push "bin/Release/MyApp.${{ steps.get-version.outputs.version }}.nupkg" --source github --skip-duplicate

完整修正后的release.yml

# This workflow will build a .NET project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-net

name: Publish MyApp 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: Get version
      uses: kzrnm/get-net-sdk-project-versions-action@v1
      id: get-version
      with:
        proj-path: MyApp/MyApp.csproj
    - name: Add GitHub Packages as Source
      run: dotnet nuget add source --username YOUR_GITHUB_USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/YOUR_GITHUB_ACCOUNT/index.json"
    - name: Publish to GitHub Packages
      run: dotnet nuget push "bin/Release/MyApp.${{ steps.get-version.outputs.version }}.nupkg" --source github --skip-duplicate

额外注意事项

  • 确保GITHUB_TOKEN拥有write:packages权限,GitHub Actions默认的仓库级GITHUB_TOKEN已包含该权限
  • 若使用组织账户,需确认仓库的Packages权限设置允许该token执行推送操作

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 06:35:50