[.NET] แก้ปัญหา Build แล้ว Error The build is configured to run SonarQube analysis

หลังจากปรับ Framework ของบริษัทให้เป็น .NET Standard เรียบร้อยแล้ว เมื่อทดสอบ Build ผ่านตัว Jenkins เกิด Error ดังนี้

The build is configured to run SonarQube analysis but the SonarQube analysis targets could not be located

ภาพรวมของ SonarQube Analysis นะครับ

ต้องเข้าใจก่อนนะครับ ว่าการวิเคราะห์ Code ของ SonarQube จริงๆ แล้ว มันแอบไปใช้งานตัว MSBuild.exe ครับ โดยมี 3 ขั้นตอนลำดับ ดังนี้

  • SonarScanner.MSBuild.exe begin
    • เปิดการเชื่อมต่อกับ SonarQube Server ครับ
    • และแอบไปเพิ่ม Config ให้กับ MSBuild.exe โดยจะไปเพิ่มไฟล์ SonarQube.Integration.ImportBefore.targets เพื่อใช้สำหรับขั้นตอนถัดไป (SonarScanner.MSBuild.exe analysis) ครับ ที่ Path %USERPROFILE%\AppData\Local\Microsoft\MSBuild\Current\Microsoft.Common.targets
    • ไฟล์ SonarQube.Integration.ImportBefore.targets ตัวเป็นๆครับ
    • แอบมาส่องไฟล์ SonarQube.Integration.ImportBefore.targets กันครับ
  • MSBuild.exe analysis
    • Build Project หรือ Solution โดยใช้ Config ที่ได้แอบไปแก้ไขใน MSBuild.exe ตั้งแต่ช่วง SonarScanner.MSBuild.exe begin
  • SonarScanner.MSBuild.exe end
    • เอาผลการวิเคราะห์ขั้นต้นที่ได้จาก MSBuild.exe ส่งไปยัง SonarQube Server ครับ
    • และปิดการเชื่อมต่อกับ SonarQube Server
    • รวมถึงลบ Config SonarQube.Integration.ImportBefore.targets ออกไปด้วย เพื่อไม่ให้ Build เพื่อจุดประสงค์อื่นๆเกิดปัญหาครับ

อะไรที่ทำให้เกิดปัญหานี้หละ

  • ตอนทำ Jenkins Step ลืมใส่ SonarScanner.MSBuild.exe end โดยแต่ละ Step ของ Sonar มันมีกระบวนการบางอย่างแอบไปเพิ่มใน MSBuild ครับ
  • เกิด Error ระหว่างที่่ทำ Sonar กำลัง Analysis ทำให้กระบวนการทำงานของ SonarQube ไม่สมบูรณ์ครับ

การแก้ไข

  • ลบไฟล์ SonarQube.Integration.ImportBefore.targets ออกจาก Path %USERPROFILE%\AppData\Local\Microsoft\MSBuild\Current\Microsoft.Common.targets

ในที่สุดสามารถ Build ได้ครับ สบายใจแล้ว Step ถัดไป มาไล่ Revise Flow ของ Automate Test ครับ

Reference