【问题标题】:How to build a git polling build bot?如何构建一个 git polling build bot?
【发布时间】:2011-08-23 19:32:35
【问题描述】:

一个基于 cron、bash + make 的脚本(例如,比 Hudson 更小、更不健壮)如何构建机器人轮询 git 存储库并检测它是否应该现在构建 - 例如如果在从远程 git repo 定期拉取时,它已检索到新代码?

目前,它看起来像这样:

git fetch  > build_log.txt 2>&1
if [ $? -eq 0 ]
then
  echo "Fetch from git done";
  git merge FETCH_HEAD >> build_log.txt 2>&1 ;
  if [ $? -eq 0 ]
  then
    echo "Merge via git done"; ...
    # builds unconditionally at the moment
  fi
fi

【问题讨论】:

    标签: git shell continuous-integration build-automation


    【解决方案1】:

    您可以记录分支的提示以在轮询之间构建,并在提示更改时构建,即分支发生更改时。

    git rev-parse <branch_name>
    

    将检索分支中最新提交的 sha1。将命令的输出与存储的输出进行比较,以及何时发生变化:

    1. 更新存储的sha1
    2. 执行构建

    这使您可以定位特定的分支,并且仅在该分支发生更改时进行构建。否则,如果您想在任何分支更改时构建,您只需检查git fetch 的输出是否为空(当没有更新时,git fetch 不返回任何内容)。

    这是您的脚本的一个版本,它仅在 master 更改时构建(因此对实验分支的更改不会触发 master 的新构建,如果它没有更改):

    if [ ! -f prev_head ]; # initialize if this is the 1st poll
    then
      git rev-parse master > prev_head
    fi
    # fetch & merge, then inspect head
    git fetch  > build_log.txt 2>&1
    if [ $? -eq 0 ]
    then
      echo "Fetch from git done";
      git merge FETCH_HEAD >> build_log.txt 2>&1 ;
      git rev-parse master > latest_head
      if ! diff latest_head prev_head > /dev/null ;
      then
        echo "Merge via git done"; ...
        cat latest_head > prev_head # update stored HEAD
        # there has been a change, build
      fi
    fi
    

    【讨论】:

    • 太好了!谢谢!
    • 但是你有一个错字:应该是 if ! diff latest_head prev_head - 不是“最新的”。
    • 感谢@Experience 的捕获——答案已编辑以修复变量名称。
    【解决方案2】:

    如果您可以控制远程存储库,您可以考虑通过hooks 进行操作,而不是轮询。这样你的脚本只有在有新东西要构建时才会被调用。

    【讨论】:

      【解决方案3】:

      如果没有获取任何内容,则“get fetch”将不输出任何行,因此只需检查 build_log.txt 上的零文件大小:

      git fetch > build_log.txt 2>&1
      if [ -s build_log.txt ]
      then
         # build
      fi
      

      【讨论】:

      • 这个解决方案对于单分支远程的特殊情况是最简洁的。但是,如果远程上的另一个分支发生更改,这将不会满足您的要求。 Fetch 将输出任何可用的新分支,而不是跟踪特定分支。
      【解决方案4】:

      您还可以针对特定分支而不维护最新的头或使用临时文件:

      if [[ $(git fetch 2>&1 | grep master) ]]; then 
        # build 
      fi
      

      【讨论】:

        猜你喜欢
        • 2015-03-02
        • 1970-01-01
        • 2015-01-14
        • 2020-09-12
        • 1970-01-01
        • 2023-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多