如何在 GitHub 操作中推送 nuget 包
How to push nuget package in GitHub actions
我正在尝试使用 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
第二次更新:
我在 jcansdale
的 GitHub issue 中得到了一个答案,上面写着(还没有测试过):
Support for the dotnet nuget push --api-key option has now been added to GitHub Packages. For some reason this works consistently, but using basic auth (password in nuget.config file) fails randomly!
示例:
- 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
这是一个适用于所有平台的解决方法:
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 推送创建一个带日期戳的预发布版本并将其上传到 nuget
- 要使后缀生效,您需要在 .csproj
中设置 <VersionPrefix>
而不是 <Version>
- 如果您不想要预发布后缀,请删除 --version-suffix 参数
- shell 明确设置为 bash 以便与 windows
上的构建兼容
- 您需要将上面的 [user] 和 [repository] 替换为您自己的特定值
- 您需要创建一个具有 write:packages
权限的 personal access token
- 然后创建一个 GitHub Secret 名为 GHPackagesToken 并将上面创建的令牌放在那里
- 使用 GitHub Secrets 不再需要包含您的令牌的单独文件
- 这假设您的 .csproj 中有
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
- 如果你不这样做,那么你将需要一个额外的步骤运行
dotnet pack
- 确保在您的 .csproj
中指定 <RepositoryUrl>...</RepositoryUrl>
- 如果您无法使上述代码正常工作,请参阅 https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml, which pushes to https://github.com/vslee/IEXSharp/packages(忽略我所有无关的评论)
- 我发布了这个 bc 我尝试了上面 jwillmer 的两个例子,以及 GH 问题线程上的 @anangaur 和 @marza91,但都不适合我(在任何平台上)
- 一旦GitHub修复了无法直接在
dotnet nuget push
命令中使用API键的问题(参见initial post of GH issue),那么我们就不会不再需要此解决方法
我的工作解决方案:
- 将 'usernamecompanyname' 替换为您的存储库的有效值
- 我将构建和打包分开,以便在出现问题时更容易调试
- 您可以将 github 秘密中的
ACTIONS_RUNNER_DEBUG
变量设置为 true
以允许更详细的调试
- 将
dotnet-version
更改为您想要的 dotnet-sdk 版本
- 无需在您的 github 回购机密中指定
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
确保您的项目文件具有以下内容
<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 }}
您可以使用 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 流。
GitHub 在将 NuGet 包发布到 GitHub 包时遇到间歇性问题。我伸出援手,他们给了我两个选择。
选项 1:CURL
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.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
我正在尝试使用 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
第二次更新:
我在 jcansdale
的 GitHub issue 中得到了一个答案,上面写着(还没有测试过):
Support for the dotnet nuget push --api-key option has now been added to GitHub Packages. For some reason this works consistently, but using basic auth (password in nuget.config file) fails randomly!
示例:
- 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
这是一个适用于所有平台的解决方法:
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 推送创建一个带日期戳的预发布版本并将其上传到 nuget
- 要使后缀生效,您需要在 .csproj 中设置
- 如果您不想要预发布后缀,请删除 --version-suffix 参数
<VersionPrefix>
而不是<Version>
- shell 明确设置为 bash 以便与 windows 上的构建兼容
- 您需要将上面的 [user] 和 [repository] 替换为您自己的特定值
- 您需要创建一个具有 write:packages 权限的 personal access token
- 然后创建一个 GitHub Secret 名为 GHPackagesToken 并将上面创建的令牌放在那里
- 使用 GitHub Secrets 不再需要包含您的令牌的单独文件
- 这假设您的 .csproj 中有
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
- 如果你不这样做,那么你将需要一个额外的步骤运行
dotnet pack
- 如果你不这样做,那么你将需要一个额外的步骤运行
- 确保在您的 .csproj 中指定
- 如果您无法使上述代码正常工作,请参阅 https://github.com/vslee/IEXSharp/blob/master/.github/workflows/dotnetcore.yml, which pushes to https://github.com/vslee/IEXSharp/packages(忽略我所有无关的评论)
- 我发布了这个 bc 我尝试了上面 jwillmer 的两个例子,以及 GH 问题线程上的 @anangaur 和 @marza91,但都不适合我(在任何平台上)
- 一旦GitHub修复了无法直接在
dotnet nuget push
命令中使用API键的问题(参见initial post of GH issue),那么我们就不会不再需要此解决方法
<RepositoryUrl>...</RepositoryUrl>
我的工作解决方案:
- 将 'usernamecompanyname' 替换为您的存储库的有效值
- 我将构建和打包分开,以便在出现问题时更容易调试
- 您可以将 github 秘密中的
ACTIONS_RUNNER_DEBUG
变量设置为true
以允许更详细的调试 - 将
dotnet-version
更改为您想要的 dotnet-sdk 版本 - 无需在您的 github 回购机密中指定
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
确保您的项目文件具有以下内容
<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 }}
您可以使用 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 流。
GitHub 在将 NuGet 包发布到 GitHub 包时遇到间歇性问题。我伸出援手,他们给了我两个选择。
选项 1:CURL
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.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