2011-10-09 179 views
5

我剛開始編寫Web應用程序。提交後使用git鉤子

我使用GIT進行版本控制,並且我在同一臺計算機上安裝了git和web服務器。

應用有3個環境:開發,測試和生產

我想使用git鉤後每次提交更新開發,測試或生產應用。

這是什麼最佳實踐?

我需要的是這樣的:

  1. 當我犯,開發必須自動更新
  2. 時提交信息中包含「測試:」
  3. 在消息的前面 - 開發和測試必須更新。
  4. 當提交消息在消息生成前包含「生產:」時,必須更新開發和測試。

謝謝!

+4

只是一個建議(因爲它感覺更一致和更容易):使用一個_branch_開發,一個用於測試,一個用於生產。每次你推動「測試」一個鉤子應該更新你的測試環境(等等)。 – KingCrunch

+0

感謝您分享您的建議 – Irakli

回答

1

我剛寫了一個鉤/小bash腳本來解決這個問題

#!/bin/bash 

if git log --pretty=format:%s -1 | grep -q "^test: " 
then 
    #action/update dev/test 
elif git log --pretty=format:%s -1 | grep -q "^production: " 
then 
    #action/update dev/test/production 
else 
    #action/update dev 
fi 

這是我的第一個bash腳本等等..請幫助改善這種:)

+0

在這裏使用'git checkout -f'會很有用。 – jackyalcine

1

你可以寫一個post-commit鉤子將解析使用類似git log -1 --format=%B提交信息和不喜歡git push dev適當的行動等

如果你談論的是推你提交到遠程中央回購和該回購必須執行此操作,那麼您必須以類似的方式使用post-receive鉤子。請注意,提交鉤子在您提交的客戶端倉庫上運行。

即便如此,使用您在消息中說的環境推送到環境並不是一個合適的工作流程。你可以有不同的分支,在那裏你可以挑選你的提交等。你可以設置鉤子,當你推到test分支時,測試環境被更新,等等。

+0

謝謝!你已經幫了我 – Irakli

+0

你已經幫了我,但並不是完整的答案。請檢查我的新帖子關於這個問題。再次感謝! – Irakli

2

基於伊拉克利想法,這裏是我的工作作爲後收到我的回購...

#!/bin/bash 

MESSAGE=$(git log -1 HEAD --pretty=format:%s) 

if [[ "$MESSAGE" == *\[staging\]* ]]; 
then 
    #action/update staging 
    # another method not being used... 
    # GIT_WORK_TREE=/path/to/working/site/ git checkout -q -f staging 
    echo "NOTE: Beginning Auto-Push to Staging Server... " 
    `git push staging` 
    echo "======================================================== 
======== Done! Pushed to STAGING.com ============= 
======== Thanks Captain. Keep up the good work! ======== 
========================================================" 
elif [[ "$MESSAGE" == *\[production\]* ]]; 
then 
    #action/update production 
    echo "NOTE: Beginning Auto-Push to Production Server... " 
    # `git push production` 
    echo "======================================================== 
======== Done!!! Pushed to Production.com ======= 
======== Test immediately for any errors! ========= 
========================================================" 
fi 

注:

爲了讓'git push staging'工作,你需要在該工作樹上有一個.git/hooks/post-reveive鉤子。我使用this code,除了我在底部添加'umask 002 & & git reset --hard'。

我也有一個denyrecive添加到工作樹的.git/config文件:

[receive] 
    denycurrentbranch = ignore 

注2:

請注意,這個設置並不適合每一個人......只爲小( ish)網站快速&髒更新是好的。