1. k8s安装sonarqube
    1.1 pv创建
#cat pv.yaml
kind: PersistentVolume
apiVersion: v1
metadata:
  name: gluster-heketi
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/sonarqube"



---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gluster-heketi
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi


---
kind: PersistentVolume
apiVersion: v1
metadata:
  name: gluster-heketi2
  labels:
    type: local
spec:
  storageClassName: manual
  capacity:
    storage: 10Gi
  accessModes:
    - ReadWriteOnce
  hostPath:
    path: "/home/gpsql"



---
apiVersion: v1
kind: PersistentVolumeClaim
metadata:
  name: gluster-heketi2
spec:
  storageClassName: manual
  accessModes:
    - ReadWriteOnce
  resources:
    requests:
      storage: 10Gi

1.2 pgsql创建

#cat pgsql.yaml
apiVersion: apps/v1
kind: Deployment
metadata:
  name: postgres-sonar
  labels:
    app: postgres-sonar
spec:
  replicas: 1
  selector:
    matchLabels:
      app: postgres-sonar
  template:
    metadata:
      labels:
        app: postgres-sonar
    spec:
      containers:
      - name: postgres-sonar
        image: postgres:11.4
        imagePullPolicy: IfNotPresent
        ports:
        - containerPort: 5432
        env:
        - name: POSTGRES_DB
          value: "sonarDB"
        - name: POSTGRES_USER
          value: "sonarUser"
        - name: POSTGRES_PASSWORD
          value: "123456"
        volumeMounts:
          - name: data
            mountPath: /var/lib/postgresql/data
      volumes:
        - name: data
          persistentVolumeClaim:
            claimName: gluster-heketi


---
apiVersion: v1
kind: Service
metadata:
  name: postgres-sonar
  labels:
    app: postgres-sonar
spec:
  clusterIP: None
  ports:
  - port: 5432
    protocol: TCP
    targetPort: 5432
  selector:
    app: postgres-sonar


1.3 sonarqube创建

#cat sonarqube.yaml
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:
      - name: init-sysctl
        image: busybox
        imagePullPolicy: IfNotPresent
        command: ["sysctl", "-w", "vm.max_map_count=262144"]
        securityContext:
          privileged: true
      containers:
      - name: sonarqube
        image: sonarqube:lts
        ports:
        - containerPort: 9000
        env:
        - name: SONARQUBE_JDBC_USERNAME
          value: "sonarUser"
        - name: SONARQUBE_JDBC_PASSWORD
          value: "123456"
        - name: SONARQUBE_JDBC_URL
          value: "jdbc:postgresql://postgres-sonar:5432/sonarDB"
        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: data
          subPath: conf
        - mountPath: /opt/sonarqube/data
          name: data
          subPath: data
        - mountPath: /opt/sonarqube/extensions
          name: data
          subPath: extensions
      volumes:
      - name: data
        persistentVolumeClaim:
          claimName: gluster-heketi2


---
apiVersion: v1
kind: Service
metadata:
  name: sonarqube
  labels:
    app: sonarqube
spec:
  type: NodePort
  ports:
    - name: sonarqube
      port: 9000
      targetPort: 9000
      nodePort: 30003
      protocol: TCP
  selector:
    app: sonarqube

  1. 访问地址:ip+:30003 默认密码admin/admin
    在这里插入图片描述

  2. jenkins 配置:
    3.1 安装插件:sonarQube Scanner
    3.2服务端配置
    在这里插入图片描述
    在这里插入图片描述

3.3 配置客户端:

在这里插入图片描述

  1. jenkins配置pipeline
    4.1 sonarqube创建项目
    在这里插入图片描述
    在这里插入图片描述

4.2 go代码检测

pipeline {
	agent any
	stages{
        stage('git代码拉取') { 
            
        steps {
	     script{
          git credentialsId: '05995c1e-0589-4c8c-a404-96ca175ef457', url: 'http://192.168.10.100/gitlab-instance-e417c306/golangtest.git'
	     }
      
    }
    }
    stage('SonarQube analysis'){
             
                steps {
                    script {
                  //这里填写,在系统管理中配置的sonar服务器名称
                  
                    scannerHome = tool 'sonarqube'
                    }
            withSonarQubeEnv('sonarqube') {
                            //           sh "printenv"
                            // sh "sonar-scanner -h"
                    // scannerHome = tool 'sonarqube'
                    // sh "${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=test -Dsonar.projectName=test   -Dsonar.sources=. -Dsonar.exclusions=*.go,*vendor* -Dsonar.tests=. -Dsonar.test.inclusions=*.go -Dsonar.test.exclusions=*vendor*" }
                      sh "pwd;ls ./ ;${scannerHome}/bin/sonar-scanner -Dsonar.projectKey=test  -Dsonar.sources=. -Dsonar.exclusions=.  -Dsonar.host.url=http://192.168.10.100:30003  -Dsonar.login=59efcab0e1c7045b3c05855b5400f4a6824fd762"}
                    //在sonar中项目的名称
                    // "-Dsonar.projectKey=test" +
                    //在sonar中项目的名称
                    // "-Dsonar.projectName=test" +
                    // "-Dsonar.sourceEncoding=UTF-8 " +
                    //项目类型
                    // "-Dsonar.language=go " +
                    //需要过滤的文件类型
                    // "-Dsonar.exclusions=**/*.go,**/vendor/** " +
                    // "sonar.tests=."
                    // " sonar.test.inclusions=**/*.go" +
                    //pom中的版本
                    // "-Dsonar.projectVersion=2021.8.0 " +
                    //需要检查代码的路径
                    // "-Dsonar.sources=master-business/src/main/java " +
                    //  "sonar.sources=." +
                    //  "sonar.test.exclusions=**/vendor/**" 
                    //需要检查代码源码路径
                    // "-Dsonar.java.binaries=**/target/classes"
             
          
    }
    }
    }
}
    

4.3 执行jenkins构建
其他代码同上

Logo

K8S/Kubernetes社区为您提供最前沿的新闻资讯和知识内容

更多推荐