Git & GitHub Interview Question #91

How would you build and test a .NET project with GitHub Actions?

GitHub Collaboration, Repository Rules, Issues, Releases & Actions CI/CD Mid-Level Intermediate

Quick Interview Answer

A typical .NET CI workflow checks out the repository, installs the required .NET SDK, restores dependencies, builds the solution, and runs tests. Production workflows may also cache dependencies, upload test results, or package artifacts.

Detailed Explanation

A typical .NET CI workflow checks out the repository, installs the required .NET SDK, restores dependencies, builds the solution, and runs tests. Production workflows may also cache dependencies, upload test results, or package artifacts.

The SDK version should match the project requirements, and the workflow should fail when the build or tests fail so pull-request checks remain meaningful.

Code Example

name: .NET CI

on:
  push:
  pull_request:

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-dotnet@v4
        with:
          dotnet-version: "8.0.x"
      - run: dotnet restore
      - run: dotnet build --no-restore
      - run: dotnet test --no-build