需求分析
在jenkins中沒有找到構建前插件,每次構建時間很長,希望可以實現判斷代碼是否更新,如果沒更細則停止構建步驟。
實現步驟
在構建時執行shell命令,而jenkins提供的的環境變量可以實現此判斷 https://wiki.jenkins.io/display/JENKINS/Conditional+BuildStep+Plugin
GIT_COMMIT The commit hash being checked out. GIT_PREVIOUS_COMMIT The hash of the commit last built on this branch, if any. GIT_PREVIOUS_SUCCESSFUL_COMMIT The hash of the commit last successfully built on this branch, if any.
GIT_COMMIT 當前拉取版本的commit id
GIT_PREVIOUS_COMMIT 最后在此分支上構建的 commit id
GIT_PREVIOUS_SUCCESSFUL_COMMIT 最后在此分支上成功構建的 commit id號
#!/bin/bash if [ $GIT_PREVIOUS_SUCCESSFUL_COMMIT == $GIT_COMMIT ];then echo "no change,skip build" exit 0 else echo "git pull commmit id not equals to current commit id trigger build" fi
注意,不能使用-eq 只能使用==
提交新版本后,構建提示如下:
$ git show|head -1 commit 27617e680d2e6bf00062700623792aef63926edd
在jenkins中執行構建