判断Git是否有新的提交
公司要搭建CI,有这样一个需求:判断Git是否有新的提交,如果有的话拉取代码构建,如果没有不构建,Jenkins的搭建这里就不赘述了,主要讲一下判断这里。
Jenkins需要安装插件Conditional BuildStep,地址https://wiki.jenkins-ci.org/display/JENKINS/Conditional+BuildStep+Plugin
配置如下:
执行脚本:
#!/bin/bash if [ ! $GIT_PREVIOUS_SUCCESSFUL_COMMIT ];then echo "GIT_PREVIOUS_SUCCESSFUL_COMMIT is not exists." exit 0 else echo "GIT_COMMIT=[$GIT_COMMIT],GIT_PREVIOUS_SUCCESSFUL_COMMIT=[$GIT_PREVIOUS_SUCCESSFUL_COMMIT]" if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ];then echo "GIT_COMMIT is equals to GIT_PREVIOUS_SUCCESSFUL_COMMIT,skip build." exit -1 else echo "GIT_COMMIT is not equals to GIT_PREVIOUS_SUCCESSFUL_COMMIT" sh build_using_xctool.sh//这里是有代码更新的条件下需要执行的代码,我这里是跑一个脚本 exit 0 fi fi
判断的条件是本地的Git_Commit跟远端进行对比,不一致就代表有新的代码提交,执行操作,否则exit