【发布时间】:2015-07-07 06:57:28
【问题描述】:
我们有两个 git repo 分支(team1 和 team2)。我想通过 apache 在两个主机名或 URL 下为分支提供服务。目前主 URL 为 team1 服务。我希望 apache 也为 team2 服务,以便他们也可以在服务器中检查他们的更新。请告诉我如何配置它,以便对 team2 的更新反映在不同的 URL 中。
【问题讨论】:
标签: git apache git-branch
我们有两个 git repo 分支(team1 和 team2)。我想通过 apache 在两个主机名或 URL 下为分支提供服务。目前主 URL 为 team1 服务。我希望 apache 也为 team2 服务,以便他们也可以在服务器中检查他们的更新。请告诉我如何配置它,以便对 team2 的更新反映在不同的 URL 中。
【问题讨论】:
标签: git apache git-branch
至少在单分支部署中对我有用的一种方法:
以下示例使用 Linux 环境。
将服务器上的裸 Git 存储库创建到非 www 可访问的位置(例如 /home/someuser/repo.git):
$ cd /home/someuser
$ mkdir repo.git
$ cd repo.git
$ git --bare init
将服务器的存储库添加到您的开发存储库的远程:
$ git remote add production someuser@server:~/repo.git
然后创建一个post-receive 钩子(到home/someuser/repo.git/hooks)来检查被推送的分支:
#!/bin/bash
# post-receive
# Read hook input params.
while read oldrev newrev refname; do
# Get "simple" branch name from push data.
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "team1" == "$branch" ]; then
# team1 branch pushed.
elif [ "team2" == "$branch" ]; then
# team2 branch pushed.
fi
done
然后在 if-else 中,您需要获取推送的内容并将它们部署到分配有不同虚拟主机的特定目录。假设team1 位于/var/www/team1/html 中,team2 位于/var/www/team2/html 中。
部署发生在更改服务器的 repo 的 git 工作树位置并在那里获取更改。以下内容应该让两个团队都能做到这一点:
#!/bin/bash
# post-receive
# Destinations to deploy repo contents to.
TEAM1_DEST="/var/www/team1/html"
TEAM2_DEST="/var/www/team2/html"
# Read hook input params.
while read oldrev newrev refname; do
# Get "simple" branch name from push data.
branch=$(git rev-parse --symbolic --abbrev-ref $refname)
if [ "team1" == "$branch" ]; then
# team1 branch pushed.
GIT_WORK_TREE=$TEAM1_DEST git checkout -f team1
elif [ "team2" == "$branch" ]; then
# team2 branch pushed.
GIT_WORK_TREE=$TEAM2_DEST git checkout -f team2
fi
done
记得为post-receive钩子设置chmod +x。
我已经多次使用这种方法来部署推送网站。 注意:上面的钩子没有经过测试,但说明了原理。
【讨论】:
您需要在 Apache 中创建一个新的 VirtualHost 配置。这定义了将确定 apache 在何处搜索 web 文件的主机名。
【讨论】: