是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?
当前回答
在经历了许多错误的开始和死胡同之后,多亏了这篇文章,我终于能够使用“git push remote”部署网站代码。
作者的更新后脚本只有一行长,并且他的解决方案不像其他一些解决方案那样需要.htaccess配置来隐藏Git回购。
如果您在Amazon EC2实例上部署它,则会遇到几个绊脚石;
1)如果使用sudo创建裸目标存储库,必须将repo的所有者更改为ec2-user,否则推送将失败。(尝试“chown ec2-user:ec2-user repo”)
2)如果你没有预先配置你的amazon-private-key的位置,推送会失败。可以在/etc/ssh/ssh_config中作为IdentityFile参数,也可以在~/。ssh/config使用“[主机]- HostName - IdentityFile - User”布局在这里描述…
...但是,如果在~/中配置了Host。ssh/config和不同于HostName的Git推送将失败。(这可能是Git的bug)
其他回答
You could conceivably set up a git hook that when say a commit is made to say the "stable" branch it will pull the changes and apply them to the PHP site. The big downside is you won't have much control if something goes wrong and it will add time to your testing - but you can get an idea of how much work will be involved when you merge say your trunk branch into the stable branch to know how many conflicts you may run into. It will be important to keep an eye on any files that are site specific (eg. configuration files) unless you solely intend to only run the one site.
或者,你有没有考虑过把改变推到网站上?
有关git钩子的信息,请参阅githooks文档。
Giddyup是语言无关的只是添加水的git挂钩,通过git推送自动化部署。它还允许你有自定义启动/停止钩子重新启动web服务器,加热缓存等。
https://github.com/mpalmer/giddyup
请看一些例子。
听起来你应该在你的服务器上有两个副本。一个裸拷贝,你可以推/拉,当你完成时,你会推你的改变,然后你会把这个克隆到你的web目录,并设置一个cronjob来每天从你的web目录更新git pull。
对于post-receive hook,我使用了两种解决方案:
部署方案1
#!/bin/bash
# /git-repo/hooks/post-receive - file content on server (chmod as 755 to be executed)
# DEPLOY SOLUTION 1
export GIT_DIR=/git/repo-bare.git
export GIT_BRANCH1=master
export GIT_TARGET1=/var/www/html
export GIT_BRANCH2=dev
export GIT_TARGET2=/var/www/dev
echo "GIT DIR: $GIT_DIR/"
echo "GIT TARGET1: $GIT_TARGET1/"
echo "GIT BRANCH1: $GIT_BRANCH1/"
echo "GIT TARGET2: $GIT_TARGET2/"
echo "GIT BRANCH2: $GIT_BRANCH2/"
echo ""
cd $GIT_DIR/
while read oldrev newrev refname
do
branch=$(git rev-parse --abbrev-ref $refname)
BRANCH_REGEX='^${GIT_BRANCH1}.*$'
if [[ $branch =~ $BRANCH_REGEX ]] ; then
export GIT_WORK_TREE=$GIT_TARGET1/.
echo "Checking out branch: $branch";
echo "Checking out to workdir: $GIT_WORK_TREE";
git checkout -f $branch
fi
BRANCH_REGEX='^${GIT_BRANCH2}.*$'
if [[ $branch =~ $BRANCH_REGEX ]] ; then
export GIT_WORK_TREE=$GIT_TARGET2/.
echo "Checking out branch: $branch";
echo "Checking out to workdir: $GIT_WORK_TREE";
git checkout -f $branch
fi
done
部署方案2
#!/bin/bash
# /git-repo/hooks/post-receive - file content on server (chmod as 755 to be executed)
# DEPLOY SOLUTION 2
export GIT_DIR=/git/repo-bare.git
export GIT_BRANCH1=master
export GIT_TARGET1=/var/www/html
export GIT_BRANCH2=dev
export GIT_TARGET2=/var/www/dev
export GIT_TEMP_DIR1=/tmp/deploy1
export GIT_TEMP_DIR2=/tmp/deploy2
echo "GIT DIR: $GIT_DIR/"
echo "GIT TARGET1: $GIT_TARGET1/"
echo "GIT BRANCH1: $GIT_BRANCH1/"
echo "GIT TARGET2: $GIT_TARGET2/"
echo "GIT BRANCH2: $GIT_BRANCH2/"
echo "GIT TEMP DIR1: $GIT_TEMP_DIR1/"
echo "GIT TEMP DIR2: $GIT_TEMP_DIR2/"
echo ""
cd $GIT_DIR/
while read oldrev newrev refname
do
branch=$(git rev-parse --abbrev-ref $refname)
BRANCH_REGEX='^${GIT_BRANCH1}.*$'
if [[ $branch =~ $BRANCH_REGEX ]] ; then
export GIT_WORK_TREE=$GIT_TARGET1/.
echo "Checking out branch: $branch";
echo "Checking out to workdir: $GIT_WORK_TREE";
# DEPLOY SOLUTION 2:
cd $GIT_DIR/; mkdir -p $GIT_TEMP_DIR1;
export GIT_WORK_TREE=$GIT_TEMP_DIR1/.
git checkout -f $branch
export GIT_WORK_TREE=$GIT_TARGET1/.
rsync $GIT_TEMP_DIR1/. -v -q --delete --delete-after -av $GIT_TARGET1/.
rm -rf $GIT_TEMP_DIR1
fi
BRANCH_REGEX='^${GIT_BRANCH2}.*$'
if [[ $branch =~ $BRANCH_REGEX ]] ; then
export GIT_WORK_TREE=$GIT_TARGET2/.
echo "Checking out branch: $branch";
echo "Checking out to workdir: $GIT_WORK_TREE";
# DEPLOY SOLUTION 2:
cd $GIT_DIR/; mkdir -p $GIT_TEMP_DIR2;
export GIT_WORK_TREE=$GIT_TEMP_DIR2/.
git checkout -f $branch
export GIT_WORK_TREE=$GIT_TARGET2/.
rsync $GIT_TEMP_DIR2/. -v -q --delete --delete-after -av $GIT_TARGET2/.
rm -rf $GIT_TEMP_DIR2
fi
done
这两种解决方案都基于本文中提供的早期解决方案。
请注意, 美元BRANCH_REGEX = ' ^ $ {GIT_BRANCH1}。” 过滤匹配“master”或“dev*”字符串的分支名称,如果推送的分支匹配,则部署工作树。 这使得将开发版本和主版本部署到不同的位置成为可能。
DEPLOY SOLUTION 1仅删除文件,这些文件是repo的一部分,并由提交删除。比部署方案2快。
DEPLOY SOLUTION 2的优势在于,它将从生产目录中删除添加到服务器端的任何新文件,无论这些文件是否添加到repo。它将永远是回购的干净的欺骗。比部署方案1慢。
git配置——local receive.denyCurrentBranch updateInstead
在Git 2.3中添加了一个很好的可能性:https://github.com/git/git/blob/v2.3.0/Documentation/config.txt#L2155
您在服务器存储库上设置它,如果工作树是干净的,它也会更新工作树。
在2.4中有进一步的改进,包括push-to-checkout钩子和未出生分支的处理。
示例用法:
git init server
cd server
touch a
git add .
git commit -m 0
git config --local receive.denyCurrentBranch updateInstead
cd ..
git clone server local
cd local
touch b
git add .
git commit -m 1
git push origin master:master
cd ../server
ls
输出:
a
b
这确实有以下GitHub公告中提到的缺点:
Your server will contain a .git directory containing the entire history of your project. You probably want to make extra sure that it cannot be served to users! During deploys, it will be possible for users momentarily to encounter the site in an inconsistent state, with some files at the old version and others at the new version, or even half-written files. If this is a problem for your project, push-to-deploy is probably not for you. If your project needs a "build" step, then you will have to set that up explicitly, perhaps via githooks.
但是所有这些点都超出了Git的范围,必须由外部代码来处理。因此,从这个意义上说,这和Git钩子一起是最终的解决方案。
推荐文章
- VS 2017 Git本地提交数据库。每次提交时锁定错误
- 如何在过去的一些任意提交之间注入一个提交?
- 有nginx access_log和error_log日志的STDOUT和STDERR的主进程
- 从GitHub克隆项目后拉git子模块
- GitHub上的分叉和克隆有什么区别?
- 递归地按模式添加文件
- 我如何使用notepad++(或其他)与msysgit?
- 如何将现有的解决方案从Visual Studio 2013添加到GitHub
- Git存储库中的悬垂提交和blob是什么?它们来自哪里?
- 我如何简单地从我最新的git提交创建一个补丁?
- Git显示“警告:永久添加到已知主机列表”
- 我如何检索一个回购的远程git地址?
- 如何列出提交,因为某些提交?
- 如何在不位于存储库的情况下执行Git命令?
- 为什么git在Windows下记不住我的密码