github action publishing nuget packages

To publish nuget packages for your dotnet application using github action, you can use the following yaml. In github directory structure is pretty straight forward. 

Here is the yaml that you can use. You will notice that i have configure permission for packages. This is to ensure we are able to use the default secret.GITHUB_TOKEN to publish to nuget packages. So we're not using a PAT token here.


name: .NET

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

permissions:
  packages: write

jobs:
  build-and-publish:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        dotnet-version: ['8.0'] # Add all your supported versions

    steps:
    - name: Checkout code
      uses: actions/checkout@v4

    - name: Set up .NET ${{ matrix.dotnet-version }}
      uses: actions/setup-dotnet@v4
      with:
        dotnet-version: ${{ matrix.dotnet-version }}

    - name: Restore dependencies
      run: dotnet restore

    - name: Build for ${{ matrix.dotnet-version }}
      run: dotnet build --configuration Release --framework net${{ matrix.dotnet-version }}

    - name: Publish for ${{ matrix.dotnet-version }}
      run: dotnet publish --configuration Release --framework net${{ matrix.dotnet-version }} -o publish-${{ matrix.dotnet-version }}
   
    - name: Create the package
      run: dotnet pack --configuration Release
   
    - name: Publish the package to GPR
      run: dotnet nuget push bin/Release/*.nupkg --source "https://nuget.pkg.github.com/${{ github.repository_owner }}/index.json" --api-key "${{ secrets.GITHUB_TOKEN }}" --skip-duplicate

    - name: Upload artifact for ${{ matrix.dotnet-version }}
      uses: actions/upload-artifact@v4
      with:
        name: published-artifact-net${{ matrix.dotnet-version }}
        path: publish-${{ matrix.dotnet-version }}

You can see the published nuget packages here



For a comprehensive list of instruction in github action, check out this link here

https://docs.github.com/en/actions/writing-workflows/workflow-syntax-for-github-actions#permissions

Source repository

https://github.com/mitzenjeremywoo/github-action-dotnet-build

 


Comments

Popular posts from this blog

gemini cli getting file not defined error

NodeJS: Error: spawn EINVAL in window for node version 20.20 and 18.20

vllm : Failed to infer device type