Create NuGet Package with GitHub Actions
Asked Answered
K

3

5

I have a solution for an ASP.NET Core API project that contains the main API project as well as two other library projects.

I now want to create NuGet packages for the library projects so that I can use them in other applications.

Do I need to separate my library projects into their own solutions and check them into their own separate repositories on GitHub in order to generate their NuGet packages through GitHub actions?

Currently the API and the library projects are in one solution and I keep them in the same repository. Do I need to split my projects into their own repositories or can I create NuGet packages only for my library projects from a single repository?

Kliman answered 27/12, 2022 at 8:15 Comment(0)
S
8

As per my experience, I suggest you to put the Nuget Application into another repository and follow the below instructions.

I've done this many times. Let me walk you through it.

1. Create API Key:

Sign in to nuget.org then go to the API Keys management and create a key.

2. Add the API key to GitHub repository

Go to GitHub and desired repository settings, then to Secrets. Create a new secret and paste there API key created on the first step.

enter image description here

3. Add workflow instructions:

Create a file under the root < 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

It does:

  • triggers on release publish
  • runs on ubuntu-latest
  • setup .NET SDK
  • runs dotnet build then tests
  • packs nugets and push it nuget.org using the attached nuget key

4. Create release:

  • Find the Releases link on the main page of your repo
    enter image description here

  • Then create a new release
    enter image description here

  • Verify the success of workflow.
    enter image description here


Helpful Links:

  1. Github Doc
Stackhouse answered 29/12, 2022 at 13:32 Comment(0)
M
2

Create the action using 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
Madame answered 5/10, 2023 at 17:17 Comment(0)
C
1

The alternative to Jiya's answer is to use the GitHub action linch90/publish-nuget, which does the same kind of operation, and will do the same 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

Project gets published only if there's a NUGET_KEY configured in the repository (API key to authenticate with NuGet server)

So you need to register a NUGET_KEY secret in your repository.

Chickamauga answered 29/12, 2022 at 16:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.