September 15, 20258 min readBy Prithivi Kai Balaji

NuGet Publish: Complete Guide to Publishing Packages

NuGetGitHub Packages.NETDevOpsCI/CD

Publishing NuGet Packages to GitHub Packages: A Complete Guide


GitHub Packages provides a secure, reliable way to host your NuGet packages alongside your source code. This comprehensive guide will walk you through the entire process from creating a package to publishing it to GitHub Packages.


🎯 What You'll Learn


  • ✅ Create and configure NuGet package projects
  • ✅ Set up GitHub Personal Access Tokens
  • ✅ Build and package your .NET libraries
  • ✅ Publish packages to GitHub Packages
  • ✅ Automate publishing with GitHub Actions
  • ✅ Consume packages from GitHub Packages

  • 📁 GitHub Repository


    You can find the complete source code and examples in the GitHub repository:

    View Complete Source Code on GitHub 🚀


    📋 Prerequisites


    Before you begin, ensure you have:


  • GitHub Account - A GitHub account with repository access
  • .NET SDK - .NET 8.0 SDK or later installed on your machine
  • GitHub Personal Access Token (PAT) - With appropriate package permissions
  • Development Environment - Visual Studio, VS Code, or your preferred IDE

  • ---


    🚀 Step 1: Create Your NuGet Package Project


    1.1 Create a Class Library Project


    Start by creating a new class library project:


    BASH
    dotnet new classlib -n MyAwesomeLibrary
    cd MyAwesomeLibrary
    

    1.2 Configure Package Metadata


    Edit your .csproj file to include package metadata:


    XML
    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        
        <!-- Package Information -->
        <PackageId>MyAwesomeLibrary</PackageId>
        <Version>1.0.0</Version>
        <Authors>Your Name</Authors>
        <Company>Your Company</Company>
        <Description>A fantastic library for amazing functionality</Description>
        <PackageTags>utility, helper, awesome</PackageTags>
        <PackageLicenseExpression>MIT</PackageLicenseExpression>
        <PackageReadmeFile>README.md</PackageReadmeFile>
        
        <!-- GitHub Packages Configuration -->
        <RepositoryUrl>https://github.com/Prithivi-au/nuget-publish</RepositoryUrl>
        <RepositoryType>git</RepositoryType>
      </PropertyGroup>
    </Project>
    

    🔑 Step 2: Set Up GitHub Personal Access Token


    2.1 Create a Personal Access Token


    1. Go to GitHub Settings → Developer settings → Personal access tokens → Tokens (classic)

    2. Click "Generate new token (classic)"

    3. Select the following scopes:

    - write:packages - Upload packages to GitHub Packages

    - read:packages - Download packages from GitHub Packages

    - repo - Access to repositories (if your package is in a private repo)


    2.2 Configure NuGet Source


    Add GitHub Packages as a NuGet source:


    BASH
    dotnet nuget add source https://nuget.pkg.github.com/Prithivi-au/index.json \
      --name github \
      --username Prithivi-au \
      --password YOUR_PERSONAL_ACCESS_TOKEN \
      --store-password-in-clear-text
    

    📦 Step 3: Build and Package Your Library


    3.1 Build the Package


    BASH
    dotnet pack --configuration Release
    

    This creates a .nupkg file in the bin/Release folder.


    3.2 Publish to GitHub Packages


    BASH
    dotnet nuget push bin/Release/MyAwesomeLibrary.1.0.0.nupkg \
      --source github \
      --api-key YOUR_PERSONAL_ACCESS_TOKEN
    

    🤖 Step 4: Automate with GitHub Actions


    Create .github/workflows/publish.yml:


    YAML
    name: Publish Package
    
    on:
      push:
        branches:
          - main
        tags:
          - 'v*'
    
    jobs:
      publish:
        runs-on: ubuntu-latest
        steps:
        - uses: actions/checkout@v3
          with:
            fetch-depth: 0  # Fetch all history for version calculation
        
        - name: Setup .NET
          uses: actions/setup-dotnet@v3
          with:
            dotnet-version: '8.0.x'
            
        - name: Calculate Version
          id: version
          run: |
            # This step dynamically generates package versions:
            # - For tagged releases (v1.0.0): Uses the tag version
            # - For main branch commits: Creates semantic version like 1.0.42-abc1234
            #   where 42 is commit count and abc1234 is short commit SHA
            if [[ \$GITHUB_REF == refs/tags/* ]]; then
              VERSION=\$\{GITHUB_REF#refs/tags/v\}
            else
              # Generate version from git info
              COMMIT_COUNT=\$(git rev-list --count HEAD)
              SHORT_SHA=\$(git rev-parse --short HEAD)
              VERSION="1.0.\$\{COMMIT_COUNT\}-\$\{SHORT_SHA\}"
            fi
            echo "VERSION=\$VERSION" >> \$GITHUB_OUTPUT
            echo "Generated version: \$VERSION"
            
        - name: Set environment variables
          run: |
            echo "REPO_OWNER_LC=\$\{GITHUB_REPOSITORY_OWNER,,\}" >> \$GITHUB_ENV
            
        - name: Restore dependencies
          run: dotnet restore MyAwesomeLibrary/MyAwesomeLibrary.sln
          
        - name: Build
          run: dotnet build MyAwesomeLibrary/MyAwesomeLibrary.sln --no-restore
          
        - name: Test
          run: dotnet test MyAwesomeLibrary/MyAwesomeLibrary.sln --no-build --verbosity normal
          
        - name: Pack
          run: dotnet pack MyAwesomeLibrary/MyAwesomeLibrary/MyAwesomeLibrary.csproj --configuration Release -p:Version=\$\{\{ steps.version.outputs.VERSION \}\} --output ./nupkgs
          
        - name: List created packages
          run: |
            echo "Created packages:"
            ls -la ./nupkgs/
          
        - name: Push all packages to GitHub Packages
          if: startsWith(github.ref, 'refs/tags/') || github.ref == 'refs/heads/main'
          run: |
            dotnet nuget push "./nupkgs/*.nupkg" \\
              --source "https://nuget.pkg.github.com/\$\{\{ env.REPO_OWNER_LC \}\}/index.json" \\
              --api-key \$\{\{ secrets.NUGET_PUBLISH_TOKEN \}\} \\
              --skip-duplicate
    

    📥 Step 5: Consume Your Package


    5.1 Add GitHub Packages Source


    BASH
    dotnet nuget add source https://nuget.pkg.github.com/Prithivi-au/index.json \
      --name github \
      --username Prithivi-au \
      --password YOUR_PERSONAL_ACCESS_TOKEN \
      --store-password-in-clear-text
    

    5.2 Install the Package


    BASH
    dotnet add package MyAwesomeLibrary --source github
    

    🎉 Conclusion


    You've successfully:

  • ✅ Created a NuGet package project
  • ✅ Configured GitHub Packages as a NuGet source
  • ✅ Published your package to GitHub Packages
  • ✅ Set up automated publishing with GitHub Actions
  • ✅ Consumed your package from GitHub Packages

  • GitHub Packages provides a seamless way to host your NuGet packages alongside your source code, making it easy to share and distribute your .NET libraries.


    🔗 Additional Resources


  • GitHub Packages Documentation
  • NuGet Package Creation Guide
  • GitHub Actions Documentation
  • NuGet Package Repository

    Get the complete NuGet package source code, GitHub Actions workflows, and step-by-step examples for publishing packages to GitHub Packages.

    View on GitHub