【问题标题】:git logs not matching when setting --work-tree with push-to-deploy post-receive hook使用 push-to-deploy post-receive hook 设置 --work-tree 时 git 日志不匹配
【发布时间】:2014-05-27 16:59:58
【问题描述】:

我通过在/home/ubuntu/push-to-deploy/test.git 处设置--bare 目录,将其用作我的远程并在--bare 中添加hooks/post-receive,来使用我的生产服务器设置推送部署,如下所示:

#!/bin/bash

while read oldrev newrev ref
do
  branch=`echo $ref | cut -d/ -f3`
  if [ "production" == "$branch" -o "master" == "$branch" ]; then

    git --work-tree=/var/www/test/ checkout -f $branch
    sudo chown -R ubuntu:www-data /var/www/test

    echo 'Changes pushed to Amazon EC2 PROD.'
  fi
done

当从我的本地主机推送到这个新的远程时,这非常有用。 post-receive 脚本按应有的方式执行,内容更新按应有的方式反映在/var/www/test 目录中。唯一的问题是我在/var/www/test 中的git log 根本不匹配我的本地主机。这是--work-tree 的正常行为吗?如果是这样,我可以做些什么来保留这个push-to-deploy 功能并且仍然将我的 git 日志和内容复制到生产目录中?

还有

当我的内容复制到生产目录 (/var/www/test) 时,所有文件所有权都被覆盖到 ubuntu:ubuntu,这使得 www-data 无法执行其操作。我在post-receive 中添加了一行,以便在每次接收后更新所有权,但还有其他方法(更好的方法)吗?

更新

确保www-data作为组保留的方法是这样设置目录的guid:

chmod -R g+s /var/www/test

这会将其设置为目录的当前组,因此如果您希望它为www-data,请确保在发出该命令之前将组设置为www-data

谢谢

【问题讨论】:

  • git log --git-dir=/home/ubuntu/push-to-deploy/test.git/var/www/test 执行时应该可以正常工作。
  • 致命:无法识别的参数:--git-dir=/home/ubuntu/push-to-deploy/test.git
  • 对不起,我的意思是git --git-dir=/home/ubuntu/push-to-deploy/test.git log
  • 另外,我是否应该在/var/www/test/.git/config 中添加一个选项以将git-dir 更改为/home/ubuntu/push-to-deploy/test.git,这样我就不必为每个这样的命令指定--git-dir 选项?
  • 请注意,简单地将引用名称与cut -d/ 分开是不太正确的:如果有refs/tags/master/v2 形式的引用,例如,cut 将提取字段 3,这将是master。在这种特殊情况下可能没问题,但通常你应该只测试"$ref" == refs/heads/master

标签: git git-post-receive git-bare


【解决方案1】:

您可以将环境变量GIT_DIR 设置为/home/ubuntu/push-to-deploy/test.git 并且:

  • 做你的git --work-tree=/var/www/test/ checkout -f $branch
  • 或在/var/www/test/ 中填写您的git log

在这两种情况下,都会考虑正确的索引。


OP sadmicrowave 确认in the comments

刚刚做了一个chmod -R g+s /var/www/test,它现在正在工作。

【讨论】:

  • 听起来在这种情况下它可以工作,但如果我的生产服务器上有多个 git repo 怎么办?
  • @sadmicrowave 那么你需要一些脚本/包装器来封装你想要运行的 git 命令,同时将GIT_DIR 设置为正确的存储库。
  • 对我的操作的第二部分有什么建议吗?
  • nvm - 刚刚做了一个chmod -R g+s /var/www/test,它现在正在工作
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-22
  • 2013-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-02
  • 2013-07-17
相关资源
最近更新 更多