I have been relying on my previous efforts over five years ago that initially set out our C# builds in Jenkins. However, my next topic, code coverage, was new territory for me.
In the process of working through code coverage, I had the opportunity to discover opportunities to clean up the existing build scripts set out in the previous articles so far.
This article will cover the revisions I’ve made. However, I’ll also update the previous articles that will jump immediately to the result.
In CI/CD Building a C# Project in Jenkins steps were added that have better solutions now, such as using MSBuild for building the project, which required adding MSBuild as a tool. Instead, we can use the dotnet tool. This is automatically installed as part of Visual Studio 2022.
So originally in the script, we searched for the solution (.sln file) and then ran MSBuild on it. The dotnet tool automates a lot of this. Now we just need the command:
bat "dotnet build --nologo -c Release --no-restore"
We no longer need MSBuild installed as a tool in Jenkins, as this was the only use.
The dotnet tool also supports operations for nuget. So we can do a nuget restore from dotnet:
bat "dotnet restore --nologo --no-cache"
The dotnet tool supports testing as well, so that command is simplified, too:
bat "dotnet test --nologo -c Release --results-directory TestResults --logger trx --no-restore --no-build"
Unfortunately, it doesn’t support all of them, and one of the commands from nuget we will need later in CI/CD Publishing NuGet Packages, but I will move the tool configuration there.
Finally, while iterating on projects, I encountered an issue with a proliferation of workspaces in the Jenkins workspace folder. Cleaning up the workspace at the end removes code to handle cleaning up the results of previous iterations. It means more work is done to download the GitHub repository each time, but the goal is to have these be extremely small in the first place. If you were building, say, the Unreal Engine GitHub repository, you would avoid the clean workspace option and choose another solution to avoid this cost.