是否可以使用git push部署一个网站?我有一种预感,它与使用git挂钩来执行git重置有关——在服务器端很难,但我该如何完成这一点呢?
当前回答
我最终创建了我自己的基本部署工具,它会自动从repo - https://github.com/jesalg/SlimJim -下拉新的更新,基本上它会监听github的接收后钩子,并使用代理来触发更新脚本。
其他回答
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钩子一起是最终的解决方案。
部署场景
在我们的场景中,我们将代码存储在github/bitbucket上,并希望将其部署到活动服务器上。 在这种情况下,以下组合对我们来说是有效的(这是这里高度好评的答案的混合):
复制你的。git目录到你的web服务器 在本地复制git远程添加live ssh://user@host:port/文件夹 在远程:git配置receive.denyCurrentBranch忽略 在远程:nano .git/hooks/post-receive并添加以下内容: #!/bin/sh GIT_WORK_TREE=/var/www/vhosts/example.org 远程:chmod +x .git/hooks/post-receive 现在你可以用git push live来推送
笔记
此解决方案适用于较旧的git版本(使用1.7和1.9进行测试) 你需要确保先推送到github/bitbucket,这样你就会有一个一致的实时回购 如果你的。git文件夹在根目录中,请确保通过添加.htaccess (source)来隐藏它: RedirectMatch 404 /\..*$
使用下面的更新后文件:
复制你的。git目录到你的web服务器 在你的本地副本上,修改你的.git/config文件,并添加你的web服务器作为远程服务器: (远程“生产”) Url = username@webserver:/path/to/htdocs/.git 在服务器上,用下面的文件替换。git/hooks/post-update 添加对文件的执行访问(同样是在服务器上): Chmod +x .git/hooks/post-update 现在,只要本地推送到你的web服务器,它就会自动更新工作副本: Git推送生产
#!/bin/sh
#
# This hook does two things:
#
# 1. update the "info" files that allow the list of references to be
# queries over dumb transports such as http
#
# 2. if this repository looks like it is a non-bare repository, and
# the checked-out branch is pushed to, then update the working copy.
# This makes "push" function somewhat similarly to darcs and bzr.
#
# To enable this hook, make this file executable by "chmod +x post-update".
git-update-server-info
is_bare=$(git-config --get --bool core.bare)
if [ -z "$is_bare" ]
then
# for compatibility's sake, guess
git_dir_full=$(cd $GIT_DIR; pwd)
case $git_dir_full in */.git) is_bare=false;; *) is_bare=true;; esac
fi
update_wc() {
ref=$1
echo "Push to checked out branch $ref" >&2
if [ ! -f $GIT_DIR/logs/HEAD ]
then
echo "E:push to non-bare repository requires a HEAD reflog" >&2
exit 1
fi
if (cd $GIT_WORK_TREE; git-diff-files -q --exit-code >/dev/null)
then
wc_dirty=0
else
echo "W:unstaged changes found in working copy" >&2
wc_dirty=1
desc="working copy"
fi
if git diff-index --cached HEAD@{1} >/dev/null
then
index_dirty=0
else
echo "W:uncommitted, staged changes found" >&2
index_dirty=1
if [ -n "$desc" ]
then
desc="$desc and index"
else
desc="index"
fi
fi
if [ "$wc_dirty" -ne 0 -o "$index_dirty" -ne 0 ]
then
new=$(git rev-parse HEAD)
echo "W:stashing dirty $desc - see git-stash(1)" >&2
( trap 'echo trapped $$; git symbolic-ref HEAD "'"$ref"'"' 2 3 13 15 ERR EXIT
git-update-ref --no-deref HEAD HEAD@{1}
cd $GIT_WORK_TREE
git stash save "dirty $desc before update to $new";
git-symbolic-ref HEAD "$ref"
)
fi
# eye candy - show the WC updates :)
echo "Updating working copy" >&2
(cd $GIT_WORK_TREE
git-diff-index -R --name-status HEAD >&2
git-reset --hard HEAD)
}
if [ "$is_bare" = "false" ]
then
active_branch=`git-symbolic-ref HEAD`
export GIT_DIR=$(cd $GIT_DIR; pwd)
GIT_WORK_TREE=${GIT_WORK_TREE-..}
for ref
do
if [ "$ref" = "$active_branch" ]
then
update_wc $ref
fi
done
fi
我使用toroid.org的以下解决方案,它有一个更简单的钩子脚本。
服务器端:
$ mkdir website.git && cd website.git
$ git init --bare
Initialized empty Git repository in /home/ams/website.git/
并在服务器上安装钩子:
$ mkdir /var/www/www.example.org
$ cat > hooks/post-receive
#!/bin/sh
GIT_WORK_TREE=/var/www/www.example.org git checkout -f
GIT_WORK_TREE=/var/www/www git clean -f -d # clean directory from removed files
$ chmod +x hooks/post-receive
在您的客户端:
$ mkdir website && cd website
$ git init
Initialized empty Git repository in /home/ams/website/.git/
$ echo 'Hello, world!' > index.html
$ git add index.html
$ git commit -q -m "The humble beginnings of my web site."
$ git remote add web ssh://server.example.org/home/ams/website.git
$ git push web +master:refs/heads/master
然后要发布,只需输入
$ git push web
网站上有完整的描述:http://toroid.org/ams/git-website-howto
对于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中找到原始/master的位置,以及如何更改它?
- Bower: ENOGIT Git未安装或不在PATH中
- Bitbucket上的Git:总是要求密码,即使上传了我的公共SSH密钥
- Git别名-多个命令和参数
- 如何添加一个“打开git-bash这里…”上下文菜单到windows资源管理器?
- 是否可以在Git中只提取一个文件?
- 当我做“git diff”的时候,我怎么能得到一个并排的diff ?
- 在git中如何将提交移动到暂存区?
- 如何缩小。git文件夹
- 如何在本地删除分支?
- 找到包含特定提交的合并提交
- Windows上Git文件的权限
- 如何从一个枝头摘到另一个枝头
- 如何获得在两次Git提交之间更改的所有文件的列表?
- 什么是跟踪分支?