Publish Blazor Component to Nuget with Github Actions
This guide will show you how to create a GitHub Action which triggers on a Git Tag and publishes the Blazor Component to Nuget.
Please note, the process is the same for any .Net Core project type.
Steps:
- GitHub Repository
- Blazor Component Project
- Nuget.org Account and API Key
- Github Secret
- Github Action
- Create a Tag and Trigger a Release
GitHub Repository
In this example, we will use the following repository, https://github.com/IvanJosipovic/Demo01
Clone the repo you created and remember the location, in our case its "C:\Repos\IvanJosipovic\Demo01"
Blazor Component Project
In Visual Studio, lets create a "Razor Class Library" Project.
Enter a Project Name, set the location of your cloned repository, then click Create.
Click Create.
Note: "Support pages and views" is only needed if the project will use legacy .cshtml views.
Edit the RazorClassLibrary.csproj and make sure it contains the following:
Push the code to your GitHub repository.
Nuget.org Account and API Key
Go to https://www.nuget.org and create an account.
Once you're logged in, click on your username in the top right, then click on "API Keys".
Click Copy and save the key to safe place, well need it in the next step.
GitHub Secret
In your GitHub repository, click on Settings, then click on Secrets.
Create a Secret named "NUGET_API_KEY" with your key.
GitHub Action
In your repository, click on "Actions", then click on "Set up a workflow yourself"
Enter the following yaml (Source)
Note: In your example, you will need to tweak a number of the parameters.
Create a Tag and Trigger a Release
In Visual Studio, perform a Git Fetch as we have pushed changes to our master branch.
In Visual Studio Team Explorer, click on Tags.
Click on New Tag then enter "v0.0.2-alpha", then click Create Tag.
Note: Tags names must be in "v*" format to Trigger the release
Note: The Nuget Version will be the Tag name without the "v", eg. 0.0.2-alpha
Select Tag v0.0.2-alpha, then right click on it and select Push.
A GitHub Action called "Create Release" should be triggered.
If we go back to Nuget.org, we should see our new package Published.
Enjoy!