对于项目代码质量管理,在目前的微服务/模块化/快迭代敏捷开发中若是仅依赖IDE简单检查和人为的codereview对于大量代码很不适合,不单单依靠开发人员的编码规范编码及注意程序健壮性,同时须要一些工具来帮助咱们提早预防和强制检测规范。html
Sonarqube 是一款代码分析检测工具,将其与devops结合,例如集成到gitlab ci/cd或jenkins中实现部署自动代码检查,及时发现并处理bug,最大限度的将bug和不规范扼杀在编码阶段,其内部集成不少分析工具,好比pmd-cpd、checkstyle、findbugs、Jenkins,从七个方面帮咱们来源码质量管理。此文章安装最新版SonarQube-7.9.1,此版本不支持自定义数据库MySQL,jdk须要安装高版本11。java
检测代码中注释的程度:源码注释过多或者太少都很差,影响程序的可读可理解性。python
开发人员把代码push到SCM(如gitlab)->jenkins构建定义好的job,而后经过jenkins 插件(sonar scanner)分析源码->jenkins/gitlab-ci 中的scanner客户端把分析报告发到sonarqube servermysql
# 下载 wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zip # 解压 /opt/sonarqube/ unzip /opt/sonarqube/bin/[OS]/sonar.sh console # 登陆宿主机http://localhost:9000 (admin/admin)
# 宿主机requirementes sysctl -w vm.max_map_count=262144 sysctl -w fs.file-max=65536 ulimit -n 65536 ulimit -u 4096 cat >> /etc/sysctl.conf << EOF vm.max_map_count=262144 fs.file-max=65536 EOF # sonarqube不能用root用户执行 useradd sonarqube echo "sonarqubepwd" | passwd --stdin sonarqube # 检查系统 [root@devops-sonarqube ~]# grep SECCOMP /boot/config-$(uname -r) CONFIG_HAVE_ARCH_SECCOMP_FILTER=y CONFIG_SECCOMP_FILTER=y CONFIG_SECCOMP=y cat > /etc/security/limits.d/99-sonarqube.conf <<EOF sonarqube - nofile 65536 sonarqube - nproc 4096 EOF # sonarqube es须要安装安装jdk11 yum -y install java-11-openjdk.x86_64 # 7.9最新版本不支持mysql,数据库支持MSSQL/Oracle/PostgreSQL # 安装PostgreSQL # 建立sonarqube用户,受权用户create, update, and delete权限 # 若是想自定义数据库名称,不用pulic,则须要搜索路径修改 yum install -y https://download.postgresql.org/pub/repos/yum/9.6/redhat/rhel-7-x86_64/pgdg-centos96-9.6-3.noarch.rpm yum install -y postgresql96-server postgresql96-contrib /usr/pgsql-9.6/bin/postgresql96-setup initdb systemctl start postgresql-9.6 systemctl enable postgresql-9.6 su - postgres psql create user sonarqube with password 'sonarqube'; create database sonarqube owner sonarqube; grant all on database sonarqube to sonarqube; \q # 查看postgresql监听 vi /var/lib/pgsql/9.6/data/postgresql.conf # 配置白名单 vi /var/lib/pgsql/9.6/data/pg_hba.conf host all all 127.0.0.1/32 md5 #重启服务 systemctl restart postgresql-9.6 ss -tan | grep 5432 # 建立库/用户,并受权 psql -h 127.0.0.1 -p 5432 -U postgres # 下载软件包 cd /opt && wget https://binaries.sonarsource.com/Distribution/sonarqube/sonarqube-7.9.1.zip ln -sv sonarqube-7.9.1 sonarqube chown sonarqube.sonarqube sonarqube/* -R # 切换到系统sonarqube用户开始安装 su - sonarqube # 设置数据库访问,编辑$SONARQUBE-HOME/conf/sonar.properties sonar.jdbc.username=sonarqube sonar.jdbc.password=sonarqube # 注意为127.0.0.1 sonar.jdbc.url=jdbc:postgresql://127.0.0.1/sonarqube # 配置ES存储路径,编辑SONARQUBE-HOME/conf/sonar.properties sonar.path.data=/var/sonarqube/data sonar.path.temp=/var/sonarqube/temp # 配置web server,编辑SONARQUBE-HOME/conf/sonar.properties sonar.web.host=192.0.0.1 sonar.web.port=80 sonar.web.context=/sonarqube # web服务器性能调优 $SONARQUBE-HOME/conf/sonar.properties sonar.web.javaOpts=-server $SONARQUBE-HOME/conf/wrapper.conf wrapper.java.command=/path/to/my/jdk/bin/java # 执行启动脚本 Start: $SONAR_HOME/bin/linux-x86-64/sonar.sh start Graceful shutdown: $SONAR_HOME/bin/linux-x86-64/sonar.sh stop Hard stop: $SONAR_HOME/bin/linux-x86-64/sonar.sh force-stop # 插件安装 1.Marketplace方式安装(Administration > Marketplace) 2.手动安装(将下载好的插件上传至服务器目录:$SONARQUBE_HOME/extensions/plugins,重启sonarqube服务)
docker pull sonarqube docker run -d --name sonarqube -p 9000:9000 sonarqube # 分析mvn项目 # On Linux: $ mvn sonar:sonar # With boot2docker: $ mvn sonar:sonar -Dsonar.host.url=http://$(boot2docker ip):9000 # docker主机系统要求 sysctl -w vm.max_map_count=262144 sysctl -w fs.file-max=65536 ulimit -n 65536 ulimit -u 4096
因为gitlab项目较多,共用了gitlab-runner,所以在gitlab-runner安装sonner-scanner便可,可通用对构建的项目进行扫描linux
# 下载安装 cd /opt && wget https://binaries.sonarsource.com/Distribution/sonar-scanner-cli/sonar-scanner-cli-4.0.0.1744-linux.zip # 添加进PATH mv sonar-scanner-4.0.0.1744-linux sonar-scanner cat > /etc/profile.d/sonar-scanner.sh <<EOF export PATH=$PATH:/opt/sonar-scanner/bin EOF source /etc/profile.d/sonar-scanner.sh [root@common-runner ~]# sonar-scanner -h INFO: INFO: usage: sonar-scanner [options] INFO: INFO: Options: INFO: -D,--define <arg> Define property INFO: -h,--help Display help information INFO: -v,--version Display version information INFO: -X,--debug Produce execution debug output
Create new project->Provide a token->git
stages: - sonarqube_scan - deploy_src - install_dependency - restart_server - check_server variables: RUNNER_BASE_DIR: "/home/gitlab-runner/builds/QFafxxxEq/0/devops/" BASE_DIR: "/go2cloud_api/" job sonarqube_scan_job: stage: sonarqube_scan # 注意,此用户为gitlab-runner执行,指定/.为此项目目录 script: - sonar-scanner -Dsonar.projectKey=go2cloud_api_test -Dsonar.sources=/. -Dsonar.host.url=http://43.xxx.xxx.xxx:9110 -Dsonar.login=a393276xxxxxxxxxxxxxxxxxxx03004a714 tags: - 51common-runner only: - go2cloud-platform-test when: always job deploy_src_job: stage: deploy_src script: - scp -r ${RUNNER_BASE_DIR}${BASE_DIR}* root@172.16.100.5:/project${BASE_DIR} tags: - 51common-runner only: - go2cloud-platform-test when: always
提交代码测试:web
查看运行jobsql
查看sonarqube项目docker
shell
查看详情
能够利用插件集成,也能够将sonar-scanner 安装在jenkins服务区上面,每次进行工具扫描。
sonar-scanner安装和gitlab-runner上安装同样,详见:2.2.1 sonar-scanner安装
能够两种方式集成:直接在构建的时候执行扫描命令分析报告,插件形式集成。
在构建的时候利用安装好的sonar-scanner命令集成
# 配置PATH export PATH=/data/apps/miniconda3/bin:/data/apps/miniconda3/condabin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/data/apps/miniconda3/bin:/data/apps/software/sonar-scanner/bin:/root/bin # 指定jenkins的workspace目录 BASE_DIR=/root/.jenkins/workspace/ # 指定项目名称,此job的名称 PROJECT=go2cloud_api_prod_job # 指定conda中项目的虚拟环境 PROJECT_ENV=go2cloud-api-prod-env # 切换python环境 source activate ${PROJECT_ENV} $(which python) -m pip install mock nose coverage # 更新python环境 echo "++++++++++++++++++++++++++++++更新Python环境+++++++++++++++++++++++++++++++++++++++" if [ -f ${BASE_DIR}${PROJECT}/requirements.txt ];then $(which python) -m pip install -r ${BASE_DIR}${PROJECT}/requirements.txt && echo 0 || echo 0 fi # 代码检查/单元测试/代码测试覆盖率 echo "+++++++++++++++++++++++++++++++代码检查+++++++++++++++++++++++++++++++++++++++" cd ${BASE_DIR} # 生成pylint.xml $(which pylint) -f parseable --disable=C0103,E0401,C0302 $(find ${PROJECT}/* -name *.py) >${BASE_DIR}${PROJECT}-pylint.xml || echo 0 #echo "+++++++++++++++++++++++++++++++单元测试+++++++++++++++++++++++++++++++++++++++" # 生成nosetests.xml #$(which nosetests) --with-xunit --all-modules --traverse-namespace --with-coverage --cover-package=go2cloud-api-deploy-prod --cover-inclusive || echo 0 #$(which nosetests) --with-xunit --all-modules --traverse-namespace --with-coverage --py3where=${PROJECT} --cover-package=${PROJECT} --cover-inclusive || echo 0 #echo "+++++++++++++++++++++++++++++++代码覆盖率+++++++++++++++++++++++++++++++++++++++" # 生成coverage.xml # -m coverage xml --include=${PROJECT}* || echo 0 # sonarqube 代码扫描 sonar-scanner \ -Dsonar.projectKey=go2cloud_api_prod \ -Dsonar.sources=${BASE_DIR}${PROJECT}/. \ -Dsonar.host.url=http://xxx.xxx.xxx.xxx:9100 \ -Dsonar.login=2194d90xxxxxxxxxxxxxxxxxxxxxxxxbec7f69
运行项目查看
查看sonarqube项目
# 若是在构建中未指定将扫描报告发送给server端地址,须要在客户端中配置,在安装好scanner的conf目录下修改:sonar-scanner.properties 服务端的地址 sonar.host.url=http://xxx.xxx.xxx.xxx:9100
jenkins服务器安装scanner
须要在sonarqube server配置jenkins的api token,用来jenkins将报告发送给sonarqube server
jenkins利用sonarqube的token建立凭据
根据扫描的程序语言填写对应的analysis properties,在此填写项目相关信息。
若是使用pipeline,能够参考声明式示例
pipeline { agent any stages { stage('SonarQube analysis 1') { steps { sh 'mvn clean package sonar:sonar' } } stage("Quality Gate 1") { steps { waitForQualityGate abortPipeline: true } } stage('SonarQube analysis 2') { steps { sh 'gradle sonarqube' } } stage("Quality Gate 2") { steps { waitForQualityGate abortPipeline: true } } } }
在jenkins上面配置了projectname,在sonarqube上就不用配置项目
Gitlab-runner输出限制,gitlab界面显示不全ob's log exceeded limit of 4194304 bytes.
https://gitlab.com/gitlab-org/gitlab-runner/blob/master/docs/configuration/advanced-configuration.md#the-runners-section