【问题标题】:How to push nuget package in GitHub actions如何在 GitHub 操作中推送 nuget 包
【发布时间】:2020-01-13 07:52:55
【问题描述】:

我正在尝试使用 GitHub 操作从我的项目生成 NuGet 包并将其推送到(私有)GitHub 注册表。

我的脚本([NAME] 字段已编辑):

name: Update NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    name: Update NuGet 
    steps:
      - uses: actions/checkout@master
      - uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '2.2.105'
      - name: Package Release
        run: |  
          cd [SOLUTION_FOLDER]
          dotnet pack -c Release -o out
      - name: Publish Nuget to GitHub registry
        run: dotnet nuget push ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out/$(ls ./[SOLUTION_FOLDER]/[PROJECT_FOLDER]/out) -s https://nuget.pkg.github.com/[USERNAME]/index.json -k ${GITHUB_TOKEN}  
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} 

日志输出:

info : Pushing [PROJECT_FOLDER].3.4.23.nupkg to 'https://nuget.pkg.github.com/[USERNAME]'...
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
info : An error was encountered when fetching 'PUT https://nuget.pkg.github.com/[USERNAME]/'. The request will now be retried.
info : An error occurred while sending the request.
info :   The server returned an invalid or unrecognized response.
info :   PUT https://nuget.pkg.github.com/[USERNAME]/
error: An error occurred while sending the request.
error:   The server returned an invalid or unrecognized response.
##[error]Process completed with exit code 1.

这是对应的 GitHub 问题(带有解决方法选项):https://github.com/NuGet/Home/issues/8580

【问题讨论】:

  • 请添加关于您如何使用dotnet nuget push 命令修复error: Please specify the path to the package. 的说明。
  • @Konard 那是我的错。我没有在 env var 中获得文件名。我通过直接添加命令来解决它dotnet nuget push /path/$(ls ..)
  • 你设法让它工作了吗?我正在尝试同样的事情,但很挣扎。
  • @JustusBurger 我正在联系 GitHub 支持。如果他们可以帮助我解决它,我会发布答案。
  • 为什么不使用Publish NuGet Action

标签: bash github nuget github-actions


【解决方案1】:

第二次更新: 我在jcansdaleGitHub issue 中得到了一个答案,上面写着(还没有测试过):

GitHub Packages 现已添加对 dotnet nuget push --api-key 选项的支持。由于某种原因,这始终有效,但使用基本身份验证(nuget.config 文件中的密码)随机失败!

例子:

  - name: Publish Nuget to GitHub registry
    run: dotnet nuget push ./<project>/out/*.nupkg -k ${GITHUB_TOKEN} -s https://nuget.pkg.github.com/<organization>/index.json --skip-duplicate --no-symbols 
    env:
      GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

更新: 基于Dids answer on GitHub,我的配置现在是这样的:

name: NuGet Generation

on:
  push:
    branches:
      - master
  pull_request:
    types: [closed]
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-18.04
    name: Update NuGet package
    steps:

      - name: Checkout repository
        uses: actions/checkout@v1

      - name: Setup .NET Core @ Latest
        uses: actions/setup-dotnet@v1
        with:
          source-url: https://nuget.pkg.github.com/<organization>/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}        
          
      - name: Build solution and generate NuGet package
        run: |  
          cd <project>
          dotnet pack -c Release -o out  

      - name: Push generated package to GitHub registry
        run: dotnet nuget push ./<project>/out/*.nupkg --skip-duplicate --no-symbols true

注意:在撰写本文时,我需要使用 --no-symbols true 而不是 --no-symbols 来防止 dotnet NuGet 客户端出现异常。


旧答案:

我切换到 Windows 映像并根据 @anangaur 的示例使其工作。这是我的最终代码:

name: NuGet Generation

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: windows-latest
    name: Update NuGet 
    steps:

      - name: Checkout repository
        uses: actions/checkout@master

#  latest image has .NET already installed!
#      - name: Setup .NET environment
#        uses: actions/setup-dotnet@v1
#        with:
#          dotnet-version: '2.2.105' 
          
      - name: Build solution and generate NuGet package
        run: |  
          cd SOLUTION_FOLDER
          dotnet pack -c Release -o out  

      - name: Install NuGet client
        uses: warrenbuckley/Setup-Nuget@v1
        
      - name: Add private GitHub registry to NuGet
        run: nuget sources add -name "GPR" -Source https://nuget.pkg.github.com/ORGANIZATION_NAME/index.json -Username ORGANIZATION_NAME -Password ${{ secrets.GITHUB_TOKEN }}
        
      - name: Push generated package to GitHub registry
        run: nuget push .\SOLUTION_FOLDER\PROJECT_FOLDER\out\*.nupkg -Source "GPR" -SkipDuplicate

【讨论】:

  • 确保在您的csproj 文件中设置RepositoryUrl
  • 我有其他的 cmets,但删除了它们。我有这个问题警告:没有检测到目标存储库。确保源项目定义了“RepositoryUrl”属性。如果您使用的是 nuspec 文件,请确保它具有包含所需“type”和“url”属性的存储库元素。 BadRequest nuget.pkg.github.com/myOrganization 25ms 错误:响应状态码不表示成功:400(错误请求)。我从网址中删除了我的组织实际名称
  • 如果我使用 GitHub Actions source-url,这是否会阻止我也推送到 NuGet.org?其次,我相信--no-symbols 标志现在已修复。
  • 有人知道如何在 .Net Framework 项目中指示 RepositoryUrl 吗?他们确实在 .Net Standard 或 Core 之前添加了该元素。
【解决方案2】:

这是适用于所有平台的解决方法:

name: prerelease NuGet

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest
    # also works with windows-latest and macos-latest
    steps:
    - name: Checkout repository
      uses: actions/checkout@v1
    - name: Build with dotnet
      run: dotnet build --configuration Release --version-suffix prerelease-$(date +%Y%m%d%H%M%S)
      shell: bash
    - name: Publish nuget
      run: |
           for f in ./[repository]/bin/Release/*.nupkg
           do
             curl -vX PUT -u "[user]:${{ secrets.GHPackagesToken }}" -F package=@$f https://nuget.pkg.github.com/[user]/
           done
      shell: bash

注意事项:

  • 这会为每个 git push 创建一个带日期戳的预发布版本并将其上传到 nuget
    • 要使后缀起作用,您需要在.csproj 中设置&lt;VersionPrefix&gt; 而不是&lt;Version&gt;
    • 如果您不想要预发布后缀,请删除 --version-suffix 参数
  • shell 显式设置为 bash,以便与在 Windows 上构建的兼容性
  • 您需要将上面的 [user] 和 [repository] ​​替换为您自己的特定值
    • 您需要创建一个personal access token 并具有 write:packages 的权限
    • 然后创建一个名为 GHPackagesToken 的GitHub Secret 并将上面创建的令牌放入其中
    • 使用 GitHub Secrets 无需使用包含令牌的单独文件
  • 假设您的 .csproj 中有 &lt;GeneratePackageOnBuild&gt;true&lt;/GeneratePackageOnBuild&gt;
    • 如果你不这样做,那么你将需要一个额外的步骤来运行dotnet pack
  • 确保在您的 .csproj 中指定 &lt;RepositoryUrl&gt;...&lt;/RepositoryUrl&gt;
  • 如果您无法使上述代码正常工作,请参阅https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml,它会推送到https://github.com/vslee/IEXSharp/packages(忽略我所有无关的 cmets)
    • 我发布了这个 bc 我尝试了上面 jwillmer 的两个示例,以及 GH 问题线程上的 @anangaur 和 @marza91,但对我来说都不起作用(在任何平台上)
  • 一旦 GitHub 修复了无法在 dotnet nuget push 命令中直接使用 API 密钥的问题(请参阅 initial post of GH issue),那么我们将不再需要此解决方法

【讨论】:

  • 我遇到了一个奇怪的错误,知道吗? curl:(26)无法从文件/应用程序打开/读取本地数据##[错误]进程完成,退出代码为26。
  • 没有看到你的回购,很难说。但听起来 curl 可能难以读取文件(nupkg)?
  • Uppps,很抱歉错过了 yml;这里是... github.com/ardacetinkaya/Blazor.Console/blob/master/.github/… 顺便说一句,我将平台从 windows 更改为 ubuntu 并且该错误消失了,但出现了新的 curl: (26) 读取函数返回有趣的值
  • 好的,您实际上并没有创建 nupkg。因此,您可以取消注释 Pack 步骤,也可以将 true 添加到 .csproj
  • 真丢脸 :) 我对我之前的行为发表了很多评论。谢谢...现在一切都很好。
【解决方案3】:

您可以使用dotnet nuget add source command:

    - name: NuGet push
      run: |
        dotnet nuget add source https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json --name github --username ${{ github.repository_owner }} --password ${{ github.token }} --store-password-in-clear-text
        dotnet nuget push **/*.nupkg --source github

在 linux 环境中运行时,我需要 --store-password-in-clear-text 选项。

使用此方法,无需修改actions/setup-dotnet 任务。 此外,如果需要,此方法将允许您推送到多个 NuGet 流。

【讨论】:

  • 完美,谢谢,正是我所需要的。也可以在末尾与“--skip-duplicate”结合使用。
【解决方案4】:

确保您的项目文件具有以下内容

<PropertyGroup>
    <TargetFramework>netcoreapp3.1</TargetFramework>
    <OutputType>Library</OutputType>
    <PackageId>Example.PackageName</PackageId>
    <Version>1.0.0</Version>
    <Authors>Author Engineering</Authors>
    <Company>Company Inc</Company>
    <PackageDescription>This package for ...!</PackageDescription>
    <RepositoryUrl>
https://github.com/YOUR_ACCOUNT/Example.PackageName</RepositoryUrl>
  </PropertyGroup>

这应该是您用于构建、打包、发布和版本控制的 main.yaml:

name: Continuous Integration

on:
  push:
    branches:
      - master

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v1
    - name: Setup Dotnet Core
      uses: actions/setup-dotnet@v1
      with:
        dotnet-version: 3.1.100
        source-url: https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json
      env:
        NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

    - name: Setup Nuget Config
      run: sed 's/GITHUB_TOKEN/${{ secrets.GITHUB_TOKEN }}/g' .nuget.config > nuget.config

    - name: Build
      run: dotnet build --configuration Release

    - name: Version and Tag
      id: bump_version
      uses: mathieudutour/github-tag-action@v1
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}

    - name: Prep Version String
      run: echo ::set-env name=VERSION_NUMBER::$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')

    - name: Define Package Name
      run: echo ::set-env name=PACKAGE_NAME::$"Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg"

    - name: Set Nuget Package Version
      uses: roryprimrose/set-vs-sdk-project-version@v1
      with:
        version: ${{ env.VERSION_NUMBER }}

    - name: Pack
      run: dotnet pack --configuration Release Example.PackageName

    - name: Publish Package
      run: dotnet nuget push Example.PackageName/bin/Release/*.nupkg --source https://nuget.pkg.github.com/YOUR_ACCOUNT/index.json

    - name: Create Release
      uses: actions/create-release@v1
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
      with:
        tag_name: ${{ steps.bump_version.outputs.new_tag }}
        release_name: Release ${{ github.ref }}

【讨论】:

  • 设置环境变量已更改:docs.github.com/en/actions/reference/...。例如:运行:echo "VERSION_NUMBER=$(echo ${{ steps.bump_version.outputs.new_tag }} | sed 's/[v]//g')" >> $GITHUB_ENV 并运行:echo 'PACKAGE_NAME=$ "Example.PackageName/bin/Release/Example.PackageName.${{ env.VERSION_NUMBER }}.nupkg" ' >> $GITHUB_ENV
【解决方案5】:

我的工作解决方案:

  • 将 'usernamecompanyname' 替换为您的 repo 的有效值
  • 我将构建和打包分开,以便在出现问题时更轻松地进行调试
  • 您可以将 github 机密中的 ACTIONS_RUNNER_DEBUG 变量设置为 true 以进行更详细的调试
  • dotnet-version 更改为您想要的 dotnet-sdk 版本
  • 无需在你的 github repo secrests 中指定GITHUB_TOKEN,这个令牌默认存在

build_and_publish_nuget.yml:

name: Build and publish package

# Controls when the action will run. Triggers the workflow on push or pull request 
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  # This workflow contains a single job called "build"
  build:
    # The type of runner that the job will run on
    runs-on: ubuntu-latest
    steps:
      - name: Checkout repository
        uses: actions/checkout@master

      - name: Setup .NET environment
        uses: actions/setup-dotnet@v1
        with:
          dotnet-version: '3.1.102'
          source-url: https://nuget.pkg.github.com/usernamecompanyname/index.json
        env:
          NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}

      - name: Build project
        run: dotnet build -c Release

      - name: Generate a NuGet package
        run: dotnet pack --no-build -c Release -o .

      - name: Push to GitHub package registry
        run: dotnet nuget push *.nupkg

【讨论】:

  • 知道如何从另一个仓库(在同一组织中)使用这个包吗?
【解决方案6】:

GitHub 在将 NuGet 包发布到 GitHub 包时遇到间歇性问题。我寻求支持,他们给了我两个选择。

选项 1:卷曲

curl -vX PUT -u "<username>:<TOKEN>" -F package=@PATH-TO-PKG-FILE.nupkg https://nuget.pkg.github.com/<OWNER>/

选项 2:DOTNET GPR 工具
https://github.com/jcansdale/gpr

dotnet tool install gpr -g
gpr push PATH-TO-PKG-FILE.nupkg -k <TOKEN>

我在我的 GitHub 操作工作流程中选择了 选项 2

$file = Get-ChildItem -Path <where I output my nupkg file to> -Recurse -Filter *.nupkg | Select -First 1
gpr push $file.FullName -k ${{secrets.GITHUB_TOKEN}}          

【讨论】:

  • 或者你可以使用nuget.exe而不是dotnet nuget来推送到GPR。这应该始终如一。
【解决方案7】:

其他答案太长了,我不知道为什么。我就是这样做的:

对于 NuGet.org:

- name: Push Package to NuGet.org
  run: dotnet nuget push *.nupkg -k ${{ secrets.NUGET_ORG_API_KEY }} -s https://api.nuget.org/v3/index.json

对于 GitHub.com:

- name: Push Package to GitHub.com
  run: dotnet nuget push *.nupkg -k ${{ secrets.GITHUB_TOKEN }} -s https://nuget.pkg.github.com/USERNAME/index.json

【讨论】:

  • 如果您阅读最初的问题,您会发现您的解决方案在过去不起作用。今天很可能就是这种情况。
猜你喜欢
  • 2022-06-15
  • 2020-12-18
  • 2020-12-08
  • 1970-01-01
  • 2022-11-02
  • 1970-01-01
  • 2022-11-24
  • 2020-01-15
  • 2022-11-25
相关资源
最近更新 更多