HomeContact

Automate .NET Package Deployment to NuGet with GitHub Actions

بواسطة Shady Nagy
نُشر في dotnet
October 01, 2024
1 دقيقة للقراءة
Automate .NET Package Deployment to NuGet with GitHub Actions
هذا المحتوى غير متوفر باللغة العربية. يتم عرض النسخة الأصلية.

جدول المحتويات

01
Introduction
02
Step 1: Prepare Your .NET Project for NuGet
03
Step 2: Set Up NuGet API Key
04
Step 3: Add API Key as GitHub Secret
05
Step 4: Create GitHub Action Workflow File
06
Step 5: Test the Workflow
07
Conclusion
08
Feedback and Questions

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.

  1. Go to NuGet.org.
  2. Sign in and navigate to your account settings.
  3. Create a new API key with Push permissions for the specific package or set it to allow all packages.
  4. Copy the API key—you’ll use it in the GitHub repository as a secret.

Step 3: Add API Key as GitHub Secret

  1. Go to your GitHub repository.
  2. Click on Settings > Secrets and variables > Actions.
  3. Create a new secret with the name NUGET_API_KEY and 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.

  1. In your repository, create a folder called .github/workflows.
  2. 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 NuGet
on:
push:
branches:
- main
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Set up .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: '8.0.x'
- name: Restore dependencies
run: dotnet restore
- name: Build the project
run: dotnet build --configuration Release --no-restore
- name: Pack the project
run: dotnet pack --configuration Release --no-build --output ./nupkg
- name: Push the package to NuGet
run: dotnet nuget push ./nupkg/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json

Step 5: Test the Workflow

  1. Push changes to your main branch.
  2. GitHub Actions will automatically trigger the workflow, build your project, and publish it to NuGet.
  3. 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:

  1. Email: shady@shadynagy.com
  2. Twitter: @ShadyNagy_
  3. LinkedIn: Shady Nagy
  4. GitHub: ShadyNagy

If you found this guide beneficial, don’t hesitate to share it with your network. Until the next guide, happy coding!


الوسوم

#.NETCore#DotNet#DotNet8#GitHubActions#NuGet#NuGetDeployment#CI_CD#PackageVersioning#AutomatedDeployment#SoftwareDevelopment#DevOps#ContinuousIntegration#ContinuousDeployment#BestPractices

مشاركة


Shady Nagy

Shady Nagy

Software Innovation Architect

المواضيع

Angular
AI
GatsbyJS
dotnet
Github
Linux
MS SQL
Oracle

مقالات ذات صلة

تداخل الملفات في Visual Studio
تداخل الملفات في Visual Studio
June 08, 2021
2 د

Quick Links

Contact Us

وسائل التواصل الاجتماعي