使用git作为部署工具

时间:2011-06-21 18:04:34

标签: git bash deployment

我正在尝试使用git作为部署工具,所以当推送到生产分支时,我想相应地更新远程服务器。但是,当我运行第二个命令(拉动)时,它会返回一个错误,上面写着Operation must be run in a work tree.任何人都能指出我正确的方向吗?

以下是bash脚本的部分示例。

echo -e "Thank you for pushing your changes to ${project}. \nHold on while I update the required directories..."
GIT_WORK_TREE=/home/www/${project} git checkout -f
echo "Local directory updated!"

for ref in $@; do
    echo $ref
    if [ "$ref" = "refs/heads/production" ]; then
        # Before we could set the GIT directory strictly from the local environment
        # but the case might not be the same remotely. Need absolute paths.
        ssh git@myserver GIT_DIR=/home/www/${project}/.git GIT_WORK_TREE=/home/www/${project} git checkout -f production
        ssh git@myserver.com GIT_DIR=/home/www/${project}/.git GIT_WORK_TREE=/home/www/${project} git pull -f production
        echo "Production push completed"
    fi
done

编辑:

以下是复制粘贴错误:

remote: fatal: /usr/lib/git-core/git-pull cannot be used without a working tree.

2 个答案:

答案 0 :(得分:3)

正如@meagar所说,听起来远程服务器上的克隆存储库是使用--bare选项克隆的。在没有此选项的情况下再次克隆它以获得正常的“工作目录”副本,就像您在本地一样。

所以你现在应该:

  • 您的本地存储库
  • 服务器上的裸存储库
  • 服务器上的正常服务器(从裸服务器克隆)

考虑到这一点,你现在可以创建一个post-receive钩子,只要有东西被推送到裸存储库就会运行。挂钩(在服务器上)挂在裸仓库的钩子文件夹中(各种情况下都有样品)。

#!/bin/bash
while read oldrev newrev refname
do
    if [ "$refname" == "refs/heads/master" ]; then
        WORKDIR=/path/to/checked/out/repository
        export GIT_DIR=$WORKDIR/.git
        pushd $WORKDIR >/dev/null
        git pull --quiet >/dev/null
        # run some scripts in the checked out repository
        popd >/dev/null
    fi
done

此脚本专门查找对主分支的推送,但这可以轻松更改为另一个分支或完全删除。然后它切换到已检出的存储库的工作目录并执行拉取。拉完后,您可以运行任何其他有用的bash命令。

答案 1 :(得分:1)

听起来你认为“已部署”的仓库是一个裸仓库。您应该克隆您的回购而不 --bare,然后重试。