Automate .NET Package Deployment to NuGet with GitHub Actions

Table Of Contents
Introduction
In this guide, we’ll walk through how to set up GitHub Actions to automatically publish your .NET package to NuGet whenever you push new changes to your repository. GitHub Actions simplifies the CI/CD process by automating various workflows, and this tutorial focuses on deploying a .NET package with minimal setup.
Step 1: Prepare Your .NET Project for NuGet
Before setting up GitHub Actions, make sure your .NET project is ready for deployment to NuGet. At minimum, you need a .csproj file with relevant metadata.
Here’s an example of what your .csproj file might look like:
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><TargetFramework>net8.0</TargetFramework><PackageId>YourPackageName</PackageId><Version>1.0.0</Version><Authors>YourName</Authors><Description>This is a sample package description.</Description><PackageReleaseNotes>Release notes for version 1.0.0.</PackageReleaseNotes><RepositoryUrl>https://github.com/yourusername/yourrepo</RepositoryUrl></PropertyGroup></Project>
Make sure you replace the placeholders like YourPackageName, YourName, and https://github.com/yourusername/yourrepo with real data.
Step 2: Set Up NuGet API Key
To publish a package to NuGet, you’ll need an API key from your NuGet account.
- Go to NuGet.org.
- Sign in and navigate to your account settings.
- Create a new API key with Push permissions for the specific package or set it to allow all packages.
- Copy the API key—you’ll use it in the GitHub repository as a secret.
Step 3: Add API Key as GitHub Secret
- Go to your GitHub repository.
- Click on Settings > Secrets and variables > Actions.
- Create a new secret with the name
NUGET_API_KEYand paste the API key you copied from NuGet.
Step 4: Create GitHub Action Workflow File
Now, we’ll create a workflow file that tells GitHub Actions how to build your .NET project and push the package to NuGet.
- In your repository, create a folder called
.github/workflows. - Inside this folder, create a file named
deploy.yml.
Here’s a simple example of what the deploy.yml file should look like:
name: Publish .NET package to NuGeton:push:branches:- mainjobs:build:runs-on: ubuntu-lateststeps:- name: Checkout codeuses: actions/checkout@v4- name: Set up .NETuses: actions/setup-dotnet@v4with:dotnet-version: '8.0.x'- name: Restore dependenciesrun: dotnet restore- name: Build the projectrun: dotnet build --configuration Release --no-restore- name: Pack the projectrun: dotnet pack --configuration Release --no-build --output ./nupkg- name: Push the package to NuGetrun: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json
Step 5: Test the Workflow
- Push changes to your
mainbranch. - GitHub Actions will automatically trigger the workflow, build your project, and publish it to NuGet.
- You can monitor the workflow’s progress under the Actions tab of your repository.
Conclusion
That’s it! With just a few steps, you’ve successfully automated the deployment of your .NET package to NuGet using GitHub Actions. Now, every time you push a new update to the main branch, your package will be published without any manual steps.
Feedback and Questions
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
- Email: shady@shadynagy.com
- Twitter: @ShadyNagy_
- LinkedIn: Shady Nagy
- GitHub: ShadyNagy
If you found this guide beneficial, don’t hesitate to share it with your network. Until the next guide, happy coding!







