C family project

Using YAML or the Azure Classic interface to create the Azure build pipeline for C family projects.

Before you begin, read Azure Pipelines integration overview.

Once you have created your project in SonarQube Server and set up feature integration for your project, you can add the SonarQube Server analysis to your Azure build pipeline.

In your build pipeline, insert the following steps in the order they appear here. These steps can be interweaved with other steps of your build as long as the following order is followed. All steps have to be executed on the same agent.

To create your Azure build pipeline, you can use either YAML or the Azure Classic interface.

  • The use of the Classic interface is not always possible (e.g. if your code is stored on GitHub).

  • If you use YAML, SonarSource can provide you with YAML templates or code examples.

Step 1: Make the Build Wrapper available on the build agent.

Download and unzip the Build Wrapper on the build agent, as explained below, according to your build agent type. See also the CFamily Prerequisites page. The archive to download and unzip depends on the host’s platform.

Microsoft-hosted build agent

You will need to make the Build Wrapper available on the build agent every time (as part of the build pipeline). To accomplish this, you can add a PowerShell script task (on Windows) or a Bash task (on Linux and macOS) by inserting a Command Line task. See the examples below (substitute the URL of your specific SonarQube instance into the download path below).

PowerShell commands on a Windows host Invoke-WebRequest -Uri '<YourSonarQubeURL>/static/cpp/build-wrapper-win-x86.zip' -OutFile 'build-wrapper.zip' Expand-Archive -Path 'build-wrapper.zip' -DestinationPath '.'

Bash commands on a Linux host curl '<yourSonarQubeURL>/static/cpp/build-wrapper-linux-x86.zip' --output build-wrapper.zip unzip build-wrapper.zip

Bash commands on a Linux ARM64 host curl '<yourSonarQubeURL>/static/cpp/build-wrapper-linux-aarch64.zip' --output build-wrapper.zip unzip build-wrapper.zip

Bash commands on a macos host curl '<yourSonarQubeURL>/static/cpp/build-wrapper-macosx-x86.zip' --output build-wrapper.zip unzip build-wrapper.zip

Self-hosted build agent

You can either download it every time (using the same scripts) or only once (as part of the manual setup of your build agent).

Step 2: Add a Prepare Analysis Configuration task

If you want to use a specific scanner version, see Using various features.

Using YAML

Add a SonarQube’s Prepare Analysis Configuration task before your build task. See the YAML file example below.

Using the Classic interface

In the procedure below, the manual configuration mode is used to define analysis parameters at the pipeline level. You may use the sonar-project.properties file instead or another specified configuration file. For more information, see Using various features.

Add a SonarQube’s Prepare analysis configuration task and configure it as follows:

  1. In SonarQube Server Service Endpoint, select the SonarQube Server service connection you created in Adding the SonarQube service connection to your AzDO project in Setting up project integration.

  2. In Choose the way to run the analysis, select Use SonarScanner CLI (even if you build with Visual Studio/MSBuild).

  3. Select the Manually provide configuration mode.

  4. In the Project Key field, enter your project key.

  5. In Advanced section > Additional properties, add the following property:

    • key: sonar.cfamily.compile-commands

    • Value: the path to the compile_commands.json file inside the Build Wrapper output directory: sonar.cfamily.compile-commands=<output directory>/compile_commands.json.

Step 3: Add a Command Line task to run your build

For the analysis to happen, your build has to be run through a command line so that it can be wrapped-up by the build-wrapper.

To do so, run Build wrapper executable and pass in as the arguments:

  1. The output directory configured in the previous task.

  2. The command that runs a clean build of your project (not an incremental build): see the command examples below.

PowerShell commands on a Windows host with an MSBuild build build-wrapper-win-x86/build-wrapper-win-x86-64.exe --out-dir <outputDirectory> MSBuild.exe /t:Rebuild

Bash commands on a Linux host with a make build build-wrapper-linux-x86/build-wrapper-linux-x86-64 --out-dir <outputDirectory> make clean all

Bash commands on a Linux ARM64 host with a make build build-wrapper-linux-aarch64/build-wrapper-linux-aarch64 --out-dir <outputDirectory> make clean all

Example of bash commands on a macos host with a xcodebuild build build-wrapper-macosx-x86/build-wrapper-macos-x86 --out-dir <outputDirectory> xcodebuild -project myproject.xcodeproj -configuration Release clean build

Step 4: Add a Run code analysis task

Add a SonarQube’s Run code analysis task to run the code analysis and make the results available to SonarQube. Consider running this task right after step 3’s Command line task as the build environment should not be significantly altered before running the analysis.

Step 5: Add a Publish quality gate result task.

Add a new SonarQube’s Publish quality gate Result task.

YAML file example

If you use YAML to create your Azure build pipeline, see the example below and also our YAML pipeline templates. For information about the SonarQube task inputs, see SonarQube tasks for Azure Pipelines.

trigger:
- master # or the name of your main branch
- feature/*

steps:
- checkout: self
  # disable shallow fetch
  fetchDepth: 0

# Make Build Wrapper available
- task: Bash@3
  displayName: Download Build Wrapper
  inputs:
    targetType: inline
    script: |
      curl  '<YourSonarQubeURL>/static/cpp/build-wrapper-linux-x86.zip' --output build-wrapper.zip
      unzip build-wrapper.zip

# Prepare Analysis Configuration task
- task: SonarQubePrepare@7
  inputs:
    SonarQube: '<YourSonarqubeServiceEndpoint>'
    scannerMode: 'cli'
    configMode: 'manual'
    cliProjectKey: '<YourProjectKey>'
    extraProperties:  |
      "sonar.cfamily.compile-commands=bw_output/compile_commands.json"

# Command Line task to run your build.
- task: Bash@3
  displayName: Bash Script
  inputs:
    targetType: inline
    script: |
      ./build-wrapper-linux-x86/build-wrapper-linux-x86-64 --out-dir bw_output <Your build command>

# Run Code Analysis task
- task: SonarQubeAnalyze@7

# Publish Quality Gate Result task
- task: SonarQubePublish@7
  inputs:
    pollingTimeoutSec: '300'

Last updated

Was this helpful?