【Jenkins】集成PHPCS校验脚本
脚本如下
#!/bin/bash CMD_PATH=`dirname "$0"` LOG_DIR=${CMD_PATH}/log/ if [ ! -d ${LOG_DIR} ];then mkdir -p ${LOG_DIR} fi LOG_FILE=${LOG_DIR}/$(date +%Y%m%d).log ${CMD_PATH}/phpcs-check | tee -a ${LOG_FILE} STATUS=${PIPESTATUS[0]} echo "Code check status: " ${STATUS} exit ${STATUS}
其中phpcs-check脚本如下,需要替换
- YOUR_PATH_TO_JENKINS_WORKSPACE为你的jenkins项目workspace的路径
-
phpcs-check-ignore-list为提供phpcs忽略的文件列表的文件
-
base-master为作为差异文件对比的分支
#!/bin/bash CMD_PATH=`dirname "$0"` IGNORE_LIST_FILE="${CMD_PATH}/phpcs-check-ignore-list" PROJECT='/YOUR_PATH_TO_JENKINS_WORKSPACE/' BASE_BRANCH='base-master' FILE_LIST=`cd ${PROJECT};git diff --name-only --diff-filter=ACMR ${BASE_BRANCH} | grep '.php'` IGNORE_DIRS=`cat ${IGNORE_LIST_FILE} | grep -v ".php$"` date "+%Y-%m-%d %H:%M:%S" echo "Checking PHP Lint..." for FILE in $FILE_LIST do IGNORE=0 if [ `cat "$IGNORE_LIST_FILE" | grep "$FILE" -c` == 0 ] then for DIR in $IGNORE_DIRS do if [ `echo $FILE | grep "${DIR}" -c` != 0 ];then IGNORE=1 fi done if [ "${IGNORE}" == "0" ]; then php -l -d display_errors=0 $PROJECT/$FILE if [ $? != 0 ] then echo "Fix the error before commit." exit 1 fi FILES="$FILES $PROJECT/$FILE" fi else IGNORE=1 fi if [ "${IGNORE}" == "1" ];then echo "Ignore code check: "$FILE fi done if [ "$FILES" != "" ] then echo "Running Code Sniffer..." /usr/bin/phpcs --standard=${CMD_PATH}/phpcs_ruleset.xml --extensions=php --encoding=utf-8 -n -p $FILES if [ $? != 0 ] then echo "Fix the error before commit." exit 1 fi fi exit 0
上面脚本中的/usr/bin/phpcs和phpcs_ruleset.xml需要事先准备好,参看【PHP】代码规范检查工具PHPCS
标签: PHP
还没有人抢沙发呢~