1 钉钉配置机器人

打开群组,点击群设置,机器人,添加机器人

点击自定义

Jenkins集成钉钉_安装插件

Jenkins集成钉钉_java_02

这个webhook就是需要的

https://oapi.dingtalk.com/robot/send?access_token=XXX

2 Jenkins集成钉钉

2.1 安装插件

安装插件
Environment File 
Dingding JSON Pusher
DingTalk

2.2 配置钉钉机器人

安装完插件,找了半天没找到配置,以前在系统管理,系统配置里面,现在在系统管理最下面未分类

Jenkins集成钉钉_系统管理_03

Jenkins集成钉钉_系统管理_04

Jenkins集成钉钉_java_05

可以点击进行测试

Jenkins集成钉钉_安装插件_06

Jenkins集成钉钉_java_07

3 为项目添加通知

3.1 启用钉钉机器人

- 构建的项目:${JOB_BASE_NAME}
- Sonar质检报告:http://xxx

Jenkins集成钉钉_安装插件_08

3.2 查看通知

Jenkins集成钉钉_java_09

4 为项目通知质检结果

4.1 编写质量检测脚本

配置阿里云镜像
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

#安装jq
基本找不到jq包,因为jq在epel-release里面
yum -y install epel-release
yum install -y jq

#配置获取sonar扫描结果脚本
vim /app/dingding.sh
# 调用查询异常状态接口函数,该接口是在SonarQube平台直接爬的
function issues() {
   # 如果sonar启用了强制认证,可以通过添加 -u username:password 进行校验
  curl -u admin:admin -sH "Content-Length:0" -X GET "http://192.168.137.130:9000/api/issues/search?resolved=false&facets=severities%2Ctypes&componentKeys=$1"
}

# 类型
for i in {0..2}
do
  a=`issues $1 | jq '.facets[1].values['${i}'].val'`
  if [ ${a} = '"BUG"' ]
  then
    BUG=$(echo "BUG:`issues $1 | jq '.facets[1].values['${i}'].count'`个")
    N_BUG=`issues $1 | jq '.facets[1].values['${i}'].count'`
  fi
  if [ ${a} = '"VULNERABILITY"' ]
  then
    VULNERABILITY=$(echo "漏洞:`issues $1 | jq '.facets[1].values['${i}'].count'`个")
    N_VULNERABILITY=`issues $1 | jq '.facets[1].values['${i}'].count'`
  fi
  if [ ${a} = '"CODE_SMELL"' ]
  then
    CODE_SMELL=$(echo "异味:`issues $1 | jq '.facets[1].values['${i}'].count'`个")
  fi
done
# 严重程度
for i in {0..4}
do
  b=`issues $1 | jq '.facets[0].values['${i}'].val'`
  if [ ${b} = '"MINOR"' ]
  then
     MINOR=$(echo "次要:`issues $1 | jq '.facets[0].values['${i}'].count'`个")
  fi
  if [ ${b} = '"MAJOR"' ]
  then
     MAJOR=$(echo "主要:`issues $1 | jq '.facets[0].values['${i}'].count'`个")
  fi
  if [ ${b} = '"CRITICAL"' ]
  then
    CRITICAL=$(echo "严重:`issues $1 | jq '.facets[0].values['${i}'].count'`个")
  fi
  if [ ${b} = '"INFO"' ]
  then
    INFO=$(echo "提示:`issues $1 | jq '.facets[0].values['${i}'].count'`个")
  fi
  if [ ${b} = '"BLOCKER"' ]
  then
  BLOCKER=$(echo "阻断:`issues $1 | jq '.facets[0].values['${i}'].count'`个")
  fi
done
# 如何将脚本的结果定义为系统变量,供钉钉调用 https://www.it1352.com/1470666.html
echo "BUG=${BUG}" > ${WORKSPACE}/build
echo "VULNERABILITY=${VULNERABILITY}" >> ${WORKSPACE}/build
echo "CODE_SMELL=${CODE_SMELL}" >> ${WORKSPACE}/build
echo "CRITICAL=${CRITICAL}" >> ${WORKSPACE}/build

chmod a+x /app/dingding.sh

4.2 配置JOB任务

1 钉钉机器人

- 构建的项目:${JOB_BASE_NAME}
- Sonar质检报告:
- ${BUG}
- ${VULNERABILITY}
- ${CODE_SMELL}
- ${CRITICAL}

Jenkins集成钉钉_系统管理_10

2 源码管理

Jenkins集成钉钉_安装插件_11

3 Sonar推送配置

# must be unique in a given SonarQube instance
sonar.projectKey=$JOB_BASE_NAME
# this is the name and version displayed in the SonarQube UI. Was mandatory prior to SonarQube 6.1.
sonar.projectName=$JOB_BASE_NAME
sonar.projectVersion=1.0
# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows.
# This property is optional if sonar.modules is set.
sonar.sources=.
sonar.exclusions=**/test/**,**/target/**
sonar.java.source=1.8
sonar.java.target=1.8
# Encoding of the source code. Default is default system encoding
sonar.sourceEncoding=UTF-8

Jenkins集成钉钉_安装插件_12

4 执行shell

如果不加等待时间,本次sonar结果没统计出来,脚本获取的是上一次的执行结果

Jenkins集成钉钉_java_13

5 传入变量配置

Jenkins集成钉钉_系统管理_14

6 钉钉结果

Jenkins集成钉钉_java_15