You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

已成功发布.NET项目,求Jenkins构建ASP.NET Core项目配置方案

How to Configure Jenkins for ASP.NET Core Project Build & Publish

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.


This is the modern, official way to build ASP.NET Core projects. Here’s how to set it up in Jenkins:

  1. Make sure .NET Core SDK is installed on your Jenkins server
    Install the same SDK version your project targets (check your project’s .csproj file for <TargetFramework>). Since you’re using VS2017, it’s likely .NET Core 2.x or 3.x.

  2. 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.csproj with 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%\publish here) 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:

  1. 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, replace Professional with that edition name.)

  2. 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:PublishDir to specify where the published files are saved.

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 .csproj has the correct <TargetFramework> and publish settings (VS2017 uses these, so they’re probably right, but double-check if you hit issues).

内容的提问来源于stack exchange,提问作者Sal

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:35:48