The purpose of this document is to provide a step-by-step guide for a developer to setup CI (Continuous Integration) in their .NET projects via workflows from GitHub Actions. This document provides support to both modern .NET (Core and beyond) and .NET Framework too.
The scope of this document covers the following
- Setting up the YAML required to enable document for GitHub’s CI.
- Configuring the repo to enable CI.
- Testing the Workflow.
- A GitHub account with access to your target repository.
- A .NET application repository on GitHub.
- The projects has to be setup with EditorConfig for catching issues related to linting and code analysis.
The yaml file can be created in two ways:
- The
main.yml
can be created using Actions tab in the project repository and clicking on "set up a workflow yourself". This will redirect you to a .github/workflows/main.yml file.
- Create a
.github/workflows/main.yml
file in the project repository directly.
Note: The main.yml
file contains set of instructions that are responsible for building or formatting a repository.
We will need to setup one stage for .NET core projects. The name of this stage should be build
. Set the variable of the dotnet-version
targeting the version of .NET your application is using.
name: CI For DotNet Core
on:
push:
branches: *
pull_request:
branches: *
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Setup .NET
uses: actions/setup-dotnet@v3
with:
dotnet-version: 6.0.x
- name: Restore dependencies
run: dotnet restore
- name: Build
run: dotnet build --no-restore
- name: Test
run: dotnet test --no-build --verbosity normal
We need to setup two stages to setup CI for projects built using .NET Framework.
build
stageformat
stage
name: name: CI
on:
push:
branches:
- '*'
pull_request:
branches:
- '*'
jobs:
build:
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Build
run: cmd /c "C:\Windows\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe" /p:Configuration=Release
format:
runs-on: windows-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Install dotnet-format tool
run: dotnet tool install -g dotnet-format
- name: Format code
run: dotnet format --report msbuild DemoProjectForCI.csproj
Go to the "Actions" tab on the GitHub repository. A new workflow is triggered after a commit is made.
We can check the progress and the logs after running the workflow. We can also create a status or build badge that can be added to the Readme file and can give us an idea of the Status.
Setting up a CI pipeline for your .NET application offers several benefits:
- Improved code quality through automated linting.
- Consistent and reliable builds.
- Early detection of errors and issues.
- Streamlined collaboration through automated testing of merge requests.
Consider enhancing your CI pipeline by adding additional stages such as unit testing, integration testing, and deployment to further improve the quality and reliability of your .NET application.