Master GitHub Actions with hands-on labs and exercises. Learn how to automate workflows, run tests, deploy applications, and more using GitHub's powerful automation platform. This repository has everything you need to get started with continuous integration and continuous deployment.
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
Navigate to the src\dotnet
directory of the repository where you will find the CSharp.ExtensionMethods
project. This folder contains the C# Extension Methods project and the CSharp.ExtensionMethods.Tests
project.
Note that the CSharp.ExtensionMethods
project contains the extension methods and the CSharp.ExtensionMethods.Tests
project has a reference to Csharp.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 the CSharp.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-build-test-nuget.yml
in the .github/workflows
directory and add the following content:
name: CSharp Extension Methods Build - Test - NuGet
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
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.
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.
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.