todos os artigos
/// Coding & DevOps & Testing ///

Using GitHub Actions to create a .NET Framework pipeline: Build, test, and deploy

A step-by-step guide to setting up a CI/CD pipeline for a .NET Framework project using GitHub Actions — build, test with VSTest, restore NuGet packages, and run the application automatically.

12 de August de 2023 · ~6 min de leitura · #dotnet #dotnet-framework #github-actions ·

In this article we cover the CI/CD process of a .NET Framework project using GitHub Actions to build, test, and run — from the first workflow file to a complete automated pipeline.

· · ·
01

What are GitHub Actions?

GitHub Actions provides a flexible way to automate various tasks directly from your GitHub repositories — building, testing, and deploying applications. These actions are defined in YAML files as workflows and can be customized to suit your project’s specific requirements.

For reference, I set up a repository on GitHub with a PoC (Proof of Concept) based on this tutorial. That repository has many more features than what I cover here:

POC-GHActions-CI-NetFramework
A working proof of concept on GitHub showing GitHub Actions CI for a .NET Framework solution — with build, test, and coverage steps already configured.
· · ·
02

Creating the pipeline — step by step

Step 1 — Open the Actions tab

Open your repository on GitHub and click on the Actions tab.

Step 2 — Set up a workflow yourself

GitHub provides numerous workflow templates for different languages and frameworks. To write your own from scratch, click Set up a workflow yourself above the Choose a workflow section.

Step 3 — Name the workflow and define triggers

In the GitHub Actions editor, name the workflow and define the events that will fire it:

name: Build .NET Framework

on:
  push:
  workflow_dispatch:
  • push fires the workflow on every push to the repository.
  • workflow_dispatch allows running the workflow manually from the Actions page.

A complete reference of all trigger events is in the official GitHub Actions documentation.

Step 4 — Define the build job on a Windows runner

.NET Framework requires a Windows environment. Create a job and target windows-latest:

jobs:
  build:
    name: Build
    runs-on: windows-latest

The runs-on value is job-scoped — different jobs in the same workflow can target different operating systems.

Step 5 — Configure the job steps

The job needs the following steps in order:

  1. Checkout the source code
  2. Set up MSBuild
  3. Set up VSTest
  4. Set up NuGet
  5. Restore NuGet packages
  6. Build the solution in Release mode
  7. Run the tests
  8. (Optional) Run the compiled executable
steps:
  - name: Checkout Code
    uses: actions/checkout@v3
    with:
      fetch-depth: 0

  - name: Setup MSBuild Path
    uses: microsoft/setup-msbuild@v1.3
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true

  - name: Setup VSTest
    uses: darenm/Setup-VSTest@v1.2

  - name: Setup NuGet
    uses: NuGet/setup-nuget@v1.2
    env:
      ACTIONS_ALLOW_UNSECURE_COMMANDS: true

  - name: Restore NuGet Packages
    run: nuget restore your-solution-name.sln

  - name: Build Release
    run: msbuild your-solution-name.sln /p:Configuration=Release

  - name: Test
    run: vstest.console.exe path-to-test-binary.dll

  - name: Run
    run: path-to-application-binary.exe
Placeholders to replace
Replace your-solution-name.sln with the actual path to your solution file in both the Restore and Build steps. Replace path-to-test-binary.dll with the DLL path of the test project after a Release build — usually inside bin/Release/. Replace path-to-application-binary.exe with the path to the compiled executable of your main project.

Steps 6 and 7 — Save, trigger, and monitor

Save the workflow file — it lives at .github/workflows/build.yml in your repository. GitHub Actions triggers it automatically on the next push, or you can run it manually from the Actions tab. Each run shows a full view of progress, logs, and error details.

· · ·
03

Complete workflow file

Here is the full workflow to build, test, and run a .NET Framework solution:

name: Build .NET Framework

on:
  push:
  workflow_dispatch:

jobs:
  build:
    name: Build
    runs-on: windows-latest

    steps:
      - name: Checkout Code
        uses: actions/checkout@v3
        with:
          fetch-depth: 0

      - name: Setup MSBuild Path
        uses: microsoft/setup-msbuild@v1.3
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Setup VSTest
        uses: darenm/Setup-VSTest@v1.2

      - name: Setup NuGet
        uses: NuGet/setup-nuget@v1.2
        env:
          ACTIONS_ALLOW_UNSECURE_COMMANDS: true

      - name: Restore NuGet Packages
        run: nuget restore your-solution-name.sln

      - name: Build Release
        run: msbuild your-solution-name.sln /p:Configuration=Release

      - name: Test
        run: vstest.console.exe path-to-test-binary.dll

      - name: Run
        run: path-to-application-binary.exe
· · ·
04

Benefits of GitHub Actions for your projects

Automated builds
Builds fire automatically whenever changes are pushed, ensuring consistent and up-to-date compiled outputs without manual intervention.
Testing automation
Integrate xUnit, NUnit or MSTest via VSTest. Code changes are tested before they can be merged or deployed, reducing the risk of shipping bugs to production.
Coverage reporting
Submit coverage reports to SonarCloud, Code Climate, Codefactor, Codecov and others — directly from the pipeline, no manual uploads needed.
Deployment flexibility
Deploy to Azure, AWS, on-premises or any target using community actions. The same workflow that builds and tests can also ship to production.
Collaboration and transparency
Workflow files live alongside the code. Team members can review, comment on, and contribute to the pipeline configuration the same way they do with code.
· · ·

References

  1. GitHub. Official GitHub Actions documentation. docs.github.com/en/actions
  2. actions/checkout. GH Action: actions/checkout. github.com/actions/checkout
  3. Microsoft. GH Action: microsoft/setup-msbuild. github.com/microsoft/setup-msbuild
  4. darenm. GH Action: darenm/Setup-VSTest. github.com/darenm/Setup-VSTest
  5. NuGet. GH Action: NuGet/setup-nuget. github.com/NuGet/setup-nuget

 Categorias

 Tópicos deste artigo

URL copiada!