我正在尝试使用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上最核心的问题(带有解决方法):here
答案 0 :(得分:6)
我切换到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
答案 1 :(得分:4)
这是在所有平台上都可以使用的解决方法:
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
注意:
<VersionPrefix>
而不是<Version>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
dotnet pack
<RepositoryUrl>...</RepositoryUrl>
dotnet nuget push
命令(请参阅initial post of GH issue)中使用API密钥的问题,那么我们将不再需要这种解决方法答案 2 :(得分:3)
确保您的项目文件具有以下内容
<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 }}
答案 3 :(得分:2)
我的工作解决方案:
ACTIONS_RUNNER_DEBUG
变量设置为true
,以进行更详细的调试dotnet-version
更改为所需的dotnet-sdk版本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
答案 4 :(得分:2)
您可以使用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流。
答案 5 :(得分:1)
GitHub在将NuGet程序包发布到GitHub程序包时遇到间歇性问题。我伸出援手,他们给了我两个选择。
选项1:CURL
import json
json.loads(json_str)
选项2:DOTNET GPR工具
https://github.com/jcansdale/gpr
curl -vX PUT -u "<username>:<TOKEN>" -F package=@PATH-TO-PKG-FILE.nupkg https://nuget.pkg.github.com/<OWNER>/
我在GitHub Action Workflow中选择了选项2 :
dotnet tool install gpr -g
gpr push PATH-TO-PKG-FILE.nupkg -k <TOKEN>