GitHub Actions Build & Test Workflow
A GitHub Actions workflow that restores, builds, and tests a .NET solution on every push and pull request to main.
Prerequisites
- A .NET solution (
.sln) with at least one test project, hosted in a GitHub repository. - Push access to the repository so you can add workflow files and trigger runs.
Steps
- In the repository root, create a
.github/workflowsfolder if it doesn't already exist. - Add a
build.ymlfile to that folder with the workflow below. Replaceyour-project.slnwith your own solution file name, and setdotnet-versionto the SDK version your project targets. - Commit and push the file (or open a pull request) against
main. - Verify the run: open the repository's Actions tab, confirm the Build & Test workflow succeeds, and download the
test-resultsartifact from the run summary to inspect the.trxoutput.
Step Overview
| Step | What it does |
|---|---|
| Checkout | Clones the repository into the runner so subsequent steps have access to the source code. |
| Setup .NET | Installs the specified .NET SDK version on the runner. |
| Restore dependencies | Runs dotnet restore to download all NuGet packages defined in the solution. |
| Build | Compiles the solution in Release configuration, skipping restore since it already ran. |
| Test | Runs all test projects in Release mode without rebuilding, and writes results to a .trx file. |
| Upload test results | Uploads the .trx file as a workflow artifact so results are accessible in the GitHub Actions UI. Runs even if tests fail (if: always()). |
name: Build & Test
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
build:
name: Build, Test & Restore
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Setup .NET
uses: actions/setup-dotnet@v4
with:
dotnet-version: "10.x"
- name: Restore dependencies
run: dotnet restore your-project.sln
- name: Build
run: dotnet build your-project.sln --no-restore --configuration Release
- name: Test
run: dotnet test your-project.sln --no-build --configuration Release --logger "trx;LogFileName=test-results.trx"
- name: Upload test results
uses: actions/upload-artifact@v4
if: always()
with:
name: test-results
path: "**/*.trx"
See Also
- .NET 10 Core API with PostgreSQL Template — ships with a GitHub Actions CI pipeline out of the box.