In this article we will learn how to make a CI pipeline to automate the process of publishing NuGet packages using the GitHub Actions Workflows.
Let’s make a simple NuGet Package to test this functionality.
First of all, make a Class Library project in dotnet by running the following CLI command on the terminal.
dotnet new classlib -n HelloWorldNuGetPkg
Add the following code to HelloWorld.cs file.
using System;
namespace HelloWorldLib
{
public class HelloWorld
{
public static void Print()
{
Console.WriteLine("Hello, World!");
}
}
}
Now, add the following code to your HelloWorldNuGetPkg.csproj file.
<PackageId>YourPackageName</PackageId>
<authors>YourName</authors>
<Company>YourOrganization</Company>
<Description>Some Meaningful Description</Description>
<PackageTags>Tags related to package</PackageTags>
<PackageVersion>1.0.0</PackageVersion>
<RepositoryUrl>YourGithubRepoURL</RepositoryUrl>
Let’s check if the package is getting build properly or not. Run the following command to verify.
dotnet build
Building the GitHub Pipeline
When making a pipeline the initial step is to make sure the code can be built and all tests pass successfully after pushing to the main branch. To achieve this, we’ll set up GitHub Actions by adding a new configuration file.
Create a file named .github/workflows/ci.yml in your repository with the following content and push it to the main branch. This configuration includes two straightforward steps: one for building the code and another for testing it.
name: Publish NuGet Package
on:
push:
tags:
- 'v*.*.*'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Set up .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: '8.x' # Adjust to your target .NET version
- name: Restore dependencies
run: dotnet restore
- name: Build project
run: dotnet build --configuration Release --no-restore
- name: Pack project
run: dotnet pack --configuration Release --no-restore --output ./nupkg
- name: Publish package to NuGet
env:
NUGET_API_KEY: ${{ secrets.NUGET_API_KEY }}
run: dotnet nuget push ./nupkg/*.nupkg --source https://api.nuget.org/v3/index.json --api-key $NUGET_API_KEY
Explanation
- on: push: tags:: This triggers the workflow when you push a tag that follows the version pattern v*.*.* (e.g., v1.0.0).
- jobs: build:: Defines a job named build that runs on the latest Ubuntu runner.
- steps::
- Checkout repository: Uses the checkout action to pull your repository’s code.
- Set up .NET Core: Sets up the specified .NET Core version.
- Restore dependencies: Runs dotnet restore to restore project dependencies.
- Build project: Builds the project in Release configuration.
- Pack project: Creates a NuGet package and outputs it to the ./nupkg directory.
- Publish package to NuGet: Uses the dotnet nuget push command to publish the package to nuget.org, utilizing the NUGET_API_KEY secret for authentication.
Setting Up Secrets
To securely manage your NuGet API key, add it to your repository’s secrets:
- Go to your GitHub repository.
- Navigate to Settings > Secrets and variables > Actions.
- Click New repository secret.
- Add NUGET_API_KEY as the name and your NuGet API key as the value.
Benefits
1. Consistency and Reliability
Automated workflows ensure that each step of the process—building, testing, and publishing—is performed consistently. This reduces the risk of human error, ensuring that each package is built and tested in the same environment and manner every time.
2. Time-Saving
Manual processes for building, testing, and publishing packages can be time-consuming. Automation frees up developers’ time, allowing them to focus on writing code and developing features rather than managing releases.
3. Continuous Integration and Continuous Deployment (CI/CD)
Integrating GitHub Actions into your workflow facilitates CI/CD practices. Every change pushed to the repository can trigger the workflow, ensuring that code is continuously integrated, tested, and deployed. This leads to faster feedback loops and quicker identification of issues.
4. Improved Code Quality
Automated testing as part of the workflow ensures that any new changes are validated before they are merged or released. This helps in maintaining high code quality and reduces the likelihood of bugs making it into production.
Conclusion
By following this guide, you have set up a GitHub Actions workflow to automate the process of building, testing, and publishing NuGet packages. This automation enhances your CI/CD pipeline, ensuring that your NuGet packages are consistently updated and available for use.