判断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
git branch | sed -n '/\* /s///p' 本地分支: master 远程跟踪分支(同步) : (detached from origin/master) 远程跟踪分支(非同步) : (detached from origin/feature-foo) 标签: (detached from v1.2.3) 一般分离头: (detached from 285f294) git status | head -1 本地分支: # On branch master 远程跟踪分支(同步) : # HEAD detached at origin/master 远程跟踪分支(非同步) : # HEAD detached at origin/feature-foo 标签: # HEAD detached at v1.2.3 一般分离头: # HEAD detached at 285f294 git describe --all 本地分支: heads/master 远程跟踪分支(同步) : heads/master ( note: 不 remotes/origin/master ) 远程跟踪分支(非同步) : remotes/origin/feature-foo 标签: v1.2.3 一般分离头: v1.0.6-5-g2393761 cat .git/HEAD : 本地分支: ref: refs/heads/master 所有其它用例: SHA对应的commit git rev-parse --abbrev-ref HEAD 本地分支: master 所有其它用例: HEAD git symbolic-ref --short HEAD 本地分支: master 所有其它用例: fatal: ref HEAD is not a symbolic ref