Devops——Sonarqube+github+jenkins構(gòu)建

在CICD的流程中對(duì)于的代碼檢查是必要的,在工程中Sonarqube是必不可少的工作,構(gòu)建CICD的系統(tǒng)能幫助我們實(shí)現(xiàn)相關(guān)的代碼檢查工作。接下來(lái)我介紹的Sonarqube的構(gòu)建個(gè)使用,同時(shí)與github jeknins等工具一起構(gòu)建CICD系統(tǒng)。
原文:https://blog.csdn.net/weixin_41605937/article/details/122244691
https://blog.csdn.net/qq_35550345/article/details/103586647

一、Sonarqube的簡(jiǎn)介

sonarQube 是一款開(kāi)源代碼檢測(cè)工具。本篇介紹通過(guò) docker 來(lái)安裝。大概的一個(gè)運(yùn)作流程是這樣的,先通過(guò) sonar-scanner 插件掃描代碼,把數(shù)據(jù)存儲(chǔ)到數(shù)據(jù)庫(kù),sonarQube 讀取數(shù)據(jù)庫(kù),將數(shù)據(jù)庫(kù)展現(xiàn)在 web 平臺(tái)。

sonarQube

二、Sonarqube在doceker構(gòu)建

拉取兩個(gè)Docker鏡像

docker pull sonarqube
docker pull postgres
# 運(yùn)行數(shù)據(jù)庫(kù)容器
docker run --name postgresql -e POSTGRES_USER=sonar -e POSTGRES_PASSWORD=sonar -d postgres
# 運(yùn)行sonarqube
docker run --name sonarqube --link postgresql -e SONARQUBE_JDBC_URL=jdbc:postgresql://postgresql:5432/sonar -p 9000:9000 -d sonarqube
image.png

三、SonarQube+Github配置

SonarQube是一個(gè)自動(dòng)代碼審查工具,用于檢測(cè)代碼中的錯(cuò)誤、漏洞和不規(guī)范的編碼風(fēng)格。本文實(shí)現(xiàn)利用GithubAction實(shí)現(xiàn)在代碼提交時(shí)自動(dòng)使用SonarQube審查代碼。

3.1 創(chuàng)建并安裝Github APP

3.1.1 進(jìn)去github設(shè)置頁(yè)面

image.png

3.1.2 點(diǎn)擊Developer settings

image.png

3.1.3 點(diǎn)擊New Github App按鈕創(chuàng)建Github App

image.png

3.1.4 填寫(xiě)信息并設(shè)置權(quán)限

image.png

image.png

3.1.5 創(chuàng)建成功并生成客戶端密鑰

https://blog.csdn.net/weixin_41605937/article/details/122244691
3.1.6 生成私鑰并導(dǎo)入
3.2 配置SonarQube
3.2.1 在SonarQube中配置的ALM集成,配置Github
3.2.2 根據(jù)剛才創(chuàng)建Github APP生成的信息創(chuàng)建Github配置
3.2.3 創(chuàng)建SonarQube token
3.3.4 使用手工模式創(chuàng)建
3.3.5 填寫(xiě)項(xiàng)目標(biāo)識(shí)和項(xiàng)目名
3.3.6 記錄生成的token, 之后要用
3.4 編寫(xiě)Github Action
3.4.1 創(chuàng)建Github Action以便在提交代碼時(shí)自動(dòng)分析并將分析結(jié)果傳到SonarQube
3.4.2 在.github/workflows/目錄下創(chuàng)建build.yml文件

name: Build
on:
  push:
    branches:
      - main
  pull_request:
    types: [opened, synchronize, reopened]
jobs:
  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
        with:
          fetch-depth: 0  # Shallow clones should be disabled for a better relevancy of analysis
      - name: Set up JDK 11   
        uses: actions/setup-java@v1
        with:
          java-version: 11      # 項(xiàng)目使用的jdk版本
      - name: Cache SonarQube packages
        uses: actions/cache@v1
        with:
          path: ~/.sonar/cache
          key: ${{ runner.os }}-sonar
          restore-keys: ${{ runner.os }}-sonar
      - name: Cache Maven packages
        uses: actions/cache@v1
        with:
          path: ~/.m2
          key: ${{ runner.os }}-m2-${{ hashFiles('**/pom.xml') }}
          restore-keys: ${{ runner.os }}-m2
      - name: Build and analyze
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}  # Needed to get PR information, if any
          SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
          SONAR_HOST_URL: ${{ secrets.SONAR_HOST_URL }}
        run: mvn -B verify org.sonarsource.scanner.maven:sonar-maven-plugin:sonar
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容