Setting Up a CI Pipeline with Github Actions in C# [Tutorial]
Having code in source control is important. It allows you to collaborate with others and have a centralized location for the code to live. However, what if you want to know if the code builds, AKA, compiles as expected? Source control alone won’t tell you that and the last thing you want is to manually build the code every time a new commit gets pushed. Not only is it cumbersome, but it’s simply not scalable. What’s the answer?
In this blog post, you’ll learn how to build C# code right from GitHub using GitHub Actions. GitHub Actions is a free CICD system that allows you to build and deploy code right from a GitHub repository.
Prerequisites for GitHub Actions
One of the many great things about GitHub Actions is it’s completely built into GitHub, so there aren’t any extra steps you need to take to install or configure it.
To follow along with this blog post, you should have the following:
An understanding of Continuous Integration (CI).
A beginner level knowledge of GitHub Actions
A beginner level knowledge of C#, or an understanding of how to read code in another language.
A GitHub account, which you can sign up for free here.
Step 1: Take a Look & Fork the Repository
Before building the code in a CI process, you need to have code to build. The code doesn’t have to be anything specific. It can be a backend application, a frontend application, or even a script. For the purposes of this blog post, you’ll use a C# web API boilerplate, which can be found on GitHub here.
The C# web API boilerplate isn’t customized in any way, so you don’t have to worry about any key customizations that you may need.
Open up a web browser and go to the Blazor API on GitHub.
On the GitHub page, click the Fork button to fork the repository to a GitHub account that you are logged in with.
Once the code is forked to a GitHub account that you are using, you should now see the Blazor repository.
First Look at the Code
Within the Blazor repository, click on the web directory and open the Program.cs C# file to look at the code.
The code will show what the default Program is for building a C# Blazor app including all of the needed namespaces.
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
Step 2: How to Build Workflows in GitHub Actions
Now that you’ve seen the code and have an understanding of the application that is inside of the GitHub repository, it’s time to start building out the workflow in GitHub Actions.
Within the repository, click on the Actions button.
Once you click on the Actions button, you will see a plethora of different workflows. Workflows are the code that you set up that builds the CI or CICD pipeline. All pipelines are configured as code, which allows for reusability and collaboration across teams.
Within GitHub Actions, it will suggest pre-made workflows based on the type of code that’s in the repository. Because GitHub knows that the repository contains C# code, it will suggest a few C# workflows. On the .NET Core workflow, click the Set up this workflow button.
The workflow will automatically get created and presented to you. Let’s break down what each section means.
The name of the workflow will default to .NET Core, but because this is just a metadata name, you can change it to whatever you’d like.
The on trigger will automatically set for any time code gets pushed or a pull request (PR) is created for the master branch.
The job is set up to build, AKA, create a CI process.
The workflow is running in an ubuntu-latest virtual environment, managed and maintained by GitHub.
The steps build out the CI process which include:
Checking out the C# code
Installing all dependencies needed for the code
Restoring the C# code
Building and testing the code
Below you will see a screenshot of an example of a C# templated workflow.
Step 3: Create a C# Workflow from a Template
In the previous section you learned how to created a C# workflow based off of a template. The great thing about templates are, they are created from best practices. In short, you know what you’re getting out of a template is Microsoft best practices for building the code.
Now that the template is created, it’s time to commit it to the GitHub repository. Click the green Start commit button.
To commit the GitHub Actions workflow to the Blazor repository, click the green Commit new file button.
The workflow will now be created and you will be taken to the .github/workflows directory that gets automatically created once you commit a new workflow.
To check on the running workflow, click the Actions button.
You will now see a workflow running for the C# code.
Once the build/CI process is complete, you should see a screen similar to the one below.
Congrats! You have successfully built a CI process for a C# Blazor application.
Congrats, You Set Up a CI Pipeline!
In this blog post, you learned the key way to set up a workflow in GitHub Actions. Starting off, you took a look at some example C# code to get an idea of what can be deployed. Following that, you learned how to build a workflow out of a GitHub Actions C# template, which can be useful if you don’t want to build a workflow from scratch. Finally, you learned how to run the workflow and build the code in a true CI process. Learn more IT skills with CBT Nuggets!
delivered to your inbox.
By submitting this form you agree to receive marketing emails from CBT Nuggets and that you have read, understood and are able to consent to our privacy policy.