2013-04-11 89 views
4

當我將git推送到我的bitbucket存儲庫時,我正在嘗試設置自動部署。我有一個php部署腳本,我利用了this blog,但是當腳本運行時,它會記錄它只是從以前的提交中更新。使用Git,Bitbucket和PHP自動部署

這裏是一個例子。假設我登錄到我的服務器並輸入git pull。服務器將更新最新的變化,並可以說該提交的哈希爲001.但是,如果我做了幾個提交,我們可以稱它們爲002,003和004,我的腳本應該每次都運行,假設我在每次提交之後將這些更改推送到bitbucket 。腳本運行,但每次它將保持從001的更改。只有當我登錄到我的服務器並輸入git pull時,服務器是否會更新爲004.您是否知道會導致此問題?

// Make sure we're in the right directory 
exec('cd '.$this->_directory, $output); 
$this->log('Changing working directory... '.implode(' ', $output)); 

// Discard any changes to tracked files since our last deploy 
exec('git reset --hard HEAD', $output); 
$this->log('Reseting repository... '.implode(' ', $output)); 

// Update the local repository 
exec('git pull '.$this->_remote.' '.$this->_branch, $output); 
$this->log('Pulling in changes... '.implode(' ', $output)); 

// Secure the .git directory 
exec('chmod -R og-rx .git'); 
$this->log('Securing .git directory... '); 

if (is_callable($this->post_deploy)) 
{ 
call_user_func($this->post_deploy, $this->_data); 
} 

$this->log('Deployment successful.'); 
+0

您是否也有子模塊?如果是這樣,不要忘記'git submodule update'。 – halfer 2013-04-11 14:26:15

+0

我真的不知道子模塊是什麼。我剛開始使用bitbucket和git。 – 2013-04-11 14:30:22

+0

子模塊是存儲庫中指向標記的存儲庫。我建議儘可能不要使用它。如果您有多個使用相同插件/庫的內部應用程序,則可能會出現這種情況。他們爲此發明了作曲者;) – 2013-04-11 14:43:28

回答

5

我會推薦的是發佈不是基於最新版本在你的主人,而是最新的標籤。

/home/my-user/my-application/1.0.12/www
/home/my-user/my-application/1.0.13/www

等這提供回退功能。您可以製作一個PHP腳本,通過SSH連接到您的服務器,並基於該標記創建一個新的克隆。如果你使用Composer,你可以使用它來執行命令。如果沒有,你可以用makefile來完成。

編輯:我忘了提及你如何實際鏈接它。

你有一個符號鏈接
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.12/www

當你的整個部署腳本沒有錯誤完成後,你的符號鏈接切換到:
/home/my-user/my-application/www -> /home/my-user/my-application/1.0.13/www

您的應用程序現在住無需停機。

0

問題是文件權限。

我正在關注該博客帖子的相同鏈接。我發現php和nginx進程使用的'www-data'用戶沒有對存儲庫代碼的寫入權限。它甚至不能使用git。

爲了驗證這個嘗試在服務器上以'www-user'做'git pull'。你可以通過'sudo su www-data'切換到它。你會發現它甚至不認爲這是一個有效的git倉庫,並且無法無誤地運行你的'deploy.php'腳本。

您需要爲您的存儲庫設置適當的權限,以便www數據可以訪問它。

或者你改變了整個方法。按照這篇文章http://toroid.org/ams/git-website-howto,我覺得是一個比上述更好的方法。我結束了使用這種方法。

0

您想在您推送的遠程設置post-update鉤子。

在默認情況下,git不會檢出您推送到遠程的任何數據。這就是爲什麼默認的git配置拒絕推送到已簽出的分支,因爲當您推送到結賬分支時,簽出的文件不再與HEAD保持同步。

雖然,當遠程接收到分支的推送時,您可以對post-update掛鉤作出響應。要看看會發生什麼,你應該先用一些記錄開始:

echo "post update `date`: $*" >> /home/ingo/test/githooks.out 

當我推到了新科,比如我得到的線

post update Mi 24. Jun 13:01:14 CEST 2015: refs/heads/new 

,其中$*包含我推到了分支機構。

有了這個,你可以編寫一個簡單的腳本來檢出該分支。我更喜歡檢查分離的元首,因爲將多個模塊庫(無子模塊)的工作組合成部署版本更爲簡單。請參閱我的answer以瞭解如何在git存儲庫外簽出代碼並使用該代碼進行源代碼部署。

例如,你可以做

for b in $* 
do B=`basename $b` 
if [ "$B" = "publish" ] # react on changes to the publish branch 
then git --work-tree "PATH TO THE PUBLISHED WORK TREE" checkout publish 
do_some_extra_work # to change permissions or inform your team leader 
fi done 

當然你也可以做結帳步驟也即上拉做

if [ "`cat HEAD`" = "ref: refs/heads/master" ] 
# only do that checkout if the repository is in a sane state 
then git merge new # I assume you push to new 
fi 

很酷的事情是,你會看到的輸出您在執行推送的終端中的遠程連接鉤:

# git push remote master:new 
Counting objects: 3, done. 
Delta compression using up to 4 threads. 
Compressing objects: 100% (2/2), done. 
Writing objects: 100% (3/3), 296 bytes | 0 bytes/s, done. 
Total 3 (delta 0), reused 0 (delta 0) 
remote: Updating f81ba5b..a99a710 
remote: Fast-forward 
remote: a.txt | 2 ++ 
remote: 1 file changed, 2 insertions(+) 
To ../remote 
    db48da1..a99a710 master -> new