2025年12月5日 星期五

[研究]Jenkins + fortify 要用 MSBuild 推薦如何安裝 ?

[研究]Jenkins + fortify 要用 MSBuild 推薦如何安裝 ?

2025-12-05

環境:Visual Studio 2022 + ASP.NET + WebForm + Web Application + C# + SQL Server 2019 + SQL Server Management Studio (SSMS) 20.2

********************************************************************************

1️⃣ MSBuild 的取得方式

方式 適用情境 優缺點
Visual Studio(完整 IDE) 若本地開發或需要 GUI 安裝完整,佔用大,更新麻煩
Visual Studio Build Tools(建議) Jenkins Server、CI/CD 專用 安裝輕量、只包含 MSBuild、必要 SDK,無 GUI
.NET SDK(例如 2022 / 2023) 針對 .NET Core / .NET 5/6/7 專案 適合 SDK 專案,但舊版 WebForm/Framework 專案可能不支援

⚡ 重點:Jenkins + Fortify 針對 WebForms / .NET Framework 專案,建議使用 Visual Studio Build Tools

2️⃣ 安裝 Visual Studio Build Tools

1.下載:

2.選擇工作負載(Workload):

Workload 說明 Fortify/MSBuild 需求
.NET desktop build tools 包含 MSBuild、.NET Framework SDK、C# / VB 編譯器 ✅ 必須
ASP.NET and web development 如果有 WebForms 專案 ✅ 建議選
NuGet package manager 支援 restore ✅ 選
C++ build tools 可選 ❌ Fortify 不需要

3.安裝時注意:

  • 安裝路徑盡量不要有空格,方便 Jenkins 用 %ProgramFiles(x86)% 或短路徑。
  • 選擇「下載缺少的檔案並安裝」避免網路問題。

3️⃣ 環境變數設定

安裝完 Build Tools 之後:

MSBuild 路徑通常位於:

C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe

建議把這個路徑加入 Jenkins 執行環境:

系統環境變數 PATH

或 Jenkins Global Tool Configuration → MSBuild

4️⃣ Jenkins + MSBuild 配置建議

方法 A:Global Tool

  1. Jenkins → Manage Jenkins → Global Tool Configuration
  2. 新增 MSBuild 工具
  3. 指定安裝路徑或自動安裝(自動安裝 VS Build Tools 較少用)
  4. Job 裡選擇這個 MSBuild

方法 B:直接使用完整路徑

"C:\Program Files (x86)\Microsoft Visual Studio\2022\BuildTools\MSBuild\Current\Bin\MSBuild.exe" "%WORKSPACE%\WebApplication1\WebApplication1.sln" /t:Build /p:Configuration=Release

  • 適合 Fortify SCA batch step
  • 不需依賴 Jenkins Tool 安裝器

5️⃣ NuGet restore

Fortify SCA 需要所有依賴可解析,所以建議:

D:\BuildTool\nuget.exe restore "%WORKSPACE%\WebApplication1\WebApplication1.sln"

  • Build Tools 本身不自帶 nuget.exe
  • 可下載 NuGet CLI

6️⃣ Fortify SCA 使用建議

  • Fortify 會呼叫 MSBuild 來收集程式碼
  • MSBuild 必須能完整 build 專案,否則 scan 會失敗
  • 針對 .NET Framework 4.7.2 / WebForms:
    • 安裝 Build Tools + .NET Framework Developer Pack
    • 避免安裝完整 VS IDE

✅ 總結建議

  1. 使用 Visual Studio Build Tools(輕量、官方支援 CI)
  2. 選擇 .NET desktop build tools + ASP.NET and web development workload
  3. 安裝 NuGet CLI
  4. Jenkins Job 直接指定 MSBuild 路徑或使用 Jenkins Global Tool
  5. Fortify 前先 restore NuGet packages
  6. 測試 msbuild /t:build 是否可以在 Jenkins Agent 上完整 build
(完)

相關

沒有留言:

張貼留言