name: NuGet Push
on:
push:
tags:
- 'v*'
jobs:
build:
runs-on: windows-latest
steps:
- uses: actions/checkout@v1
- name: Setup .NET Core
uses: actions/setup-dotnet@v1
with:
dotnet-version: 3.1.202
- name: Build and Pack
run: |
dotnet build --configuration Release
dotnet pack --configuration Release
- name: Install Nuget
uses: nuget/setup-nuget@v1
with:
nuget-version: '5.x'
- name: Add private GitHub registry to NuGet
run: |
nuget sources add -name github -Source https://nuget.pkg.github.com/dotnet-campus/index.json -Username dotnet-campus -Password ${{ secrets.GITHUB_TOKEN }}
- name: Push generated package to GitHub registry and NuGet
run: |
nuget push .\bin\Release\*.nupkg -Source github -SkipDuplicate
nuget push .\bin\Release\*.nupkg -Source https://api.nuget.org/v3/index.json -SkipDuplicate -ApiKey ${{ secrets.NugetKey }}
注意:
dotnet pack --configuration Release
可以运行在 .sln
目录,将为所有 sln
包含的 类库
项目打包
dotnet-campus
为你的 GitHub 用户名 ( 或者组织名 )
https://nuget.pkg.github.com/<ORGNAME>/index.json
PS: 我第一次用户名也写错了,不过发现居然还是发布到了我的仓库下packages
补充:
GitHub 官方推荐
cli方式
dotnet nuget add source --username USERNAME --password ${{ secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/OWNER/index.json"
配置文件方式
nuget.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<packageSources>
<clear />
<add key="github" value="https://nuget.pkg.github.com/OWNER/index.json" />
</packageSources>
<packageSourceCredentials>
<github>
<add key="Username" value="USERNAME" />
<add key="ClearTextPassword" value="TOKEN" />
</github>
</packageSourceCredentials>
</configuration>
You must replace:
USERNAME
with the name of your user account on GitHub.TOKEN
with your personal access token.OWNER
with the name of the user or organization account that owns the repository containing your project.注意:
nuget push
后面的路径,一定要使用 Windows 路径分割符\
,不然会报错File does not exist
补充:
dotnet nuget push "bin/Release/OctocatApp.1.0.0.nupkg" --api-key YOUR_GITHUB_PAT --source "github"
注意: 将路径放入双引号中, 即可使用 /
- name: Install Nuget
run: |
$sourceNugetExe = "https://dist.nuget.org/win-x86-commandline/latest/nuget.exe"
$targetNugetExe = "nuget.exe"
Invoke-WebRequest $sourceNugetExe -OutFile $targetNugetExe
shell: pwsh
注意: 若以这种方式下载到当前目录,则运行命令时需要以
.\nuget
开头,因为不在环境变量中
- name: Install Nuget
uses: nuget/setup-nuget@v1
with:
nuget-version: '5.x'
注意:
on:
push:
branches:
- main
tags:
- 'PluginCore-v*'
这样是 或
的关系, 只要满足 branches
为 main
或者 tags
为 PluginCore-v*
就会触发
感谢帮助!
本文作者: yiyun
本文链接: https://moeci.com/posts/分类-github/github-actions-nuget/
版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!