已成功发布.NET项目,求Jenkins构建ASP.NET Core项目配置方案
Hey there! I get it—you’ve got your classic .NET projects building smoothly with that MSBuild command in Jenkins, but ASP.NET Core projects are a bit different. Let’s fix that so you can automate their build and publish just like your other projects.
First, Understand the Difference
ASP.NET Core uses the dotnet CLI as its primary build tool (though you can use MSBuild too, but dotnet CLI is more straightforward). Your old MSBuild 14.0 command won’t work because it doesn’t support the newer .NET Core project structure. Since Visual Studio 2017 works, your project is already set up correctly—we just need to translate that manual VS process into Jenkins-compatible commands.
Option 1: Use Dotnet CLI (Recommended)
This is the modern, official way to build ASP.NET Core projects. Here’s how to set it up in Jenkins:
Make sure .NET Core SDK is installed on your Jenkins server
Install the same SDK version your project targets (check your project’s.csprojfile for<TargetFramework>). Since you’re using VS2017, it’s likely .NET Core 2.x or 3.x.Add a build step in Jenkins
- Open your Jenkins project, go to the Build section.
- Add an Execute Windows batch command step, then paste these commands (replace
YourAspNetCoreProject.csprojwith your actual project file name):# Restore NuGet dependencies dotnet restore %WORKSPACE%\Source\YourAspNetCoreProject.csproj # Clean and rebuild the project in Release mode dotnet build %WORKSPACE%\Source\YourAspNetCoreProject.csproj --configuration Release --no-restore # Publish the project to a folder in your Jenkins workspace dotnet publish %WORKSPACE%\Source\YourAspNetCoreProject.csproj --configuration Release --output %WORKSPACE%\publish --no-build - Breakdown:
dotnet restore: Pulls all required NuGet packages.dotnet build: Compiles the project in Release mode (skips restore since we already did it to save time).dotnet publish: Packages the app into a deployable folder (%WORKSPACE%\publishhere) and skips rebuilding again.
Option 2: Use MSBuild (If You Prefer)
If you want to stick with MSBuild (since you’re familiar with it), you need to use the MSBuild version from Visual Studio 2017 (v15.0), not the old v14.0. Here’s the command:
Find the correct MSBuild path
For VS2017 Professional, the path is:C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe
(If you have Community or Enterprise, replaceProfessionalwith that edition name.)Add the build command in Jenkins
Paste this into your Execute Windows batch command step (adjust paths and project name as needed):"C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\MSBuild\15.0\Bin\MSBuild.exe" %WORKSPACE%\Source\YourAspNetCoreProject.sln /T:Clean /T:Rebuild /p:Configuration=Release /p:PublishDir=%WORKSPACE%\publish- Key changes:
- Uses VS2017’s MSBuild v15.0 instead of the outdated v14.0.
- Adds
/p:PublishDirto specify where the published files are saved.
- Key changes:
Quick Troubleshooting Tips
- Permissions: Make sure the user running Jenkins has read access to your source code and write access to the publish directory.
- Test Manually: Run the commands directly on the Jenkins server first—if they work there, they’ll work in Jenkins.
- Check Project Config: Verify your
.csprojhas the correct<TargetFramework>and publish settings (VS2017 uses these, so they’re probably right, but double-check if you hit issues).
内容的提问来源于stack exchange,提问作者Sal

