Search code examples
githubnugetgithub-actionsnuget-package.net-standard-2.0

Create NuGet Package with GitHub Actions


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?


Solution

  • 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