我有一个 ASP.NET Core API 项目的解决方案,其中包含主 API 项目以及其他两个库项目。
我现在想为库项目创建 NuGet 包,以便我可以在其他应用程序中使用它们。
我是否需要将我的库项目分离到它们自己的解决方案中,并将它们签入到 GitHub 上它们自己的单独存储库中,以便通过 GitHub 操作生成它们的 NuGet 包?
目前 API 和库项目位于一个解决方案中,我将它们保存在同一个存储库中。我是否需要将我的项目拆分到它们自己的存储库中,或者我可以从单个存储库仅为我的库项目创建 NuGet 包吗?
根据我的经验,我建议您将 Nuget 应用程序放入另一个存储库并按照以下说明进行操作。
我已经这样做过很多次了。让我引导您完成它。
登录 nuget.org,然后转到 API 密钥管理并创建密钥。
转到 GitHub 和所需的存储库设置,然后转到 Secrets。创建一个新的密钥并将其粘贴到第一步中创建的 API 密钥。
在根目录下创建文件
< 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
,它执行相同类型的操作,并且将执行相同的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/[email protected]
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 服务器进行身份验证的 API 密钥)NUGET_KEY
因此您需要在存储库中注册一个
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