Lab: C# Extension Methods - Publish GitHub Package
This lab will show you how to create a GitHub Actions workflow to test a C# Extension Methods project, create a NuGet package, and publish it to both GitHub Packages and NuGet.org.
Duration: 30-45 minutes
Instructions
-
Navigate to the
src\dotnet
directory of the repository where you will find theCSharp.ExtensionMethods
project. This folder contains the C# Extension Methods project and theCSharp.ExtensionMethods.Tests
project. -
Note that the
CSharp.ExtensionMethods
project contains the extension methods and theCSharp.ExtensionMethods.Tests
project has a reference toCsharp.ExtensionMethods
project and contains the unit tests for the extension methods. -
Open the integrated terminal in Visual Studio Code, navigate to
CSharp.ExtensionMethods
sub-directory and run the following command to build theCSharp.ExtensionMethods
project:dotnet build
-
Now goto
CSharp.ExtensionMethods.Tests
directory and run the following command to run the unit tests:dotnet test
-
Now we will create a GitHub Actions workflow to build and test the project. Create a new file named
csharp-extension-methods-release-artifacts.yml
in the.github/workflows
directory and add the following content:name: CSharp Extension Methods - Publish GitHub Package on: workflow_dispatch: jobs: build: runs-on: ubuntu-latest steps: - name: Check out repository code uses: actions/checkout@v4 - name: Setup .NET Core uses: actions/setup-dotnet@v1 with: dotnet-version: 8.0.x - name: Build CSharp.ExtensionMethods run: dotnet build --configuration Release working-directory: ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods - name: Build CSharp.ExtensionMethods.Tests run: dotnet build --configuration Release working-directory: ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods.Tests - name: Run Unit Tests run: dotnet test --no-restore --verbosity normal working-directory: ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods.Tests # Create a GitHub Release - name: Create GitHub Release id: create_release uses: actions/create-release@v1 with: tag_name: v1.0.$ release_name: 'Release v1.0.$' body: 'Release of CSharp.ExtensionMethods NuGet package' env: GITHUB_TOKEN: $
-
Commit the changes and push them to the repository.
-
Navigate to the “Actions” tab in your repository to view the workflow runs.
-
Click on the latest workflow run to view the details.
-
Next, we will enhance the workflow to create a NuGet package for the
CSharp.ExtensionMethods
project. Add the following steps to the existing workflow:- name: Create NuGet Package run: dotnet pack --configuration Release working-directory: ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods
-
Commit the changes and push them to the repository.
-
Navigate to the “Actions” tab in your repository to view the workflow runs.
-
Click on the latest workflow run to view the details.
-
Next, we will publish the NuGet package to GitHub Packages. Add the following steps to the existing workflow.
- name: Publish NuGet Package to GitHub Packages run: dotnet nuget push ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods/bin/Release/*.nupkg --source "https://nuget.pkg.github.com/$/index.json" --api-key $ --skip-duplicate
-
Commit the changes and push them to the repository.
-
Navigate to the “Actions” tab in your repository to view the workflow runs.
-
Click on the latest workflow run to view the details.
-
Next, we will publish the NuGet package to NuGet.org. Create a new NuGet API key by following the instructions here. Add the NuGet API key as a secret named
NUGET_API_KEY
in the repository. -
Add the following steps to the existing workflow:
- name: Publish NuGet Package to NuGet.org run: dotnet nuget push ./src/dotnet/CSharp.ExtensionMethods/CSharp.ExtensionMethods/bin/Release/*.nupkg --source "https://api.nuget.org/v3/index.json" --api-key $ --skip-duplicate
-
Commit the changes and push them to the repository.
-
Navigate to the “Actions” tab in your repository to view the workflow runs.
-
Click on the latest workflow run to view the details.
-
Next we will create a GitHub Release for the NuGet package. Add the following steps to the existing workflow:
# Create a GitHub Release - name: Create GitHub Release id: create_release uses: actions/create-release@v1 with: tag_name: v1.0.$ release_name: 'Release v1.0.$' body: 'Release of CSharp.ExtensionMethods NuGet package' env: GITHUB_TOKEN: $
-
Commit the changes and push them to the repository.
-
Navigate to the “Actions” tab in your repository to view the workflow runs.
-
Click on the latest workflow run to view the details.
-
You have successfully created a GitHub Actions workflow to test a C# Extension Methods project, create a NuGet package, and publish it to both GitHub Packages and NuGet.org.
-
Go to the “Packages” tab in your repository to view the published NuGet package.
-
Navigate to the NuGet.org website to view the published NuGet package.
-
You can now use the published NuGet package in your projects.
Summary
In this lab, you created a GitHub Actions workflow to test a C# Extension Methods project, create a NuGet package, and publish it to both GitHub Packages and NuGet.org.