【 Tool 】於 Ubuntu 透過 Docker 建置 SonarQube Community Edition

內容

  • 前置準備作業
  • 安裝 docker-compose
  • 修改設定
  • 安裝 SonarQube Community 版本
  • 修改 SonarQube 登入的預設密碼
  • 程式專案設定與掃描

前置準備作業

  • 已有可上網的 Ubuntu 電腦且已開啟 9000 Port

安裝 docker-compose

Step 1. 取得遠端更新伺服器的套件檔案清單

  • 請於端端機輸入下方指令
sudo apt-get update


Step 2. 安裝 docker-compose

  • 請於端端機輸入下方指令
sudo apt install -y docker-compose


Step 3. 版本確認

  • 請於端端機輸入下方指令
docker-compose --version

修改設定

Step 1. 開啟 /etc/sysctl.conf

  • 請於端端機輸入下方指令
sudo vi /etc/sysctl.conf

Step 2. 新增設定

  • /etc/sysctl.conf 檔案最後加入下面兩行設定
vm.max_map_count=262144
fs.file-max=65536


Step 3. 設定生效

  • 請於端端機輸入下方指令
sudo sysctl -p

安裝 SonarQube Community 版本

Step 1. 建立 docker-compose.yml

  • 其檔案內容新增如下


Step 2. 安裝 SonarQube

  • 請於端端機輸入下方指令
sudo docker-compose up -d


Step 3. 確認 Docker Images

  • 請於端端機輸入下方指令
sudo docker images


Step 4. 確認 Docker Container

  • 請於端端機輸入下方指令
sudo docker ps

修改 SonarQube 登入的預設密碼

Step 1. 透過瀏覽器開啟 SonarQube

  • 網址 http://SonarQube-IP:9000


Step 2. 修改預設密碼

  • 使用者登入帳號為 admin、預設登入密碼為 admin

程式專案設定與掃描

Step 1. 點選 Manually


Step 2. 輸入專案相關資訊


Step 3. 設定 baseline

  • 依需求來選擇,本範例選擇 Use the global setting


Step 4. 選擇所要分析的 Repository

  • 本範例使用 GitHub,故選擇 With GitHub Actions


Step 5. 依需求建立 GitHub Secrets

  • 於 GitHub 建立 SONAR_TOKENSONAR_HOST_URL 的 Secrets


Step 6. 選擇語言

  • 本範例選擇 Other (for JS, TS, Go, Python, PHP, ...)


Step 7. 依要求建立相關設定檔

  • 檔案結構
├── README.md
├── .github
|   └── workflows
|       └── build.yml
└── sonar-project.properties
  • sonar-project.properties
  • .github/workflows/build.yml

Step 8. Commit 程式到 GitHub 並查看 GitHub Action 與 SonarQube 狀態

  • GitHub Action

  • SonarQube


Step 9. 上傳要掃描的程式碼並查看 GitHub Action 與 SonarQube 狀態

  • 檔案結構
├── README.md
├── app.py
├── .github
|   └── workflows
|       └── build.yml
└── sonar-project.properties
  • GitHub Action

  • SonarQube

  • Bug

  • Vulnerabilities

Sample Code

GitHub

List of blogs