簡體   English   中英

Git:只從post-receive hook部署一個目錄

[英]Git: Deploy only a directory from post-receive hook

按照本文的說明,我有一個post-receive鈎子,目前讀取:

#!/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

我將提供給--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

通常,您可以使用git存儲庫中的一個或多個文件替換paths部分。 這些根據它們放置在倉庫中的方式指定為相對路徑。 所以如果你的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鈎子中執行類似的操作:

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

所以我們首先將Build目錄簽出到WT目錄中。 然后,我們cdWT目錄,從東西復制出來Build使用目錄cp -r Build/* . . 代表當前目錄,這是WT目錄我們只是cd成。 完成后,我們刪除現在空的Build目錄。

因此, Build目錄中的所有內容現在都被檢出到WT目錄中,而沒有Build目錄本身。 當然,您必須分別用工作樹和git存儲庫的實際路徑替換WTREPO

希望有所幫助

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM