需求
線上前端web靜態頁面部署,希望通過開發人員SVN提交的方式進行自動部署。
解決方案
主機IP | 用途 |
---|---|
192.168.118.14 | WEB服務器 |
192.168.118.15 | SVN服務器 |
192.168.118.14 目前存在的靜態資源如下:
[root@ngx-node1 /www]#ls
index.html test10.html test1.html test2.html test3.html test4.html test5.html test6.html test7.html test8.html test9.html
首先在 192.168.118.15 上搭建 svn 服務器
[root@svn ~]#yum install subversion -y
[root@svn ~]#mkdir -pv /svn/ngx_web/
[root@svn ~]#svnadmin create /svn/www/
分別修改三個配置文件: authz、 passwd、svnserve.conf
[root@svn /svn/ngx_web/conf]#cat authz
...
admin = ngxWeb
[/]
@admin = rw
[root@svn /svn/ngx_web/conf]#cat passwd
...
ngxWeb = ngxWeb
[root@svn /svn/ngx_web/conf]#egrep -v "^#|^$" svnserve.conf
[general]
anon-access = read
auth-access = write
password-db = passwd
authz-db = authz
[sasl]
[root@svn /svn/ngx_web/conf]#vim /etc/sysconfig/svnserve
# OPTIONS is used to pass command-line arguments to svnserve.
#
# Specify the repository location in -r parameter:
OPTIONS="-r /svn/"
修改完成重啟服務
[root@svn /svn/ngx_web/conf]#systemctl start svnserve
注意:這里的定義 /svn/[www] 這里的 www 必須和已經存在的 web目錄 一致。
到此,svn服務器搭建完成。
svn鏈接:svn://192.168.118.15/www
svn用戶名:ngxWeb
svn密碼:ngxWeb
接下來的思路就是:
- 首先將192.168.118.14 已經存在的web頁面提交到svn中
- 在本地電腦上拉取線上已經存在的web頁面
- 在 192.168.118.14 設置自動拉取
首先提交現有的web頁面(192.168.118.14)
[root@ngx-node1 ~]#svn --username=ngxWeb --password=ngxWeb checkout svn://192.168.118.15/www /www/
-----------------------------------------------------------------------
ATTENTION! Your password for authentication realm:
<svn://192.168.118.15:3690> 733127ad-8eef-4a55-ae41-21a9cf92e057
can only be stored to disk unencrypted! You are advised to configure
your system so that Subversion can store passwords encrypted, if
possible. See the documentation for details.
You can avoid future appearances of this warning by setting the value
of the 'store-plaintext-passwords' option to either 'yes' or 'no' in
'/root/.subversion/servers'.
-----------------------------------------------------------------------
Store password unencrypted (yes/no)? yes
Checked out revision 0.
# svn 成功會在 /www/ 目錄下生成一個 .svn 的隱藏目錄
[root@ngx-node1 /www]#ls -a
. .. index.html .svn test1.html test2.html test3.html test4.html test5.html test6.html test7.html test8.html test9.html
[root@ngx-node1 /www]#svn add *
A index.html
A test1.html
A test2.html
A test3.html
A test4.html
A test5.html
A test6.html
A test7.html
A test8.html
A test9.html
[root@ngx-node1 /www]#svn commit -m 'first commit.'
Adding index.html
Adding test1.html
Adding test2.html
Adding test3.html
Adding test4.html
Adding test5.html
Adding test6.html
Adding test7.html
Adding test8.html
Adding test9.html
Transmitting file data ..........
Committed revision 1.
接下來在本地電腦上建立SVN
到這里, web服務器和本地目錄的web文件是一致了。接下來為服務器端編寫一個定時任務,每分鍾都去 update 一次。
web 服務器:192.168.118.14
[root@ngx-node1 ~]#crontab -l
*/1 * * * * svn --username=ngxWeb --password=ngxWeb checkout svn://192.168.118.15/www /www/ &>> /tmp/svn_checkout_ngxweb.log
# 監控這個文件,每分鍾都會進行一次 checkout
[root@ngx-node1 /tmp]#tailf svn_checkout_ngxweb.log
Checked out revision 1.
Checked out revision 1.
本地添加一個頁面提交:
查看日志文件是否checkout到 web服務器目錄
[root@ngx-node1 /tmp]#tailf svn_checkout_ngxweb.log
Checked out revision 1.
Checked out revision 1.
A /www/hello.html
Checked out revision 2.
# 進入目錄查看
[root@ngx-node1 /tmp]#ls /www/
# hello.html 已經存在
hello.html index.html test1.html test2.html test3.html test4.html test5.html test6.html test7.html test8.html test9.html
這樣,一個簡易的svn 自動部署靜態頁面的提交需求就完成,當然還有諸多的不夠完善的地方,比如:兩個人同一個svn可能會發生沖突,導致提交不成功,或者一些異常情況。但是這樣的方式,對於測試環境來說,應該能夠應付使用了。