git repo代碼部署策略及工具


一般在項目或者產品開發流程中,先是開發人員在本地做好開發及測試,其中可能包含很多用於測試用的目錄以及源代碼文件,在部署前往往會有一個build過程。web項目最終build產生出優化生產環境下減少http請求的bundle js,已經有了sprite image外加css代碼的適合生產部署的系統。在部署的時候,很多文件可能並不想部署到服務器上去。如何處理?

一個可行的策略及步驟如下:

1. 使用.gitattributes文件中的export-ignore來指定哪些文件將不被打包到部署包;

2. 使用git archive命令將master或者integration等branch的內容打包: git archive intergation -o release.zip

3. 將上述release.zip文件解壓后即可在生產系統中部署。

以上3個步驟已經是可以工作了。但是可能還是有待改進。比如我們releaes的package也希望在一個repo中做好版本控制,也就是希望放到repo中。同時,我們可能還有一個本地最后測試production release的需求。你當然可以另外建一個repo和目錄來專門存放這個包,並且搭建對應的本地生產測試環境。但筆者建議更進一步,也就是使用同一個repo,但是又不希望看到開發repo中太多的歷史信息。那么可以繼續下面幾個步驟:

4. 創建一個deliverable的branch,專門用於保存git archive產生的發布包,並用於本地生產測試。 

git checkout --orphan deliverable // 創建deliverable的orphan branch,該分支上將保存所有release包
git rm -rf . // 由於orphan分支創建后所有index的內容都將自動包內含integration分支的內容,我們需要全部刪除

5. 將archive integration生成的發布包解壓后放到deliverable分支的根目錄中。這時deliverable就僅僅包含了干凈的發布包文件目錄。

6. 使用該發布包繼續測試是否work,確認ok后,直接git a . git commit即可。

7. 以后有新的版本發布的話,重復第2.步到第6步即可。

 

使用git archive命令可以很好地拉取git repo中的一個snapshot,同時在.gitattributes文件中指定歸檔策略,將一些不必要的文件不放在部署服務器上。

# used to remove files from deployment using `git archive`
# git files
.gitattributes export-ignore
.gitignore export-ignore
# drush files
build.make export-ignore
patches.txt export-ignore
# zen 5.x files
sites/all/themes/*/sass export-ignore
sites/all/themes/*/sass-extensions export-ignore
sites/all/themes/*/images-source export-ignore
sites/all/themes/*/fonts export-ignore
sites/all/themes/*/config.rb export-ignore
sites/all/themes/*/STARTERKIT export-ignore

最后執行以下命令生成對應的包

git archive master | bzip2 -9 > latest.tar.bz2

 類似gh-pages方法部署

https://coderwall.com/p/-bcoua/how-to-create-gh-pages-branch

https://stackoverflow.com/questions/19980631/what-is-git-checkout-orphan-used-for

https://stackoverflow.com/questions/4750520/git-branch-gh-pages

https://help.github.com/articles/configuring-a-publishing-source-for-github-pages/

https://gist.github.com/chrisjacob/833223


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM