如何构建 git 轮询构建机器人?

发布于 2024-11-30 19:35:54 字数 426 浏览 2 评论 0原文

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

目前,它看起来像这样:

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

How can a little cron, bash + make based script (e.g. much smaller and less robust than, for example, Hudson) build-bot poll a git repo and detect if it should build now - e.g. if in its periodic pull from the remote git repo, it has retrieved new code?

Currently, it looks like this:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

傲世九天 2024-12-07 19:35:54

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

git rev-parse <branch_name>

将检索分支中最新提交的 sha1。将命令的输出与存储的输出进行比较,并在其发生更改时:

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

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

这是脚本的一个版本,仅在 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

You can record the tip of your branch to build between polls, and build whenever the the tip changes–that is, when the branch has been changed.

git rev-parse <branch_name>

Will retrieve the sha1 of the latest commit in the branch. Compare the output of the command with the stored output, and when it changes:

  1. update the stored sha1
  2. perform the build

This lets you target specific branches and only build when that branch changes. Otherwise, if you want to build whenever any branch changes you can just check that the output of git fetch is empty (when there are no updates, git fetch returns nothing).

Here's a version of your script that only builds when master changes (so that changes to experimental branches don't trigger new builds of master if it hasn't changed):

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
┼── 2024-12-07 19:35:54

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

If you have control over the remote repo, you might consider doing it via hooks instead of polling. That way your script only gets called when there is something new to build.

荒芜了季节 2024-12-07 19:35:54

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

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

If nothing was fetched, then "get fetch" will output no lines, so just check for zero filesize on build_log.txt:

git fetch > build_log.txt 2>&1
if [ -s build_log.txt ]
then
   # build
fi
我不是你的备胎 2024-12-07 19:35:54

您还可以定位特定分支,而无需维护最新的头或使用临时文件:

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

You can also target a specific branch without maintaining the most recent head or using a temporary file:

if [[ $(git fetch 2>&1 | grep master) ]]; then 
  # build 
fi
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文