我有一个ASP.NET Core API项目的解决方案,其中包含主API项目和另外两个库项目。
现在我想为库项目创建NuGet包,以便我可以在其他应用程序中使用它们。
我是否需要将库项目分离到它们自己的解决方案中,并将它们检入到GitHub上的不同存储库中,以通过GitHub actions生成它们的NuGet包?
目前API和库项目位于同一个解决方案中,我将它们保存在同一个存储库中。我是否需要将我的项目拆分成它们自己的存储库,或者我是否可以仅从单个存储库为我的库项目创建NuGet包?
我有一个ASP.NET Core API项目的解决方案,其中包含主API项目和另外两个库项目。
现在我想为库项目创建NuGet包,以便我可以在其他应用程序中使用它们。
我是否需要将库项目分离到它们自己的解决方案中,并将它们检入到GitHub上的不同存储库中,以通过GitHub actions生成它们的NuGet包?
目前API和库项目位于同一个解决方案中,我将它们保存在同一个存储库中。我是否需要将我的项目拆分成它们自己的存储库,或者我是否可以仅从单个存储库为我的库项目创建NuGet包?
在根目录下创建一个文件
< YOUR REPO > /.github/workflows/release.yml
name: Release to NuGet
on:
release:
types: [published]
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup .NET SDK
uses: actions/setup-dotnet@v1
- name: Build
run: dotnet build -c Release
- name: Test
run: dotnet test -c Release --no-build
- name: Pack nugets
run: dotnet pack -c Release --no-build --output .
- name: Push to NuGet
run: dotnet nuget push "*.nupkg" --api-key ${{secrets.nuget_api_key}} --source https://api.nuget.org/v3/index.json
它会:
有用的链接:
linch90/publish-nuget
是 Jiya 的 answer 的另一种选择,它执行同样的操作,并且会执行相同的 dotnet nuget push
。name: publish to nuget
on:
push:
branches:
- master # Default release branch
jobs:
publish:
name: build, pack & publish
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
# - name: Setup dotnet
# uses: actions/setup-dotnet@v1
# with:
# dotnet-version: 6.0.0
# Publish
- name: publish on version change
id: publish_nuget
uses: alirezanet/publish-nuget@v3.0.0
with:
# Filepath of the project to be packaged, relative to root of repository
PROJECT_FILE_PATH: Core/Core.csproj
# NuGet package id, used for version detection & defaults to project name
# PACKAGE_NAME: Core
# Filepath with version info, relative to root of repository & defaults to PROJECT_FILE_PATH
# VERSION_FILE_PATH: Directory.Build.props
# Regex pattern to extract version info in a capturing group
# VERSION_REGEX: ^\s*<Version>(.*)<\/Version>\s*$
# Useful with external providers like Nerdbank.GitVersioning, ignores VERSION_FILE_PATH & VERSION_REGEX
# VERSION_STATIC: 1.0.0
# Flag to toggle git tagging, enabled by default
# TAG_COMMIT: true
# Format of the git tag, [*] gets replaced with actual version
# TAG_FORMAT: v*
# API key to authenticate with NuGet server
# NUGET_KEY: ${{secrets.NUGET_API_KEY}}
# NuGet server uri hosting the packages, defaults to https://api.nuget.org
# NUGET_SOURCE: https://api.nuget.org
# Flag to toggle pushing symbols along with nuget package to the server, disabled by default
# INCLUDE_SYMBOLS: false
# Flag to toggle not building the project and letting pack command handle restoring & building, disabled by default
# NO_BUILD: false
NUGET_KEY
(用于与NuGet服务器进行身份验证的API密钥),项目才会被发布。因此,您需要在您的代码库中注册一个NUGET_KEY
密钥。name: Publish Bolivia Package
runs-on: ubuntu-latest
permissions:
contents: read
packages: write # permission for the github token
steps:
- uses: actions/checkout@v3
- uses: actions/setup-dotnet@v3
with:
dotnet-version: '3.1.x' # No change it
source-url: https://nuget.pkg.github.com/<owner|organization>/index.json
env:
NUGET_AUTH_TOKEN: ${{secrets.GITHUB_TOKEN}}
- name: Build project
run: dotnet build --configuration Release
- name: Create the package
run: dotnet pack --configuration Release
- name: Publish the package to GPR
run: dotnet nuget push bin/Release/*.nupkg