Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowThis browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
In this quickstart, you will learn how to create a GitHub workflow to test your .NET source code. Automatically testing your .NET code within GitHub is referred to as continuous integration (CI), where pull requests or changes to the source trigger workflows to exercise. Along with building the source code, testing ensures that the compiled source code functions as the author intended. More often than not, unit tests serve as immediate feedback-loop to help ensure the validity of changes to source code.
In the GitHub repository, add a new YAML file to the .github/workflows directory. Choose a meaningful file name, something that will clearly indicate what the workflow is intended to do. For more information, see Workflow file.
Important
GitHub requires that workflow composition files to be placed within the .github/workflows directory.
Workflow files typically define a composition of one or more GitHub Action via the jobs.<job_id>/steps[*]
. For more information, see, Workflow syntax for GitHub Actions.
Create a new file named build-and-test.yml, copy and paste the following YML contents into it:
name: build and test
on:
push:
pull_request:
branches: [ main ]
paths:
- '**.cs'
- '**.csproj'
env:
DOTNET_VERSION: '6.0.401' # The .NET SDK version to use
jobs:
build-and-test:
name: build-and-test-${{matrix.os}}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
In the preceding workflow composition:
The name: build and test
defines the name, "build and test" will appear in workflow status badges.
name: build and test
The on
node signifies the events that trigger the workflow:
on:
push:
pull_request:
branches: [ main ]
paths:
- '**.cs'
- '**.csproj'
push
or pull_request
occurs on the main
branch where any files changed ending with the .cs or .csproj file extensions.The env
node defines named environment variables (env var).
env:
DOTNET_VERSION: '6.0.401' # The .NET SDK version to use
DOTNET_VERSION
is assigned the value '6.0.401'
. The environment variable is later referenced to specify the dotnet-version
of the actions/setup-dotnet@v3
GitHub Action.The jobs
node builds out the steps for the workflow to take.
jobs:
build-and-test:
name: build-and-test-${{matrix.os}}
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest, windows-latest, macOS-latest]
steps:
- uses: actions/checkout@v3
- name: Setup .NET Core
uses: actions/setup-dotnet@v3
with:
dotnet-version: ${{ env.DOTNET_VERSION }}
- name: Install dependencies
run: dotnet restore
- name: Build
run: dotnet build --configuration Release --no-restore
- name: Test
run: dotnet test --no-restore --verbosity normal
build-<os>
where the <os>
is the operating system name from the strategy/matrix
. The name
and runs-on
elements are dynamic for each value in the matrix/os
. This will run on the latest versions of Ubuntu, Windows, and macOS.actions/setup-dotnet@v3
GitHub Action is used to setup the .NET SDK with the specified version from the DOTNET_VERSION
environment variable.dotnet restore
command is called.dotnet build
command is called.dotnet test
command is called.It's common nomenclature for GitHub repositories to have a README.md file at the root of the repository directory. Likewise, it's nice to report the latest status for various workflows. All workflows can generate a status badge, which are visually appealing within the README.md file. To add the workflow status badge:
From the GitHub repository select the Actions navigation option.
All repository workflows are displayed on the left-side, select the desired workflow and the ellipsis (...) button.
Select the Create status badge menu option.
Select the Copy status badge Markdown button.
Paste the Markdown into the README.md file, save the file, commit and push the changes.
For more, see Adding a workflow status badge.
Passing | Failing | No status |
---|---|---|
|
|
|
.NET feedback
.NET is an open source project. Select a link to provide feedback:
Events
Mar 17, 9 PM - Mar 21, 10 AM
Join the meetup series to build scalable AI solutions based on real-world use cases with fellow developers and experts.
Register nowTraining
Module
Build continuous integration (CI) workflows by using GitHub Actions - Training
Learn how to create workflows that enable you to use Continuous Integration (CI) for your projects.
Certification
Microsoft Certified: Power Automate RPA Developer Associate - Certifications
Demonstrate how to improve and automate workflows with Microsoft Power Automate RPA developer.
Documentation
GitHub Actions and .NET - .NET
Learn what role GitHub Actions play in .NET application development.
Create a build validation GitHub workflow - .NET
In this quickstart, you will learn how to create a GitHub workflow to validate .NET app compilation.
Tutorial: Create a GitHub Action with .NET - .NET
Learn how to create a GitHub Action with a containerized .NET app.