SonarQube Analysis on Pull Request and not during build pipeline

2.3k Views Asked by At

Excuse my ignorance upfront, I am still a newbie.

Goal: SonarQube Analysis and Publish results on PR creation.

What I have at the moment:

Azure Devops with built-in Repo. local installs of git to push/pull Redgate SQL Source Control (our code is all SQL)

SonarQube Analysis and Publish results works in my build pipeline, I even have a build breaker built in and everything works.

The issue I am facing and wish to change is that the pipeline only triggers after the PR is completed and the merge is done to my default branch. I want the analysis to be done at the time the PR is created (and not completed) as the following step is code review. The result being that if it fails SQ Analysis that the code review step can be avoided and the required fixes be returned to the developer instead.

Having the Analysis done (on the feature branch) before it is checked again during the build phase (to check the code of the whole project) will be a massive plus.

Thanks in advance

trigger:
  - dev
jobs:
  - job: Rcs_Dev
    timeoutInMinutes: 0
    pool: Rcs Build
    cancelTimeoutInMinutes: 1
    steps:
      - checkout: self
        clean: true
      - task: SonarQubePrepare@4
        inputs:
          SonarQube: 'SonarQubeServiceConnection'
          scannerMode: 'CLI'
          configMode: 'manual'
          cliProjectKey: 'Rcs_Rcs'
          cliProjectName: 'Rcs'
          cliSources: '.'
      - task: SonarQubeAnalyze@4
        displayName: "Run Code Analysis"
        condition: and(succeeded(), or(eq(variables['Build.SourceBranchName'], 'dev'), contains(variables['Build.Reason'], 'PullRequest')))
      - task: SonarQubePublish@4
        inputs:
          pollingTimeoutSec: '300'
      - task: sonar-buildbreaker@8
        inputs:
          SonarQube: 'SonarQubeServiceConnection'
      - task: RedgateSqlChangeAutomationBuild@4
        inputs:
          operation: Build
          dbFolder: RootFolder
          packageName: Rcs
          tempServerTypeBuild: localDB
          buildAdvanced: true
          compareOptionsBuild: 'NoTransactions, IgnoreFileGroups'
          dataCompareOptionsBuild: 'DisableAndReenableDMLTriggers, SkipFkChecks'
          transactionIsolationLevelBuild: readCommitted
          queryBatchTimeoutBuild: '0'
          nugetPackageVersionSelector: Specific
          nugetPackageVersion: '1.0'
          nugetPackageVersionUseBuildId: true
          requiredVersionOfDlma: latestInstalled
1

There are 1 best solutions below

1
Daniel Campos Olivares On

If you want a concrete Pipeline to be triggered on PRs, then you have two options:

You trigger the Pipeline always (including all the branches and PRs) using:

trigger:
  branches:
    include:
    - '*' 

Or if you want to trigger on PRs against dev (what it seems the most probable scenario taking into account your description) then instead of trigger you should use:

pr:
- dev

Of course if you don't want to write twice the same Pipeline you can use the templates functionality, define the whole Pipeline only once and then extend it on each YAML, one for the normal builds with the branch trigger and one for the PRs trigger.

  1. Documentation about PR triggers: https://learn.microsoft.com/en-us/azure/devops/pipelines/repos/github?view=azure-devops&tabs=yaml#pr-triggers

  2. Documentation about Templates: https://learn.microsoft.com/en-us/azure/devops/pipelines/process/templates?view=azure-devops