使用阿里开源的项目 p3c 对gitlab上项目进行代码(java)检测,新版gitlab改动较大,相关文档较少,本文整理官档和网友脚本希望对大家有个引导作用,避免踩坑。
2.1 jar包 编译过程省略,我们得到jar包:
p3c-pmd/target/p3c-pmd-2.0.1-jar-with-dependencies.jar2.2 xml规则文件
[root@localhost p3c]# ll p3c-pmd/target/classes/rulesets/java/ 总用量 68 -rw-r--r-- 1 root root 4492 6月 23 14:25 ali-comment.xml -rw-r--r-- 1 root root 10851 6月 23 14:25 ali-concurrent.xml -rw-r--r-- 1 root root 1445 6月 23 14:25 ali-constant.xml -rw-r--r-- 1 root root 3745 6月 23 14:25 ali-exception.xml -rw-r--r-- 1 root root 2658 6月 23 14:25 ali-flowcontrol.xml -rw-r--r-- 1 root root 5159 6月 23 14:25 ali-naming.xml -rw-r--r-- 1 root root 4514 6月 23 14:25 ali-oop.xml -rw-r--r-- 1 root root 636 6月 23 14:25 ali-orm.xml -rw-r--r-- 1 root root 4573 6月 23 14:25 ali-other.xml -rw-r--r-- 1 root root 4130 6月 23 14:25 ali-set.xml2.3 文件目录 我们需要把准备好的jar包和xml规则文件放到我们规划好的目录下,这里我们放到gitlab服务器"/opt/git-hooks/"目录。
目录结构如下:
[root@localhost ~]# tree /opt/git-hooks/ /opt/git-hooks/ ├── p3c-pmd-2.0.1-jar-with-dependencies.jar └── rulesets ├── ali-comment.xml ├── ali-concurrent.xml ├── ali-constant.xml ├── ali-exception.xml ├── ali-flowcontrol.xml ├── ali-naming.xml ├── ali-oop.xml ├── ali-orm.xml ├── ali-other.xml └── ali-set.xml 1 directory, 11 files在配置gitlab之前我们需要了解下gitlab仓库数据存储位置:
[root@localhost ~]# ll /var/opt/gitlab/git-data/repositories 总用量 8 drwxr-sr-x 3 git root 4096 12月 26 2019 +gitaly drwxr-s--- 106 git root 4096 6月 29 10:05 @hashed由于项目目录名称都是经过hash的,我们并不能直接从目录名称看出来仓库及组和项目对应的目录。所以当我们需要对某个项目进行添加规则时需要找到项目对应的hash目录。具体的项目hash目录可通过gitlab管理员进入项目管理后台,点开对应的项目即可看到Gitaly relative path,此处即为对应项目hash目录名称。 进入到项目目录下 /var/opt/gitlab/git-data/repositories/“Gitaly relative path” 创建目录:custom_hooks/pre-receive.d 创建脚本文件:custom_hooks/pre-receive.d/pre-receive
// pre-receive文件注意: 01.文件名无后缀 02.文件属组(chown -R git:root custom_hooks ) 03.可执行(chmod 777 custom_hooks/pre-receive.d/pre-receive )
pre-receive脚本内容如下:
#!/bin/bash export JAVA_HOME=/opt/jdk1.8.0_111 export JRE_HOME=$JAVA_HOME/jre export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CLASSPATH export PATH=$JAVA_HOME/bin:$JRE_HOME/bin:$PATH REJECT=0 while read oldrev newrev refname; do if [ "$oldrev" = "0000000000000000000000000000000000000000" ];then oldrev="${newrev}^" fi committer=`git log -1 $newrev --pretty=