内容概要:

  1. postgres部署
  2. sonar部署
  3. 插件安装
  4. 整合sonar和maven
  5. 整合和sonar和idea
  6. 整合sonar和eclipse
  7. 整合sonar和jenkins

1. postgress部署

采用k8s持久化部署

postgress.yaml

---
apiVersion: v1
kind: ConfigMap
metadata:
  name: postgres-config
  labels:
    app: postgres
data:
  POSTGRES_DB: postgresdb
  POSTGRES_USER: postgresadmin
  POSTGRES_PASSWORD: admin123
kind: PersistentVolume
---
apiVersion: v1
metadata:
  name: postgres-pv-volume
  labels:
    type: local
    app: postgres
spec:
  storageClassName: manual
  capacity:
    storage: 5Gi
  accessModes:
    - ReadWriteMany
  hostPath:
    path: "/mnt/data"
---
kind: PersistentVolumeClaim
apiVersion: v1
metadata:
  name: postgres-pv-claim
  labels:
    app: postgres
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteMany
  resources:
    requests:
      storage: 5Gi
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres
  template:
    metadata:
      labels:
        app: postgres
    spec:
      containers:
        - name: postgres
          image: postgres:10.4
          imagePullPolicy: "IfNotPresent"
          ports:
            - containerPort: 5432
          envFrom:
            - configMapRef:
                name: postgres-config
          volumeMounts:
            - mountPath: /var/lib/postgresql/data
              name: postgredb
      volumes:
        - name: postgredb
          persistentVolumeClaim:
            claimName: postgres-pv-claim
---
apiVersion: v1
kind: Service
metadata:
  name: postgres
  labels:
    app: postgres
spec:
  type: NodePort
  ports:
   - port: 5432
  selector:
   app: postgres

2. sonar部署

sonar.yaml

---
apiVersion: v1
kind: Service
metadata:
  name: sonarqube
  labels:
    app: sonarqube
spec:
  type: NodePort            #指定 NodePort 端口
  ports:
    - name: sonarqube
      port: 9000
      targetPort: 9000
      protocol: TCP
  selector:
    app: sonarqube
---
apiVersion: apps/v1
kind: Deployment
metadata:
  name: sonarqube
  labels:
    app: sonarqube
spec:
  replicas: 1
  selector:
    matchLabels:
      app: sonarqube
  template:
    metadata:
      labels:
        app: sonarqube
    spec:
      initContainers:               #设置初始化镜像,执行 system 命令
      - name: init-sysctl
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ["sysctl", "-w", "vm.max_map_count=262144"]  #必须设置vm.max_map_count这个值调整内存权限,否则启动可能报错
        securityContext:
          privileged: true          #赋予权限能执行系统命令
      containers:
      - name: sonarqube
        image: "sonarqube"
        ports:
        - containerPort: 9000
        env:
        - name: SONARQUBE_JDBC_USERNAME
          value: "postgresadmin"        #引用 PostgreSQL 配置中设置的用户名
        - name: SONARQUBE_JDBC_PASSWORD
          value: "admin123"           #引用 PostgreSQL 配置中设置的密码
        - name: SONARQUBE_JDBC_URL
          value: "jdbc:postgresql://10.254.79.130:5432/postgresdb"   #指定 PostgreSQL 在 Kubernetes 中的地址
        livenessProbe:
          httpGet:
            path: /sessions/new
            port: 9000
          initialDelaySeconds: 60
          periodSeconds: 30
        readinessProbe:
          httpGet:
            path: /sessions/new
            port: 9000
          initialDelaySeconds: 60
          periodSeconds: 30
          failureThreshold: 6
        resources:
          limits:
            cpu: 2000m
            memory: 2048Mi
          requests:
            cpu: 1000m
            memory: 1024Mi
        volumeMounts:
        - mountPath: /opt/sonarqube/conf
          name: sonarqube
          subPath: conf
        - mountPath: /opt/sonarqube/data
          name: sonarqube
          subPath: data
        - mountPath: /opt/sonarqube/extensions
          name: sonarqube
          subPath: extensions
      volumes:
      - name: sonarqube
        emptyDir: {} 

3. 插件安装

由于国内的网络环境原因,无法直接在sonar web 页面的插件市场进行安装,必须手动将插件下载放置到对应目录下,此处采用重新制作镜像的方式植入,部分插件。包含sonar-findbugs-plugin-3.10.0.jar等。

Dockerfile

FROM sonarqube
ADD *.jar /opt/sonarqube/extensions/plugins/
RUN chmod +x /opt/sonarqube/extensions/plugins/*

4. 整合sonar和maven

4.1 setting.xml中做如下配置, pluginGroups在settings标签下:

<pluginGroups>
    <pluginGroup>org.sonarsource.scanner.maven</pluginGroup>
</pluginGroups>

4.2 profile中加入sonar的环境配置:

<profile>
    <id>sonar</id>
    <activation>
        <activeByDefault>true</activeByDefault>
    </activation>
    <properties>
        <sonar.host.url>
			<!-- sonar服务地址 -->
            http://10.20.250.23:31821
        </sonar.host.url>
        <sonar.login>admin</sonar.login>
        <sonar.password>admin</sonar.password>
        <sonar.inclusions>**/*.java,**/*.xml</sonar.inclusions>
    </properties>
</profile>

4.3 激活sonar环境配置

<activeProfiles>
    <activeProfile>sonar</activeProfile>
</activeProfiles>

4.4 执行代码检查命令

mvn clean install sonar:sonar

4.5 登录sonar web查看代码检查情况

image-20201117141756400