如何在VSTS CI/CD构建邮件及SendGrid部署后邮件中添加SonarQube分析报告?
Hey there! Let's break down your questions one by one and walk through practical, actionable solutions for each scenario:
First, make sure you've installed the official SonarQube extension in your VSTS (now Azure DevOps) organization. Once that's set up, here's how to include the analysis results in your build notifications:
- Run SonarQube analysis in your pipeline: Add the
Prepare Analysis Configuration,Run Code Analysis, andPublish Quality Gate Resulttasks to your build pipeline. This ensures SonarQube generates the analysis data after your build completes. - Extract SonarQube metrics via API: Use a PowerShell or Bash script in your pipeline to call the SonarQube REST API (e.g.,
GET api/measures/component?component=<your-project-key>&metricKeys=bugs,vulnerabilities,code_smells,coverage,duplicated_lines_density). This will fetch key metrics like bug count, vulnerability count, code coverage, etc. - Inject metrics into your VSTS email: Use Azure DevOps' built-in
Send Emailtask, or a custom task, to format the fetched metrics into an HTML table or bullet points. For example, you can pass the metrics as variables to the task and reference them in the email body like this:<h3>SonarQube Analysis Results</h3> <ul> <li>Bugs: $(SonarBugs)</li> <li>Vulnerabilities: $(SonarVulnerabilities)</li> <li>Code Coverage: $(SonarCoverage)</li> </ul> - Include Quality Gate status: Don't forget to add the Quality Gate pass/fail status from the
Publish Quality Gate Resulttask—this is critical for your team to quickly assess if the build meets quality standards.
SendGrid gives you more flexibility for customizing emails, so you can go beyond basic metrics and include richer content:
- Embed key metrics in the email body: Similar to the VSTS approach, use your CI/CD pipeline to fetch SonarQube metrics via API, then format them into clean HTML (tables, cards, etc.) and pass this HTML to SendGrid's API or a SendGrid task in your pipeline. For example, using SendGrid's
send_mailAPI endpoint, you can set thehtmlparameter to your formatted metrics content. - Attach SonarQube reports as files: If you need to share full reports, install the SonarQube PDF Report plugin to generate a PDF of your analysis. In your pipeline, add a step to trigger the PDF generation (via API:
GET api/pdfreport/download?projectKey=<your-project-key>), save the file, then use SendGrid's API to attach it to your email. Just make sure to set theattachmentsparameter in the SendGrid request with the file's content and metadata. - Link to the full SonarQube dashboard: Always include a direct link to your SonarQube project dashboard in the email—this lets your team dive deeper into specific issues if needed.
Directly embedding a live SonarQube dashboard into an email isn't feasible, since most email clients block iframes and dynamic content. Instead, use these workarounds to get the same value:
- Send a static screenshot of the dashboard: Use a headless browser tool like Puppeteer in your pipeline to capture a screenshot of your SonarQube dashboard. Upload the screenshot to a cloud storage service (or use SendGrid's attachment-to-cid feature) and embed it in your email's HTML using an
<img>tag. This gives your team a visual snapshot of the dashboard without needing to click through. - Create a condensed dashboard summary: Pull the most critical widgets from your SonarQube dashboard (e.g., Quality Gate status, top issues, coverage trend) via the API, then recreate this summary as HTML in your email. This keeps the email focused while still providing a high-level overview.
- Use SonarQube webhooks to trigger SendGrid emails: Configure a SonarQube webhook that sends analysis data to your CI/CD pipeline (or a custom endpoint) whenever a new analysis completes. Your pipeline can then use this data to generate a SendGrid email with the latest dashboard metrics and links.
Hope these solutions help you streamline your deployment and quality reporting workflows!
内容的提问来源于stack exchange,提问作者vignesh srinivasan




