使用GitHub Actions创建NuGet包

5

我有一个ASP.NET Core API项目的解决方案,其中包含主API项目和另外两个库项目。

现在我想为库项目创建NuGet包,以便我可以在其他应用程序中使用它们。

我是否需要将库项目分离到它们自己的解决方案中,并将它们检入到GitHub上的不同存储库中,以通过GitHub actions生成它们的NuGet包?

目前API和库项目位于同一个解决方案中,我将它们保存在同一个存储库中。我是否需要将我的项目拆分成它们自己的存储库,或者我是否可以仅从单个存储库为我的库项目创建NuGet包?

3个回答

4
根据我的经验,我建议您将Nuget应用程序放入另一个存储库,并遵循以下说明。
我已经做了很多次。让我带你走过这个过程。
1. 创建API密钥:
登录到nuget.org,然后进入API密钥管理并创建一个密钥。
2. 将API密钥添加到GitHub存储库
转到GitHub和所需的存储库设置,然后到Secrets。创建一个新的secret并将在第一步创建的API密钥粘贴到那里。

enter image description here

3. 添加工作流说明:

在根目录下创建一个文件 < 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

它会:

  • 在发布时触发
  • 在 ubuntu-latest 上运行
  • 设置 .NET SDK
  • 运行 dotnet build 然后测试
  • 打包 nugets 并使用附加的 nuget 密钥将其推送到 nuget.org

4. 创建发布:

  • 在您的存储库主页上找到“Releases”链接
    enter image description here

  • 然后创建一个新的发布
    enter image description here

  • 验证工作流程的成功。
    enter image description here


有用的链接:

  1. Github文档

1
使用 GitHub action linch90/publish-nugetJiyaanswer 的另一种选择,它执行同样的操作,并且会执行相同的 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密钥。

1
使用 actions/setup-dotnet@v3 创建动作。
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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接