# Docker install Sonarqube [Docker安裝及基本操作](https://medium.com/@jieshiun/%E9%96%8B%E6%BA%90%E7%A8%8B%E5%BC%8F%E7%A2%BC%E6%AA%A2%E6%B8%AC%E5%B9%B3%E5%8F%B0-sonarqube-%E5%85%A5%E9%96%80-67a96a9c1d32) ```shell docker pull sonarqube:8.9-community docker run --name sonarqube:8.9-community --restart always -p 9000:9000 -d sonarqube --以下為上傳檔案指令 dotnet tool install --global dotnet-sonarscanner dotnet sonarscanner begin /k:"LionTech.InAPI.ERPAPI" /d:sonar.host.url="http://localhost:9000" /d:sonar.login="2b568296c4a84fc804e7dfa794945d456521b8d0" dotnet build dotnet sonarscanner end /d:sonar.login="2b568296c4a84fc804e7dfa794945d456521b8d0" ``` [若指令出現以下錯誤,請記得安裝 JDK。](https://www.oracle.com/java/technologies/downloads/?er=221886#jdk17-windows) ![image](https://hackmd.io/_uploads/HJwnitetA.png) 安裝完成後, 1. windows+R,輸入sysdm.cpl開啟系統內容 2. 到進階-> 環境變數 -> 系統變數 3. 新增 變數名稱:JAVA_HOME。變數值:安裝JDK的路徑 4. 確認變更後,須重啟一個cmd變數值才會生效 5. java -version (cmd 確認是否安裝成功。) # minikube install Sonarqube ### 1. Namespace 新增命名空間 ```shell kubectl create namespace (metadata.namespace) (為了資源隔離或資源配額,且易於管理) ``` ### 2. Apply yaml 應用 ```shell kubectl apply -f <path-to-your-.yaml> -n (metadata.namespace) (pvc 要建在跟deplyment.yaml同一個namespace,這樣才找的到pvc) ``` ```yaml apiVersion: apps/v1 kind: Deployment metadata: name: sonarqube namespace: sonarqube spec: replicas: 1 selector: matchLabels: app: sonarqube template: metadata: labels: app: sonarqube spec: containers: - name: sonarqube image: sonarqube:8.9.2-community ports: - containerPort: 9000 env: - name: SONAR_JDBC_URL value: "jdbc:sqlserver://<Docker IPv4 Address>;databaseName=sonar;" #encrypt=true;trustServerCertificate=true; 9.9以後版本才會加這段進去 - name: SONAR_JDBC_USERNAME value: "sa" - name: SONAR_JDBC_PASSWORD value: "Password@" volumeMounts: - mountPath: /opt/sonarqube/data name: sonarqube subPath: data - mountPath: /opt/sonarqube/temp name: sonarqube subPath: temp - mountPath: /opt/sonarqube/logs name: sonarqube subPath: logs - mountPath: /tmp name: tmp-dir volumes: - name: sonarqube #與上面要儲存永久目錄的名稱要對到 persistentVolumeClaim: claimName: sonarqube-pvc - name: tmp-dir emptyDir: {} #臨時目錄 ``` - 連接mssql container using <Docker IPv4 Address> ![image](https://hackmd.io/_uploads/Hkjv7kIt0.png) - Some issue mentioned that "encrypt=true;trustServerCertificate=true" might not work in production env. ( env環境變數內,連接到mssql DB的JDBC連線字串,有issue說"encrypt=true;trustServerCertificate=true"這段只適用於測試環境 ) [LTA version的變動](https://docs.sonarsource.com/sonarqube/9.9/setup-and-upgrade/lts-to-lts-release-upgrade-notes/) [mssql 連線字串error](https://stackoverflow.com/questions/32766114/sql-server-jdbc-error-on-java-8-the-driver-could-not-establish-a-secure-connect) [環境變數版本的變更](https://docs.sonarsource.com/sonarqube/latest/setup-and-upgrade/configure-and-operate-a-server/environment-variables/) #### PVC ```yaml apiVersion: v1 kind: PersistentVolumeClaim metadata: name: sonarqube-pvc namespace: sonarqube spec: accessModes: - ReadWriteOnce resources: requests: storage: 10Gi ``` #### MSSQL ```yaml mssql-2019: image: mcr.microsoft.com/mssql/server:2019-latest ports: - "1433:1433" environment: ACCEPT_EULA: Y MSSQL_SA_PASSWORD: Password@ networks: - backend ``` ### 3. port-forward 端口轉發 ![image](https://hackmd.io/_uploads/SkCT1z8K0.png) ### 4. DB 資料確認 ![image](https://hackmd.io/_uploads/r1-sR-8YC.png) ### 5. Upgrade 升級版本 [Sonarqube Upgrade guide](https://docs.sonarsource.com/sonarqube/9.9/setup-and-upgrade/upgrade-the-server/upgrade-guide/) ```! If there is at least one LTA version in your upgrade path, you must first upgrade to each intermediate LTA and then to your target version. When upgrading to an LTA version, you should directly upgrade to its latest patch. According to Sonarqube's documentation, you can't just upgrade from an older version to the latest. You have to first upgrade through all previous latest LTS versions before reaching your target. 升級至目標版本前,必須先逐版升級至各LTS版本。 Example: Target Vesion(目標版本):10.6 Step: 8.9 LTA -> 9.9 LTA (升級至最近的LTS) -> 10.6 ``` ![image](https://hackmd.io/_uploads/HkFREzIY0.png) 1. 修改yaml image version to 9.9-community 2. Apply yaml again 3. 開啟Sonarqube會出現Maintenance維護狀態 ![image](https://hackmd.io/_uploads/rk63GQ8F0.png) 4. 將網址/maintenance....修改成/Setup,出現Upgrade Database畫面 ![image](https://hackmd.io/_uploads/S1Mk77ItA.png) 5. DB Upgrade完成,升級完成 (圖為版本10.6) ![image](https://hackmd.io/_uploads/r1qowmLFR.png) 6. 重複上述步驟升級到指定版本(target version) ## Upgrading a Helm chart instance [Upgrading a Helm chart instance](https://docs.sonarsource.com/sonarqube/latest/setup-and-upgrade/upgrade-the-server/upgrade/#Docker-instance) 1. Change the SonarQube version on your values.yaml. 2. Redeploy SonarQube with the same helm chart: ```shell helm upgrade --install -f values.yaml -n <your namespace> <your release > </your>name> <path to sonarqube helm chart> ``` 3. If you're upgrading a Data Center Edition: after SonarQube search pods are running and ready, only one application (app) replica will be running and ready. You can confirm that it's because of the ongoing upgrade by inspecting the logs of the pod for this text: **The database must be manually upgraded. Please backup the database and browse /setup.** 4. Change http://yourSonarQubeServerURL/maintenance...... to http://yourSonarQubeServerURL/setup and press the upgrade to upgrade database. 5. Reanalyze your projects to get fresh data. - If you are using an external database, you don't have any persistent data inside kubernetes. Therefore, there is no action required.