🔴 FREE LIVE WEBINAR
How to Become a DevOps Engineer in 2027
From Zero to DevOps Champion!
November 21st, 2026
9:30 AM CST
Live DevSecOps CI/CD Demo
Career Roadmap • Live Q&A
REGISTER FOR FREE →
100% FREE • Live on Zoom
🎁 FREE CAREER GUIDE
2027 AI-Enabled DevSecOps Career Roadmap
Your Career Journey Starts Here!
📘
9-Stage Learning Roadmap
15-Week Study Plan
8-Page PDF + 12 Interview Q&As
GET FREE ROADMAP →
100% FREE • Delivered to Your Inbox
🚀 NEW WEEKEND BATCH
AI-Enabled DevSecOps & Cloud Engineering Bootcamp
Starts December 5th, 2026
Sat: 9:30 AM – 11:30 AM CST
Sun: 10:30 AM – 12:30 PM CST

15 Weeks • 70+ Labs • Live Training
REGISTER FOR BOOTCAMP → Explore Bootcamp →
Enrollment Open!
3,500+ Students Trained
25+ Years of IT Experience
70+ Hands-On Labs
🔴 Free Webinar Register Free
🎁 Free Roadmap Get Roadmap
🚀 Weekend Batch Enroll Now →

Tuesday, March 12, 2024

How to Create Quality Gate in SonarQube and integrate with GitHub Actions | SonarQube Integration with GitHub Actions | Automate Code Scan using SonarQube In GitHub Actions and Force build to Fail or Pass



Pre-requisites:

How to Create Quality gate in SonarQube and integrate with GitHub Actions?

Make sure SonarQube is up and running and integrated with GitHub Actions. Please click here if you would like to setup SonarQube and integrate with GitHub Actions.

We will be executing below steps:
  • Login to SonarQube
  • Create Quality Gate in SonarQube
  • Add conditions in Quality Gate
  • Make quality gate as Default
  • Create GitHub Actions CICD workflow yaml
  • Add tasks for Maven build and Sonar Scan
  • Add tasks for integrating Quality gate 
  • pass/fail the builds in SonarQube

What is Quality gate?

In SonarQube a quality gate is a set of conditions that must be met in order for a project to be marked as passed.

Create Quality Gate

Login to SonarQube, Click on Quality gate, enter some name

Once you create the quality gate. Click on Add condition. 

Select new issues from the drop down and enter 2 



Select new bugs from the drop down and enter 1 as error


Setup a Default Gate


Create GitHub Actions CICD workflow yaml:

Go to GitHub repo where your Java project is, create a new file:

.github/workflows/cicd.yml


The below file have four steps(tasks) 
    - Checkout
    - Install Java on runner
    - Build using Maven
    - run Sonar Scan (this task need to have projectKey defined, otherwise build will fail)
    - run quality gate check
    - pass/fail the build

Copy the the whole yellow color marked content from below:

name: CI/CD workflow for Maven Build, Sonar Code scan and Quality gate check
on:
  push:
    branches:
      - main
  workflow_dispatch:
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Set up JDK 21
      uses: actions/setup-java@v4
      with:
        distribution: 'temurin'
        java-version: '21'
        cache: 'maven'

    - name: Build with Maven
      run: mvn clean install -f MyWebApp/pom.xml

    - name: SonarQube Scan with Quality Gate
      run: |
        mvn -f MyWebApp/pom.xml org.sonarsource.scanner.maven:sonar-maven-plugin:4.0.0.4121:sonar \
          -Dsonar.projectKey=MyWebApp \
          -Dsonar.projectName=MyWebApp \
          -Dsonar.host.url=${{ secrets.SONAR_HOST_URL }} \
          -Dsonar.token=${{ secrets.SONAR_TOKEN }} \   
          -Dsonar.qualitygate.wait=true
       
Commit the file.

As soon as you commit, build will run immediately in GitHub Actions. 
Now you can see the output of build in Actions tab.




Now login to SonarQube to see the Scan report


If your code have any defects, you can see some build fails.

SonarQube Quality gate failed:

Watch Steps in YouTube channel:

No comments:

Post a Comment