如何构建 git 轮询构建机器人?
一个基于 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以记录分支的提示以在轮询之间进行构建,并在提示发生变化时进行构建 - 即当分支发生更改时。
将检索分支中最新提交的 sha1。将命令的输出与存储的输出进行比较,并在其发生更改时:
这使您可以定位特定分支,并且仅在该分支发生更改时进行构建。否则,如果您想每当任何分支更改时都构建,您只需检查
git fetch
的输出是否为空(当没有更新时,git fetch< /code> 不返回任何内容)。
这是脚本的一个版本,仅在 master 更改时才构建(这样,如果实验分支未更改,则对实验分支的更改不会触发 master 的新构建):
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.
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:
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):
如果您可以控制远程存储库,则可以考虑通过 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.
如果未获取任何内容,则“get fetch”将不输出任何行,因此只需检查 build_log.txt 上的文件大小是否为零:
If nothing was fetched, then "get fetch" will output no lines, so just check for zero filesize on build_log.txt:
您还可以定位特定分支,而无需维护最新的头或使用临时文件:
You can also target a specific branch without maintaining the most recent head or using a temporary file: