Deployment GIT


Installation

https://github.com/netkiller/deployment.git
$ git clone https://github.com/netkiller/deployment.git
$ chmod 755 -R deployment
$ export DEPLOY_HOME=~/deployment
		

臨時使用的方法

export DEPLOY_HOME=/home/user/deployment
		
$ cd deployment/
$ ln -s bin/deploy.git run
		

使用說明

		
$ ./run
Usage: ./run [OPTION] <server-id> <directory/timepoint>

OPTION:
	development <domain> <host>
	testing <domain> <host>
	production <domain> <host>

	branch {development|testing|production} <domain> <host> <branchname>
	revert {development|testing|production} <domain> <host> <revision>
	backup <domain> <host> <directory>
	release <domain> <host> <tags> <message>

	list
	list <domain> <host>

	clean {development|testing|production} <domain> <host>
	log <project> <line>

	conf list
	cron show
	cron setup
	cron edit
		
		

模擬演示

環境說明

  1. development 開發環境

  2. testing 測試環境,代碼來自開發環境的合併

  3. production 生產環境,當testing環境通過測試後,將testing 合併到 主幹 即成為生產環境的代碼

    另外我們可以通過release功能將主幹的代碼複製到tags中,命名采用版本號

創建配置檔案

development

部署開發代碼到開發環境

cat deployment/conf/development/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/development/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

創建密碼檔案

$ cat deployment/conf/development/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

testing

部署測試分支到測試環境

cat deployment/conf/testing/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/testing/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

創建密碼檔案

$ cat deployment/conf/testing/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

production

部署主幹代碼到遠程主機

cat deployment/conf/production/mydomain.com/www.conf

REPOSITORY=git@192.168.2.1:mydomain.com/www.mydomain.com
MODE=RSYNC
OPTION="--delete --password-file=$PREFIX/conf/production/passwd"
REMOTE="jszb@192.168.2.10"
DESTINATION=mydomain.com/www.mydomain.com
			

創建密碼檔案

$ cat deployment/conf/production/passwd
eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
			

配置排出列表

有時我們不希望某些檔案被上傳到伺服器上。我們可以通過排除列表來排除上傳

cat exclude/mydomain.com/www.lst
/test/phpinfo.php
/config/database.php
/backup/*.sql
			

配置檔案管理

生產環境的安全問題,例如資料庫聯接信息,開發環境與測試環境的資料庫是可以供發人員和測試人員隨意操作的,損壞之後恢復即可,但生產環境的資料庫是不能隨便操作的,除運維人員其他人是不應該有權限的, 我們希望部署到生產環境的時候使用另一個配置檔案,並且這個配置檔案只有運維人員才能編輯。

config/database.php 將覆蓋原有的配置檔案,然後上傳到生產環境

vim share/production/mydomain.com/www/config/database.php
...
你的資料庫連接信息
...
			

部署前/後腳本

部署前需要做什麼

$ cat libexec/mydomain.com/www/before
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/cn/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/news/
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/images/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/bbs/images/
rsync -au $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/css/* $DEPLOY_HOME/src/production/mydomain.com/www.mydomain.com/news/css
			

部署後需要做什麼

cat libexec/mydomain.com/www/after
ssh www@192.168.1.1 "chown www:www -R /www/mydomain.com"
ssh www@192.168.1.1 "chown 700 -R /www/mydomain.com"
ssh www@192.168.1.1 "chown 777 -R /www/mydomain.com/www.mydomain.com/images/upload"
			

配置部署節點

在需要部署的節點上安裝rsync

		
yum install xinetd rsync -y

vim /etc/xinetd.d/rsync <<VIM > /dev/null 2>&1
:%s/yes/no/
:wq
VIM

# service xinetd restart
Stopping xinetd:                                           [  OK  ]
Starting xinetd:                                           [  OK  ]
		
		

/etc/rsyncd.conf 配置檔案

		
# cat /etc/rsyncd.conf
uid = root
gid = root
use chroot = no
max connections = 8
pid file = /var/run/rsyncd.pid
lock file = /var/run/rsync.lock
log file = /var/log/rsyncd.log

hosts deny=*
hosts allow=192.168.2.0/255.255.255.0

[www]
    uid = www
    gid = www
    path = /www
    ignore errors
    read only = no
    list = no
    auth users = www
    secrets file = /etc/rsyncd.passwd
[mydomain.com]
    uid = www
    gid = www
    path = /www/mydomain.com
    ignore errors
    read only = no
    list = no
    auth users = mydomain
    secrets file = /etc/rsyncd.passwd
[example.com]
    uid = www
    gid = www
    path = /www/example.com
    ignore errors
    read only = no
    list = no
    auth users = example
    secrets file = /etc/rsyncd.passwd
		
		

創建密碼

		
cat > /etc/rsyncd.passwd <<EOD
www:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
mydomain:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
example:eF9nJCcGKJPsiqZsfjGXxwfF41cLibTo
EOD
		
		

部署代碼

[Tip]Tip

development | testing 建議使用分支管理, 而production是用master分支

開發環境部署

$ ~/deployment/run branch development mydomain.com www development 首次需要運行,切換到開發分支
$ ~/deployment/run development mydomain.com www
		

測試環境部署

$ ~/deployment/run branch development mydomain.com www testing 首次需要運行,切換到開發分支
$ ~/deployment/run testing mydomain.com www
		

如果每個bug一個分支的情況可以每次先運行

$ ~/deployment/run branch development mydomain.com www bug0005
		

生產環境部署

$ ~/deployment/run production mydomain.com  www
		

每次部署都會在伺服器 /www/mydomain.com/backup/ 下備份更改的檔案

回撤操作

當程序升級失敗需要立即回撤到指定版本時使用

			
$ ~/deployment/run revert {development|testing|production} <domain> <host> <revision>
			
			

./run revert development mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
./run revert testing mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
./run revert production mydomain www 29dd5c3de6559e2ea6749f5a146ee36cbae750a7
			

發行一個版本

release 升級你的版本

$ ~/deployment/run release mydomain.com www stable-2.0
			

分支管理

查看當前分支

[www@manager deployment]$ ./run branch development mydomain.com www
* master
		

切換分支

[www@manager deployment]$ ./run branch development mydomain.com www development
HEAD is now at 461b796 提交最新代碼
Branch development set up to track remote branch development from origin.
Switched to a new branch 'development'
		

現在已經切換到開發分支

[www@manager deployment]$ ./run branch development mydomain.com www
* development
  master
		

備份操作

將生產環境備份至本地

$ ~/deployment/run backup mydomain.com www /backup/2012-06-12/
		

日誌

部署日誌 deploy.YYYY-MM-DD.log, 記錄部署時間與動態

 $ cat log/deploy.2012-08-03.log
[2012-12-06 21:52:05] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:52:10] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:53:13] [checkout] commit:29dd5c3de6559e2ea6749f5a146ee36cbae750a7 /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:53:18] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:53:39] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:53:45] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:54:08] [update] /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:54:10] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
[2012-12-06 21:54:13] [checkout] commit:29dd5c3de6559e2ea6749f5a146ee36cbae750a7 /opt/git/testing/mydomain.com/m.mydomain.com
[2012-12-06 21:54:15] [deploy] testing/mydomain.com/m.mydomain.com => www@192.168.2.15:mydomain.com/m.mydomain.com
		

項目日誌 www.example.com.log 記錄項目有哪些更新, 上傳的細節, 你能通過日誌看到那些檔案被上傳

		
$ cat log/www.example.com.log
--------------------------------------------------
HEAD is now at 03b3ad5 XXXXXXXXXXXX
- share:
- libexec:
2012/12/06 21:53:45 [12488] building file list
2012/12/06 21:53:45 [12488] .d..t...... application/config/development/
2012/12/06 21:53:45 [12488] <f.st...... application/config/development/database.php
2012/12/06 21:53:45 [12488] .d..t...... application/controllers/
2012/12/06 21:53:45 [12488] <f.st...... application/controllers/info.php
2012/12/06 21:53:45 [12488] .d..t...... application/core/
2012/12/06 21:53:45 [12488] <f.st...... application/core/MY_Controller.php
2012/12/06 21:53:45 [12488] .d..t...... application/models/
2012/12/06 21:53:45 [12488] <f.st...... application/models/news.php
2012/12/06 21:53:45 [12488] .d..t...... application/views/
2012/12/06 21:53:45 [12488] <f.st...... application/views/example.html
2012/12/06 21:53:45 [12488] <f.st...... application/views/index.php
2012/12/06 21:53:45 [12488] .d..t...... resources/css/
2012/12/06 21:53:45 [12488] <f.st...... resources/css/m.css
2012/12/06 21:53:45 [12488] sent 23640 bytes  received 421 bytes  3701.69 bytes/sec
2012/12/06 21:53:45 [12488] total size is 2869760  speedup is 119.27
--------------------------------------------------
		
		

debug

啟用調試模式

vim bin/deploy.git

DEBUG=yes
		

然後查看log/debug.log


Netkiller 微信訂閲號