Git:只从post-receive hook部署一个目录

时间:2014-01-09 23:33:58

标签: git deployment

按照this article的说明,我有一个后接收钩子,目前读取:

#!/bin/sh
git --work-tree=/home/user/example.com --git-dir=/home/user/example.com.git checkout -f

这很有效 - 所有文件和目录都已部署完毕。但是,我只想在example.com.git中的Build文件夹中部署文件(顺便说一下,我使用Hammer。)因此,并非所有的开发文件都可用且可在实时服务器。

问题:为了只检查/部署Build目录中的内容,我可以更改/添加上述Git命令?

更新 在评论中讨论后,我得到了以下内容,这在我的案例中有用。

#!/bin/sh
git --work-tree=/home/user/example.com --git-dir=/home/user/example.com.git checkout -f master -- Build/
cd /home/user/example.com
cp -rRp Build/. .
rm -rf Build

2 个答案:

答案 0 :(得分:5)

我会将提供给--work-tree--git-dir的值分别缩写为WTREPO,这样一切都可以在没有水平滚动条的情况下适用。

一般表格应为:

#!/bin/sh
git --work-tree=WT --git-dir=REPO checkout -f -- paths

假设在实时服务器上部署的WT目录中,您只想签出存储库中的Build文件夹。这变成了

#!/bin/sh
git --work-tree=WT --git-dir=REPO checkout -f -- Build

通常,您可以将paths部分替换为git存储库中的一个或多个文件。这些根据它们放置在仓库中的方式指定为相对路径。所以如果你的git repo看起来像这样:

FolderOne
|- FolderTwo
|  |- FileOne
|- FolderThree

FolderFour
|- FileFive

您想要结帐FileOne和所有FolderFour,您应该这样做:

git --work-tree=WT --git-dir=REPO checkout -f -- FolderOne/FolderTwo/FileOne FolderFour

<强>更新

自从我处理Git钩子以来已经有一段时间了,但我认为除了stdin和argv这样的细节之外,你可以把任何东西放进去。

因此,要检查Build文件夹中的所有内容,而不是Build文件夹本身,您可以在post-receive hook中执行类似的操作:

git --work-tree=WT --git-dir=REPO checkout -f -- Build/
cd WT
cp -r Build/* .
rmdir Build

因此我们首先将Build目录签出到WT目录中。然后我们cd进入WT目录,使用Buildcp -r Build/* .目录中复制所有内容。 .代表当前目录,即我们WT进入的cd目录。完成后,我们删除现在为空的Build目录。

因此,Build目录中的所有内容现在都已签出到WT目录中,而没有Build目录本身。当然,您必须分别用工作树和git存储库的实际路径替换WTREPO

希望有所帮助

答案 1 :(得分:1)

yanhan的解决方案对我不起作用..这是我的方法...

#!/bin/bash
TARGET="/htdocs/my-project/public"
TEMP="/htdocs/my-project/tmp"
PUBLIC="/htdocs/my-project/tmp/public"
GIT_DIR="/htdocs/my-project/test.git"
BRANCH="master"

while read oldrev newrev ref
do
    if [[ $ref = refs/heads/$BRANCH ]];
    then
        # create temp directory
        mkdir $TEMP
        # check out the master branch
        git --work-tree=$TEMP --git-dir=$GIT_DIR checkout -f 
        # remove current target files and directory
        rm -rf $TARGET
        # move contents of the chosen folder to the target 
        mv $PUBLIC $TARGET
        # delete the tmp directory since not needed anymore..
        rm -rf $TEMP
    fi
done