For the complete documentation index, see llms.txt. This page is also available as Markdown.

C# (.NET Core)

Use a CI pipeline to build and test a C# (.NET Core) application.

You can build and test a C# and .NET Core application using a Linux or Windows platform on Harness Cloud or a self-managed Kubernetes cluster build infrastructure.

This guide assumes you've created a Harness CI pipeline.

Specify architecture

You can use a Linux or Windows platform to build and test C# (.NET Core) apps. These examples use Linux build infrastructure.

stages:
  - stage:
      name: build
      identifier: build
      type: CI
      spec:
        cloneCodebase: true
        platform:
          os: Linux
          arch: Amd64 ## Can be Amd64 or Arm64
        runtime:
          type: Cloud
          spec: {}

There are several self-managed build infrastructure options. This example uses a Kubernetes cluster build infrastructure.

stages:
  - stage:
      name: build
      identifier: build
      description: ""
      type: CI
      spec:
        cloneCodebase: true
        infrastructure:
          type: KubernetesDirect
          spec:
            connectorRef: YOUR_KUBERNETES_CLUSTER_CONNECTOR_ID
            namespace: YOUR_NAMESPACE
            automountServiceAccountToken: true
            nodeSelector: {}
            os: Linux

Install dependencies

The .NET Core SDK and other .NET libraries are pre-installed on Harness Cloud runners. For details about all available tools and versions, go to Platforms and image specifications. You can use Run steps to install additional dependencies or run dotnet restore.

You can use Run steps to install dependencies or run commands such as dotnet restore.

Cache dependencies

Add caching to your Build (CI) stage.

Cache your .NET dependencies with Cache Intelligence.

Add caching to your stage.spec:

You can use built-in steps to:

YAML example: Save and restore cache steps

Here's an example of a pipeline with Save Cache to S3 and Restore Cache from S3 steps.

Build and run tests

Add Run steps to build and run your tests.

Visualize test results

You can view test results on the Tests tab of your pipeline executions. Harness supports JUnit XML and TRX test report formats.

For .NET projects, you can generate TRX reports natively using dotnet test -l:trx without needing a converter. Alternatively, you can use a converter to output JUnit XML reports, such as NUnit to JUnit or .NET trx2JUnit.

For your pipeline to produce test reports, you need to modify the Run step that runs your tests. Make sure the command generates test reports and add the reports specification.

Run tests with Test Intelligence

Test Intelligence is available for C# (.NET Core); however, it is behind the feature flag TI_DOTNET. Contact Harness Support to enable the feature.

With this feature flag enabled, you can use Run Tests steps to run unit tests with Test Intelligence on C# codebases.

Test splitting

Harness CI supports test splitting (parallelism) for both Run and Run Tests steps.

Specify version

The .NET SDK is pre-installed on Hosted Cloud runners. For details about all available tools and versions, go to Platforms and image specifications.

If you need a specific .NET Core SDK version that isn't already installed, you can use a Run step to install it.

Install one .NET SDK version
Install multiple .NET SDK versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in your steps.

Specify the desired .NET SDK image tag in your steps. There is no need for a separate install step when using Docker.

Use one .NET SDK version

On Windows platforms, you might also need to install Microsoft Build Tools into the container.

Use multiple .NET SDK versions
  1. Add the matrix looping strategy configuration to your stage.

  1. Reference the matrix variable in the image field of your steps.

On Windows platforms, you might also need to install Microsoft Build Tools into the container.

Full pipeline examples

The following full pipeline examples are based on the partial examples above.

If you copy this example, replace the placeholder values with appropriate values for your code repo connector and repository name. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.

YAML example

If you copy this example, replace the placeholder values with appropriate values for your code repo connector, Kubernetes cluster connector, Kubernetes namespace, and repository name. Depending on your project and organization, you may also need to replace projectIdentifier and orgIdentifier.

YAML example

Next steps

Now that you have created a pipeline that builds and tests a C# (.NET Core) app, you could:

Last updated

Was this helpful?