Integrate SonarQube and GitLab CI via Docker
在開始這篇文章前,你 可能 需要有基本的 Linux 跟 Docker 基礎,才比較好嚼
Install Linux Ubuntu 20.4
已經有很詳細的中文文章,我就不花時間廢話了,點我
註:建議 Memory 給到 16G UP
Install Docker
官方的教學淺顯易懂,點我
下面是擷取官方的重點
Set up the repository
- Update the apt package index and install packages to allow apt to use a repository over HTTPS:
- Add Docker’s official GPG key:
- Use the following command to set up the stable repository. To add the nightly or test repository, add the word nightly or test (or both) after the word stable in the commands below. Learn about nightly and test channels.
Install Docker Engine
Update the apt package index, and install the latest version of Docker Engine and containerd, or go to the next step to install a specific version:
Install PostgreSQL
這邊先啟動 SonarQube 要使用的 External DB
使用 Embedded DB 會有諸多限制,單純實驗用
很簡單,就照著官方 Docker Image 文件操作即可,如果你有特別的需求也是可以抓 Dockerfile 來自己產 Image
Install SonarQube
這時候你的 DB 應該已經順利啟動
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
再來起 SonarQube 服務,第一次啟動會花不少時間在 DB 建表
有額外的插件(jar) 就丟進 sonarqube_extensions 裡,SonarQube 啟動會自動安裝
因為 Community-Edition 沒有提供 Branch 功能,所以這裡我會丟免付費版本的 Branch 套件: sonarqube-community-branch-plugin.jar
Install GitLab Runner
如果你的 GitLab 已經有可以使用的 Runner,那你可以跳過這個步驟
官方文件在 這裡
Register Runner
- Run the register command based on the mount type:
- Enter your GitLab instance URL (also known as the gitlab-ci coordinator URL).
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Enter the token you obtained to register the runner.
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
- Enter a description for the runner. You can change this value later in the GitLab user interface.
- Enter the tags associated with the runner, separated by commas. You can change this value later in the GitLab user interface.
- Provide the runner executor. For most use cases, enter docker.
- If you entered docker as your executor, you’ll be asked for the default image to be used for projects that do not define one in .gitlab-ci.yml.
Import Projects from GitLab
按照 文件 一步一步來,應該沒啥問題
你可以跳過這個步驟,直接設定單個 GitLab Project,只是我認為大量匯入,之後會比較方便
Set up a Project
終於到了建立 SonarQube 分析專案的步驟了
前面我們建立了 GitLab Runner 並且註冊 GitLab Project 都是為了讓 GitLab CI 運作
這裡選 GitLab CI 去執行分析工作
Image Not Showing
Possible Reasons
- The image file may be corrupted
- The server hosting the image is unavailable
- The image path is incorrect
- The image format is not supported
Learn More →
接下來 SonarQube 會按步驟幫助你建立 SonarQube 跟 GitLab CI 的關係
sonar-project.properties
排除多個指定資料夾
.gitlab-ci.yml
如果需要在 branch 使用, 要移除 only 區塊
Analysis Reports
報告的規則、問題解決流程、分析數據,官方 都有一一解釋

docker-compose
分別起 container 實在太麻煩,你可以直接用 docker-compose 一次啟動這些服務
Install docker-compose
官方安裝手冊
create docker-compose
GitLab Runner 相對比較獨立,於是沒放進 docker-compose
預期他會在其他地方,如:GKE, K8S
run docker-compose