
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.
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.
To publish a package to NuGet, you’ll need an API key from your NuGet account.
NUGET_API_KEY
and paste the API key you copied from NuGet.Now, we’ll create a workflow file that tells GitHub Actions how to build your .NET project and push the package to NuGet.
.github/workflows
.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
main
branch.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.
Your insights drive us! For any questions, feedback, or thoughts, feel free to connect:
If you found this guide beneficial, don’t hesitate to share it with your network. Until the next guide, happy coding!
Quick Links
Legal Stuff