Create your own GitHub Action with .NET
When using GitHub Actions as your CI / CD solution, you might come to the point where your own custom Action would become handy. Here is how to create one with your .NET skills.
When using GitHub Actions as your CI / CD solution, you might come to the point where your own custom Action would become handy. Here is how to create one with your .NET skills.
In this tutorial, we build a simple Hello World Action with a few input and output parameters.
Create the code
As GitHub Actions don't have any form of UI, we will create a .NET Console App for this project.
dotnet new console -n MyGitHubAction
A nice tool that helps you to easily create and document command line parameters for .NET Console Apps is CommandLineParser. Let's install it via NuGet!
dotnet add package CommandLineParser
The command line parameters form the Input values of our GitHub Action. Create a new Options
class and add some parameters like the following ones.
Once the options are set, we can focus on the logic. In this example we just check, if the name options were set. If so, we greet the person, if not we just greet the whole world.
Note the following line, which follows is the syntax to create output variables in GitHubActions.
Console.WriteLine($"::set-output name=date::{DateTime.Now}");
We can now test our code right on our machines.
dotnet run -- -f "Robin-Manuel" -l "Thiel"
If you are satisfied with the results, we can proceed to the next step.
Create a Dockerfile
There are multiple ways to create a GitHub Action. We will use a Docker container to pack our Action. For this, let's create the following Dockerfile.
Let's test the container locally!
docker build -t MyGitHubAction:1.0 .
docker run MyGitHubAction:1.0 -f "Robin-Manuel" -l "Thiel"
GitHub Action Metadata
The last thing we need is a manifest, which describes the Action to other users. For this, we create the action.yaml
file in the root folder. In this file, we describe the action metadata.
Release the GitHub Action
Good job, we are done. To Release the Action, just push everything to a GitHub Repository and create a new Release in there. The UI will guide you through the process of publishing the Action to the Marketplace.
☝️ Advertisement Block: I will buy myself a pizza every time I make enough money with these ads to do so. So please feed a hungry developer and consider disabling your Ad Blocker.