Skip to content

完成代码扫描

SonarQube配置信息

服务器url为http://sonar.hisense.com

Sonarqube Token为d658a43a0b87780550bf191a6ce04c239c059020

其中token也可自行替换为自己的token,细节这里不再涉及,如下图

代码扫描

一、java maven (后端)

执行扫描步骤合并在maven打包当中

在mvn install步骤后添加:

xml
mvn -s nexus-setting.xml  sonar:sonar -Dsonar.projectName=${projectName} -Dmaven.test.skip=true -Dsonar.projectKey=${projectKey} -Dsonar.host.url=http://sonar.hisense.com -Dsonar.login=${sonar_login_token}

参数含义:

参数名参数含义示例
sonar.projectName显示名称,通常为项目名称-模块名称pangea-system
sonar.projectKey项目唯一标识,通常为com.hisense.公司简称-部门简称.项目名称.模块名称com.hisense.jtit-yw.pangea.pangea-system
sonar.host.url服务器urlhttp://sonar.hisense.com
sonar.loginSonarqube Tokend658a43a0b87780550bf191a6ce04c239c059020

sonar.projectKey命名规范

sonar平台用于区分不同项目,使用的是sonar.projectKey关键字,其命名规范com.hisense.<内外部公司/部门/团队英文简称>.<项目英文简称>.<模块英文简称>

  • 集团IT-办公共享系统部:com.hisense.jtit-bggx.<项目英文简称>.<模块英文简称>
  • 集团IT-数据管理与应用部:com.hisense.jtit-dsj.<项目英文简称>.<模块英文简称>
  • 集团IT-服务运营管理部:com.hisense.jtit-fwyyglb.<项目英文简称>.<模块英文简称>
  • 集团IT-国内营销解决方案部:com.hisense.jtit-yx.<项目英文简称>.<模块英文简称>
  • 集团IT-供应链解决方案部:com.hisense.jtit-gyl.<项目英文简称>.<模块英文简称>
  • 集团IT-研发解决方案部:com.hisense.jtit-yf.<项目英文简称>.<模块英文简称>
  • 集团IT-财务解决方案部:com.hisense.jtit-cw.<项目英文简称>.<模块英文简称>
  • 集团IT-流程推进与规划管理部:com.hisense.jtit-xmgl.<项目英文简称>.<模块英文简称>
  • 集团IT-光通信解决方案部:com.hisense.jtit-gtx.<项目英文简称>.<模块英文简称>
  • 集团IT-基础架构与云服务管理部:com.hisense.jtit-yw.<项目英文简称>.<模块英文简称>
  • 中国区营销:com.hisense.zgq.<项目英文简称>.<模块英文简称>
  • 国际营销:com.hisense.gjyx.<项目英文简称>.<模块英文简称>
  • 完善中...

ocp pipeline示例

apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  labels:
    app: test-sonar
  name: test-sonar-pipeline
spec:
  nodeSelector: {}
  output: {}
  postCommit: {}
  resources: {}
  runPolicy: Serial
  source:
    type: None
  strategy:
    jenkinsPipelineStrategy:
      jenkinsfile: |-
        try {
          timeout(time: 60, unit: 'MINUTES') {
            // git info
            def git_url = "http://gitlab.hisense.com/caojun1/springboot-demo.git"
            def git_branch = "master"
            def git_credentialsId = "d8b67189-7843-47b2-b569-469ffa14dc07"
 
            // sonar info
            def sonar_url = 'http://sonar.hisense.com'
            def sonar_login_token = "d658a43a0b87780550bf191a6ce04c239c059020"
 
            node {
              stage("Initialize") {
                //project = env.PROJECT_NAME
                project = "cj-test"
              }
            }
 
            node("maven") {
              stage("Checkout") {
                git branch: "${git_branch}", credentialsId: "${git_credentialsId}", url: "${git_url}"
              }
 
              stage("Sonar") {
                echo "run sonarqube"
                sh "mvn clean install sonar:sonar -Dsonar.projectKey=${project} -Dsonar.host.url=${sonar_url} -Dsonar.login=${sonar_login_token}"
              }
            }
          }
        } catch (err) {
          echo "in catch block"
          echo "Caught: ${err}"
          currentBuild.result = 'FAILURE'
          throw err
        }
    type: JenkinsPipeline
  triggers: []

二、js (前端)

在ocp的pipeline中,基于sonar-scanner的镜像完成扫描

方案

在项目中的pipeline中,clone仓库代码,并基于sonar基础镜像 创建用于扫描的镜像。并进行扫描

步骤

1.在ocp,创建Build,一个source为dockerfile的build。dockerfile的内容:基于sonar-scanner镜像,在镜像内执行命令

xml
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  annotations:
    openshift.io/generated-by: OpenShiftNewBuild
  labels:
    app: sonar-scanner
  name: sonar-scanner
spec:
  failedBuildsHistoryLimit: 5
  nodeSelector: null
  output:
    to:
      kind: ImageStreamTag
      name: 'sonar-scanner:latest'
  postCommit: {}
  resources: {}
  runPolicy: Serial
  source:
    dockerfile: |
      FROM registry.hisense.com/sonar-scanner:v1
      COPY . /opt/code
      RUN sonar-scanner -Dsonar.host.url=http://sonar.hisense.com -Dsonar.projectKey=com.hisense.jtit-yw.pangea.front-end -Dsonar.projectName=pangea-react-web -Dsonar.sources=. -Dsonar.login=d658a43a0b87780550bf191a6ce04c239c059020 -Dsonar.projectBaseDir=.
    type: Dockerfile
  strategy:
    dockerStrategy:
      forcePull: true
      from:
        kind: DockerImage
        name: 'registry.hisense.com/sonar-scanner:v1'
      noCache: true
    type: Docker
  successfulBuildsHistoryLimit: 5
  triggers: []

2.在ocp,创建Pipeline,使用oc build触发上述的build定义

xml
apiVersion: build.openshift.io/v1
kind: BuildConfig
metadata:
  labels:
    app: sonar-front-end
  name: sonar-pipeline-front-end
spec:
  nodeSelector: {}
  output: {}
  postCommit: {}
  resources: {}
  runPolicy: Serial
  source:
    type: None
  strategy:
    jenkinsPipelineStrategy:
      jenkinsfile: |-
        try {
          timeout(time: 60, unit: 'MINUTES') {
            // git info
            def git_url = "http://gitlab.hisense.com/bdmd/fundppm/pangea-react-web.git"
            def git_branch = "master"
            def git_credentialsId = "609a65f2-ed41-45b3-9211-12434fceed13"
 
            node("nodejs") {
              stage("Checkout") {
                git branch: "${git_branch}", credentialsId: "${git_credentialsId}", url: "${git_url}"
              }
 
              stage("Sonar") {
                echo "run sonarqube"
                sh "oc start-build sonar-scanner --follow  --from-dir=."
              }
            }
          }
        } catch (err) {
          echo "in catch block"
          echo "Caught: ${err}"
          currentBuild.result = 'FAILURE'
          throw err
        }
    type: JenkinsPipeline
  triggers: []

注意事项

sonar扫描完成,请提供sonar.projectKey杜艺配置sonar平台(sonar.hisense.com)的权限,查看扫描结果。

其中参数说明以及填写规范均在上方中涉及,参考即可