Search code examples
.netjenkinsmsbuildjenkins-pipeline

Jenkins doesn't recognize msbuild.exe


I just started working with Jenkins and following this tutorial: https://medium.com/southworks/creating-a-jenkins-pipeline-for-a-net-core-application-937a2165b073

I've got to the point of cleaning the solution but I get the error: 'msbuild.exe' is not recognized as an internal or external command, operable program or batch file.

This is my Jenkinsfile until now:

pipeline {
    agent any
    stages {
        stage ('Clean workspace') {
            steps {
                cleanWs()
            }
        }
        stage ('Checkout git') {
            steps {
                git credentialsId: 'jenkins_id', url: 'https://github.com/org/project.git', branch: 'feature-branch'
            }
        }
        stage('NuGet restore') {
            steps {
                bat "dotnet restore ${workspace}\\solution.sln"
            }
        }
        stage('Clean solution') {
            steps {
                bat "msbuild.exe ${workspace}\\solution.sln -nologo -nr:false -p:platform=\"x64\" -p:configuration=\"release\" -t:clean"
            }
        }
    }
}

This is how I configured the MSBuild in Jenkins: enter image description here

C:\Program Files (x86)\Microsoft Visual Studio\2019\BuildTools\MSBuild\Current\Bin is also added to my Path variable and I have an msbuild system variable with the same value.

I don't know if it matters but I'm using Rider for IDE.

Does someone know why Jenkins can't find msbuild?

Thanks


Solution

  • I assume you have Visual Studio 2019 installed. I suspect that the path is not correct. Use the vswhere command to find MSBuild. You can use the following command:

    "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere" -latest -requires Microsoft.Component.MSBuild -find MSBuild\**\Bin\MSBuild.exe
    

    On a machine of mine with Visual Studio 2022 Community installed, the result is:

    C:\Program Files\Microsoft Visual Studio\2022\Community\MSBuild\Current\Bin\MSBuild.exe
    

    On Windows you can depend on vswhere being available at "%ProgramFiles(x86)%\Microsoft Visual Studio\Installer\vswhere", if Visual Studio 2017 or later is installed.

    (vswhere is Windows only, is part of the Visual Studio Installer for Windows, and is not part of the .NET SDK.)