What you’ll learn

  • Build and test a .NET console app.
  • Move working local commands into Jenkins.
  • Publish and archive executable output.
  • Distinguish compile failures from test failures.

When you’re finished

  • Produce a passing Jenkins C# pipeline.
  • Download its archived output.
  • Successfully complete the intentional-failure labs.

Repository: jenkins-csharp-demo

Goal:

GitHub → Jenkins → Restore → Build → Test → Publish → Archive .exe

3.1 Create the repository and solution

Create an empty GitHub repository named:

jenkins-csharp-demo

Clone it locally:

git clone "https://github.com/<GITHUB-USER>/jenkins-csharp-demo.git"
cd jenkins-csharp-demo

Create the solution, application, and test project:

dotnet new sln -n JenkinsCSharpDemo --format sln

dotnet new console `
    -n JenkinsHelloWorld `
    -o src/JenkinsHelloWorld

dotnet new xunit `
    -n JenkinsHelloWorld.Tests `
    -o tests/JenkinsHelloWorld.Tests

dotnet sln JenkinsCSharpDemo.sln add `
    src/JenkinsHelloWorld/JenkinsHelloWorld.csproj

dotnet sln JenkinsCSharpDemo.sln add `
    tests/JenkinsHelloWorld.Tests/JenkinsHelloWorld.Tests.csproj

dotnet add `
    tests/JenkinsHelloWorld.Tests/JenkinsHelloWorld.Tests.csproj `
    reference `
    src/JenkinsHelloWorld/JenkinsHelloWorld.csproj

3.1.1 Required values

  • JenkinsCSharpDemo — solution name.
  • JenkinsHelloWorld — application project name.
  • JenkinsHelloWorld.Tests — test project name.

These commands use .NET SDK 10. The explicit --format sln keeps the solution filename consistent with the pipeline listings; .NET 10 otherwise creates a .slnx file. See Microsoft’s solution-format guidance. Use the same SDK major version locally and in each pipeline.

The backtick (`) is PowerShell’s line-continuation character. You can place each command on one line instead.

3.2 Add unit tests

Use a tiny class so the message can be tested independently of console output.

src/JenkinsHelloWorld/Greeting.cs:

namespace JenkinsHelloWorld;

public static class Greeting
{
    // Keep the message in one place so the unit test can verify it.
    public static string GetMessage()
    {
        return "Hello, world!";
    }
}

src/JenkinsHelloWorld/Program.cs:

using JenkinsHelloWorld;

// The executable only needs to print the message.
Console.WriteLine(Greeting.GetMessage());

tests/JenkinsHelloWorld.Tests/GreetingTests.cs:

using JenkinsHelloWorld;

namespace JenkinsHelloWorld.Tests;

public class GreetingTests
{
    [Fact]
    public void GetMessage_ReturnsExpectedGreeting()
    {
        // Arrange
        const string expected = "Hello, world!";

        // Act
        string actual = Greeting.GetMessage();

        // Assert
        Assert.Equal(expected, actual);
    }
}

3.3 Build locally

3.3.1 Why we run everything manually first

Jenkins is not a special C# compiler. It will eventually automate the same commands you can run yourself:

Without Jenkins:
You → dotnet restore → dotnet build → dotnet test → dotnet publish

With Jenkins:
Jenkins → dotnet restore → dotnet build → dotnet test → dotnet publish

The value Jenkins adds is repeatability, orchestration, triggering, logging, artifact retention, and gates.

Always prove the commands work outside Jenkins first.

Restore dependencies:

dotnet restore JenkinsCSharpDemo.sln

Build:

dotnet build JenkinsCSharpDemo.sln `
    --configuration Release `
    --no-restore

Run tests:

dotnet test JenkinsCSharpDemo.sln `
    --configuration Release `
    --no-build

Expected result: the test run completes successfully with 0 failed tests. Exact console wording and timing can vary by .NET SDK/test-runner version.

Publish a Windows executable:

dotnet publish src/JenkinsHelloWorld/JenkinsHelloWorld.csproj `
    --configuration Release `
    --runtime win-x64 `
    --self-contained false `
    --output artifacts/publish

3.3.2 Important dotnet publish options

Option Required? Meaning
project path Yes Project to publish
--configuration Release Recommended Produces Release output
--runtime win-x64 Required for this example Targets 64-bit Windows
--self-contained false Chosen for this lab Requires compatible .NET runtime on destination
--output artifacts/publish Recommended Predictable artifact directory

3.3.3 Checkpoint

Before moving on, confirm:

  • restore succeeds;
  • Release build succeeds;
  • unit test passes;
  • publish output appears under artifacts/publish;
  • the published application contains the expected executable/output files.

If any item fails, stop here and use Appendix G — Troubleshooting.

3.4 Add the Jenkinsfile

Create a file named exactly Jenkinsfile in the repository root.

The full version is in Appendix C.

The important flow is:

stages {
    stage('Restore') { ... }
    stage('Build')   { ... }
    stage('Test')    { ... }
    stage('Publish') { ... }
}

On Windows, Jenkins should use bat or PowerShell-compatible steps rather than Unix sh.

3.4.1 Manual → automated progression

Use this sequence deliberately:

  1. Manual commands: run the .NET commands in PowerShell yourself.
  2. Manual Jenkins execution: use Build Now.
  3. Pipeline from source control: Jenkins reads Jenkinsfile from GitHub.
  4. Automatic trigger: configure source-control-triggered execution after the basic pipeline is proven.

Do not skip directly to automation when debugging the first build.

3.4.2 Create the Jenkins Pipeline job

  1. Commit and push the repository.
  2. In Jenkins, select New Item.
  3. Enter jenkins-csharp-demo.
  4. Select Pipeline.
  5. Under Pipeline, choose Pipeline script from SCM.
  6. Select Git.
  7. Enter the GitHub repository URL.
  8. Set the script path to Jenkinsfile.
  9. Save.
  10. Select Build Now.
  11. Watch each stage.
  12. Open Console Output for the detailed command output.

3.4.3 First Git commit

git add .
git status
git commit -m "Add initial C# Jenkins demo"
git push

3.5 Archive the executable

Jenkins can preserve build output using archiveArtifacts.

Example:

post {
    success {
        archiveArtifacts artifacts: 'artifacts/publish/**/*',
                         fingerprint: true
    }
}

After a successful run:

  1. Open the Jenkins build.
  2. Locate the archived artifacts.
  3. Open the artifacts/publish tree.
  4. Download the executable and supporting files.
  5. Run the executable from a machine with the required .NET runtime.

3.6 Failure labs

3.6.1 Lab A — Compile failure

Introduce invalid C# syntax, for example:

return "Hello, world!"

The missing semicolon should cause the Build stage to fail.

Expected behavior:

Restore: PASS
Build:   FAIL
Test:    NOT RUN
Publish: NOT RUN

Fix the syntax and push again.

3.6.2 Lab B — Test failure without compile failure

Change the application message:

return "Hello from Jenkins!";

but leave the unit-test expectation as:

const string expected = "Hello, world!";

Expected behavior:

Restore: PASS
Build:   PASS
Test:    FAIL
Publish: NOT RUN

This proves that compiles successfully is not the same as behaves correctly.

3.6.3 Lab C — Successful behavior change

Update both the application and the test to:

Hello from Jenkins!

Commit and push.

Expected behavior:

Restore: PASS
Build:   PASS
Test:    PASS
Publish: PASS
Archive: PASS

3.6.4 Jenkins pipeline checkpoint

After the first successful Jenkins run, confirm:

  • Jenkins retrieved the intended Git commit;
  • Restore passed;
  • Build passed;
  • Test passed;
  • Publish passed;
  • archived output is downloadable;
  • console output clearly shows each command.

3.7 Automatic triggering

A GitHub webhook is the normal end-state for prompt push-triggered builds, but a Jenkins instance running only on localhost is not directly reachable by GitHub.

For the first lab, use one of these:

  • Manual: select Build Now.
  • Polling: configure Jenkins SCM polling for learning purposes.
  • Later: expose Jenkins safely or move it to a reachable server and use a webhook.

Do not expose a local Jenkins controller directly to the public Internet without understanding authentication, TLS, firewalling, patching, and reverse-proxy/security requirements.