How to Integrate SonarQube Quality Gate in Jenkins Pipeline

Kadaie KyiphyuKadaie Kyiphyu
4 min read
💡
CI/CD Tools တွေကို အခုမှ စလေ့လာနေတဲ့ သူတွေအတွက် အထောက်အကူ ဖြစ်စေဖို့ရည်ရွယ်ပါတယ်။ 😊

ဒီ Lab ကိုမစမ်းခင်မှာ ...

SonarQube Server ၊ Scanner နဲ့ Quality Gate တို့ရဲ့ work flow ကိုမသိသေးရင်တော့ ဒီမှာအရင်ဆုံးသွားကြည့်နိုင်ပါတယ်။

SonarQube Scanner နဲ့ SonarQube Server Configuration လည်းအရင်လုပ်ထားဖို့လိုပါမယ်။

ဒီအပိုင်းမှာတော့ SonarQube Server ဆီရောက်လာတဲ့ report တွေကနေတဆင့် Quality Gate မှာ Condition တွေသတ်မှတ်ပြီး Jenkins pipeline ကို ဆက်သွားမလား ရပ်မလား ဆိုတဲ့ integration အဆင့်ကို လုပ်ပြသွားမှာပါ။

ဥပမာအားဖြင့် Quality Gate Condition မှာ Bugs က ၄ ခု ထက်ပိုနေရင် Pipeline ကို Failed လုပ်မယ် Abort လုပ်လိုက်လို့သတ်မှတ်ထားလိုက်မယ်။ အဲ့တာဆို Reports ထဲမှာ Bugs 4 ခု ပါတဲ့အထိ လက်ခံသေးတယ် Pipeline continuous ဆက် run မယ်ပေါ့။ Bugs 4 ခုထက်ပိုပြီး Bugs 5 ခုကစလို့ အများကြီးပါလာပြီဆိုရင် Pipeline က Failed ဖြစ်သွားမယ် Abort ဖြစ်သွားမယ်ပေါ့။

သေချာထပ်ပြောရမယ်ဆိုရင် Quality Gate ကသတ်မှတ်ထားတဲ့ Condition ထက် မပိုနေဘူး Bugs 4 ခု အောက်နည်းနေမယ်ဆိုရင် SonarQube server ကနေ Jenkins ဆီကို ပို့တဲ့ webhook JSON Payload ထဲမှာ Quality Gate Status ကို OK နဲ့ ပို့မယ်ပေါ့။ Bugs 4 ခု ထက်ပိုသွားရင်တော့ webhook JSON Payload ထဲက Quality Gate Status ကို ERROR ဆိုပြီး ထည့်ပို့လိုက်လိမ့်မယ်။ အဲ့တော့ အဲ့ဒီရောက်လာတဲ့ webhook JSON Payload ကို Jenkins pipeline ထဲမှာ Integrate လုပ်ထားတဲ့ Quality Gate Stage ကနေစောင့်ကြည့်ပြီး OK ဆိုရင် Pass ဖြစ်ပြီးဆက်သွားမယ် ERROR ဆိုတာနဲ့ FAILED ဖြစ်မယ် Pipeline Abort ဖြစ်သွားမယ်ပေါ့။

ဒီလောက်ဆိုရင်တော့ Quality Gate က Jenkins နဲ့ဘယ်လိုတွဲအလုပ်လုပ်လည်း နည်းနည်းတော့ရှင်းသွားမယ်ထင်ပါတယ်။

လုပ်ရမယ် Step တွေကတော့

  • အရင်ဆုံး Webhook တစ်ခု Create လုပ်ရပါမယ်။

  • Webhook Create လုပ်ရင်း သတ်မှတ်လိုက်တဲ့ secret ကို Jenkins မှာ Credential တစ်ခု အနေနဲ့ Create လုပ်ရပါမယ်။ Server Config မှာ Webhook secret ပြန်ရွေးပေးဖို့ လိုပါမယ်။

  • ပြီးရင်တော့ Pipeline ထဲမှာ Quality Gate Stage တစ်ခု ထည့်လိုက်ရင်ရပါပြီ။

Webhook တစ်ခု Create လုပ်ဖို့အတွက် SonarQube Server Console ဆီကိုသွားလိုက်ပါ။

ပြီးရင်တော့ Administration > Configuration > Webhooks ထဲထိရောက်တဲ့အထိသွားလိုက်ပါ။

ပြီးရင်တော့ ညာဘက်ဘေးနားက Generate ကို နှိပ်လိုက်ပါ။ အဲ့ဒီမှာ name ကတော့ ကိုယ်ကြိုက်ရာပေးပါ။ URL ကတော့ Jenkins မှာ SonarQube Scanner Plugin install လုပ်တုန်းက ဖြစ်ပေါ်လာတဲ့ URL Address ကိုထည့်ပေးရမှာပါ။ default အားဖြင့် JENKINS_URL/sonarqube-webhook/ orhttp://172.31.85.252:8080/sonarqube-webhook/ ဆိုပြီး ထည့်လိုက်ပါ နောက်ဆုံးက / slash လေးကိုမမေ့ပါနဲ့ ။ ဒီနေရာမှာ Jenkins ရဲ့ Private IP ကိုပဲသုံးလိုက်ပါတယ်။ Secret ကတော့ နှစ်သက်ရာ password တစ်ခုခုကိုထည့်ပြီး မှတ်သားလာခဲ့ပါ၊ Jenkins မှာ Credentials အနေနဲ့ Create လုပ်ရမှာပါ။

Webhook Create ပြီးရင်တော့ Jenkins Console ဆီကိုပြန်သွားပါမယ် Manage Jenkins > Credentials ထဲကိုရောက်ပြီးဆိုရင် Global ထဲကိုဆက်သွားပြီး Credential တစ်ခု Create လုပ်ရပါမယ်။ အဲ့ဒီမှာ Kind ကို Secret text ရွေးပါမယ်။ Secret နေရာမှာ အခုဏ သတ်မှတ်ခဲ့တဲ့ webhook secret ကိုထည့်ရပါမယ် ပြီးရင်တော့ နှစ်သက်ရာ id တစ်ခုခု သတ်မှတ်ပြီး Create လိုက်ရင်ရပါပြီ။

webhook secret ကို Jenkins credential အနေနဲ့ရပြီဆိုရင်တော့ Jenkins console ထဲက SonarQube Server Configuration ဆီကိုပြန်သွားရပါမယ်။ Jenkins console > Manage Jenkins > Systems အထဲကိုရောက်ပြီဆိုရင် SonarQube servers ဆိုတာကို ရှာပါ။ သူ့အောက်မှာ SonarQube Installation ဆိုတဲ့အောက်မှာ Advance ဆိုတဲ့ နေရာရှိပါတယ်။အဲ့တာကိုနှိပ်ပြီး Webhook Secret ဆိုနေရာမှာ Create လုပ်ခဲ့တဲ့ webhook hook secret credential ကို ရွေးရမှာပါ။ပြီးရင် Save and Apply လိုက်ပါရပါပြီ။

နောက်ဆုံးအဆင့်အနေနဲ့ Pipeline ထဲမှာ Quality Gate Stage တစ်ခု ကို ထပ်ထည့်ပါမယ်။ Jenkins ဖိုင်ကိုဖွင့်လိုက်ပါ။ ပြီးရင်တော့ SonarQube Analysis stage အောက်မှာ ဒါလေးထည့်လိုက်ရင်ရပါပြီ အသေးစိတ်ကိုတော့ ဒီ link ကနေလည်း သွားဖတ်ကြည့်နိုင်ပါတယ်။ https://www.jenkins.io/doc/pipeline/steps/sonar/

stage ('Quality Gate') {
    steps {
        waitForQualityGate abortPipeline: true
    }
}

waitForQualityGate: Wait for SonarQube analysis to be completed and return quality gate status

Full Jenkinsfile

pipeline {
    agent any
    stages {
        stage('Checkout Source') {
            steps {
                checkout scm
            }
        }
        stage('SonarQube Analysis') {
            steps {
                script{
                    def scannerHome = tool name: 'Sonar', type: 'hudson.plugins.sonar.SonarRunnerInstallation';
                    withSonarQubeEnv() {
                        sh "${scannerHome}/bin/sonar-scanner"
                    }
                }
            }
        }
        stage ('Quality Gate') {
            steps {
                waitForQualityGate abortPipeline: true
            }
        }   
        stage('Build') {
            steps {
                echo 'Building....'
                echo  'Build completed.'
            }
        }
        stage('Deploy') {
            steps {
                echo 'Deploying....'
                echo  'Deploy completed.'
            }
        }
    }
}

Jenkins ဖိုင်ကို update လုပ်ပြီးရင်တော့ Repo ဆီ ပြန် push လုပ်ပြီး Pipeline Run သွားတာကိုကြည့်လိုက်ပါ။

Blue Ocean မှာ လည်းကြည့်နိုင်ပါတယ်။ Blue Ocean ကိုမသိသေးရင်ဒီမှာသွားကြည့်နိုင်ပါတယ်။

ဒီ testing မှာတော့ ညီမသုံးထားတဲ့ Application Source Code မှာ Bugs တွေအများကြီးရှိနေတာမလို့ Quality Gate status ERROR ပြပြီး Pipeline တန်း failed သွားပါတယ်။

ဒါကတော့ SonarQube Server မှာ သတ်မှတ်ထားတဲ့ Quality Gate ရဲ့ Default Conditions တွေကြောင့်ပဲဖြစ်ပါတယ်။ SonarQube Server ကို သွားကြည့်ကြရအောင်ပါ။ အခုလို အများကြီးတွေ့ရပါတယ်။

ဒီမှာကတော့ Sonar Way ဆိုတဲ့ Build-in Quality Gate တစ်ခုကိုတွေ့ ရမှာပါ ဘေးနားက Create ကို နှိပ်ပြီး အသစ်တစ်ခု ဖန်တီးနိုင်ပါတယ်။ Name တစ်ခု ပေးပြီး Create လိုက်ပါ။

ပြီးရင်တော့ အောက်က Default ပါလာတဲ့ Condition တွေကိုတွေ့ရပါလိမ့်မယ်။

ပြင်ချင်ရင်တော့အောက်က Unlock editing ကိုနှိပ်ပြီ ပြင်တာဖြစ်ဖြစ် အသစ် ထပ်ထည့်ချင်တာဖြစ်ဖြစ် လုပ်လို့ရပါတယ်။

Unlock editing လုပ်ပြီးတဲ့အခါ ဒီတိုင်းတွေ့ရမှာပါ။ ညီမ ကတော့ Condition တစ်ခု အသစ် လုပ်ပြပြီး Effect ဖြစ်မဖြစ် စမ်းကြည့်ချင်တာမလို့ အကုန် ဖျက်ချလိုက်ပါမယ် ဘေးနားမှာ Delete icon လေးပါပါတယ်။

ဒီမှာ Measures မှာဆိုရင် Critical issues 863 ခု ရှိပါတယ်။အဲ့တာကို Conditions စစ်ပါတယ်။

အသစ် Createလုပ်ထားတဲ့ Quality Gate ကို ပြန်သွားလိုက်ပါ Add Conditions ကိုနှိပ်ပြီးအသစ်ထည့်လို့ရပါတယ်။

Critical issues ကို စစ်မှာ မလို့ ရှာ ပြီး Add လိုက်ပါမယ်။

အခုဏ သွားကြည့်ခဲ့တာ Critical issues 863 ရှိတာမလို့ 864 ထားကြည့်လိုက်ပါမယ်။ ဘေးနားက Operator က is greater than ပါ။ ဆိုလိုတာက Critical issues 864 ထက်ပိုရင် webhook ကနေ ERROR လိုပို့မယ် Pipeline က Failed ဖြစ်သွားမယ် Abort ဖြစ်သွားမယ်ပေါ့။ အကယ်၍ 864 ထက်နည်းနေရင်တော့ OK ပို့မယ် Pipeline ကဆက်သွားမယ်ပေါ့။ ဒီနေရာမှာတော့ 864 ထက်နည်းပြီးသားမို့ pipeline က သေချာပေါက် Pass ဖြစ်ပြီးဆက်သွားပါမယ်။

Condition ကို စိတ်ကြိုက်သတ်မှတ်ပြီးသွားပြီဆိုရင်တော့ အသစ် Create လုပ်ထားတဲ့ Quality Gate ကို Project နဲ့ တွဲပေးဖို့လိုပါသေးတယ်။ အောက်က project မှာ all ကို နှိပ် Project name ကို ရှာပြီး select လုပ်လိုက်ရုံပါပဲ။

အဲ့လို မလုပ်ပဲ Project ထဲကိုသွားပြီး Project Settings > Quality Gate မှာ ကိုယ်သတ်မှတ်ချင်တဲ့ Quality Gate ကို ရွေးပေးလိုက်ရင်လည်းရပါတယ်။ ဒါကတော့ Quality Gate ကို Project မှာ တွဲလိုက်တာပါပဲ။

Project နဲ့ Quality Gate တွဲပြီးပြီဆိုတာနဲ့ စမ်းကြည့်ဖို့ Pipeline ကို ပြန် run လိုက်ပါ။ ပြီး ရင် Pipeline Console output or blue ocean မှာသွားကြည့်နိုင်ပါတယ်။ Quality Gate OK ပြီး Pipeline ဆက် run သွားတာကိုတွေ့ရပါလိမ့်မယ်။

အခု အခါ Condition ကို 860 ဆိုပြီး နည်းထားလိုက်ပါပြီ။ တကယ် Critical issue တက်နေတာက 863 ခုပါ Condition ထက်များနေပါတယ်။ ဘာဖြစ်မလဲသိချင်တာမလို့ Pipeline ကို ပြန် Run ကြည့်ပါမယ်။

ဒီတစ်ခေါက်မှာတော့ Quality Gate Status က ERROR လို့ ပြပြီး Pipeline က Failed သွားခဲ့ပါပြီ။ Critical issues က သတ်မှတ်ထားတဲ့ Quality Gate Condition 860 ထက်များနေလို့ပါ။

အဲ့တော့အပေါ်မှာပြောခဲ့တဲ့ Jenkins ဆီကို ပြန်ပို့လိုက်တယ်ဆိုတဲ့ webhook JSON Payload ကိုလည်း သွားကြည့်နိုင်ပါတယ်။ ဒါက Quality Gate Status ERROR ပြတာပါ။

ဒါက Quality Gate Status OK ပြတာပါ။

အဲ့တာတွေကိုဘယ်မှာသွားကြည့်လို့ရလဲဆိုတော့ SonarQube Server > Administration > Configuration > Webhook ထဲမှာ ပြန်သွားကြည့်နိုင်ပါတယ်။

ဒါဆိုရင်တော့ Quality Gate ရဲ့ Lab လေးစမ်းကြည့်မယ်ဆိုရင်တော့အဆင်ပြေလောက်မယ်လို့မျော်လင့်ပါတယ်။ SonarQube ရဲ့ work flow ကိုတော့ မသိသေးရင် အခက်တွေ့နိုင်ပါတယ်။ အခက်ခဲရှိရင်ဒီမှာ ဖတ်ကြည့်ပြီးပြန်စမ်းနိုင်ပါတယ်။ ကျေးဇူးတင်ပါတယ်။

0
Subscribe to my newsletter

Read articles from Kadaie Kyiphyu directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Kadaie Kyiphyu
Kadaie Kyiphyu