Git是什么自不必說。Git和gitlab安裝和實踐在后邊的倆篇中會寫。本篇僅重點寫Git自動部署。
Git同樣有Hooks,可以用于各種需求。可以控制提交commit名稱,可以控制代碼規范,也當然包含以下要介紹的自動部署,也不僅包含這些。
Git自動部署簡單的思想是這樣:
在服務端創建空版本庫
在客戶端創建真實版本庫,關聯遠程版本庫
在服務端部署代碼
在服務端版本庫中修改新增鉤子程序
服務端創建空版本庫?
?[yu@argcv-com?~]$?cd?[yu@argcv-com?~]$?mkdir?repo[yu@argcv-com?~]$?cd?repo/[yu@argcv-com?repo]$?git?init?--bare?test.gitInitialized?empty?Git?repository?in?/home/yu/repo/test.git/[yu@argcv-com?repo]$
2.?在客戶端創建真實版本庫,關聯遠程版本庫
[yu@argcv-com?~]$?cd?/tmp/ [yu@argcv-com?tmp]$?mkdir?client [yu@argcv-com?tmp]$?cd?client/ [yu@argcv-com?client]$?git?init Initialized?empty?Git?repository?in?/tmp/client/.git/ [yu@argcv-com?client]$?git?remote?add?origin?yu@localhost:repo/test.git [yu@argcv-com?client]$?touch?README.md [yu@argcv-com?client]$?git?add?. [yu@argcv-com?client]$?git?commit?-m?"init" [master?(root-commit)?00da9a9]?init1?file?changed,?0?insertions(+),?0?deletions(-)create?mode?100644?README.md [yu@argcv-com?client]$?git?push?origin?--all? Counting?objects:?3,?done. Writing?objects:?100%?(3/3),?210?bytes,?done. Total?3?(delta?0),?reused?0?(delta?0) To?yu@localhost:repo/test.git*?[new?branch]??????master?->?master [yu@argcv-com?client]$
3.在服務端部署代碼
[yu@argcv-com?~]$?cd?/tmp/ [yu@argcv-com?tmp]$?mkdir?deploy [yu@argcv-com?tmp]$?cd?deploy/ [yu@argcv-com?deploy]$?git?clone?~/repo/test.git?. Cloning?into?'.'... done.
4.在服務端版本庫中修改新增鉤子程序
[yu@argcv-com?repo]$?pwd /home/yu/repo [yu@argcv-com?repo]$?cd?test.git/ [yu@argcv-com?test.git]$?cd?hooks/ [yu@argcv-com?hooks]$?vi?post-receive [yu@argcv-com?hooks]$?cat?post-receive #!/bin/sh unset?GIT_DIR NowPath=`pwd` echo?"now?path?is?:"$NowPath DeployPath="/tmp/deploy" echo?"deploy?path?is?:"$DeployPath cd?$DeployPath echo?"cd?deploy?path" #git?add?.?-A?&&?git?stash?#?remove?local?changes? git?pull?origin?master?#?pull?data?from?master #?the?follow?line?is?also?ok: #?git?add?.?&&?git?fetch?origin?&&?git?reset?--hard?origin/master echo?"deploy?done" cd?$NowPath echo?"fine" #?---?Finished exit?0 [yu@argcv-com?hooks]$?chmod?+x?post-receive [yu@argcv-com?hooks]$
5.測試
[yu@argcv-com?client]$?touch?newfile [yu@argcv-com?client]$?git?add?. [yu@argcv-com?client]$?git?commit?-m?"add?new?file" [master?68efdcf]?add?new?file1?file?changed,?0?insertions(+),?0?deletions(-)create?mode?100644?newfile [yu@argcv-com?client]$?git?push?origin Counting?objects:?4,?done. Delta?compression?using?up?to?2?threads. Compressing?objects:?100%?(2/2),?done. Writing?objects:?100%?(3/3),?384?bytes,?done. Total?3?(delta?0),?reused?0?(delta?0) remote:?now?path?is?:/home/yu/repo/test.git remote:?deploy?path?is?:/tmp/deploy remote:?cd?deploy?path remote:?From?/home/yu/repo/test remote:????6627165..5ac80ec??master?????->?origin/master remote:?Updating?6627165..5ac80ec remote:?Fast-forward remote:??newfile?|?0 remote:??1?file?changed,?0?insertions(+),?0?deletions(-) remote:??create?mode?100644?newfile remote:?deploy?done remote:?fine To?yu@localhost:repo/test.gitb3a32f2..5ac80ec??master?->?master [yu@argcv-com?client]$
轉載于:https://blog.51cto.com/rhino/1843183