代码部署是开发流程中的关键环节,如果手动通过ftp、git方式会造成非常多的困扰,较为传统的做法就是搭建SVN服务器,利用SVN的hooks实现自动部署功能。
一、服务器端
(1) 下载svn
yum install -y subversion
(2) 创建SVN仓库
注意: "/home/mydisk" 是我挂载的云盘
svnadmin create /home/mydisk/svndata-test
(3) 配置SVN
(3.1) 增加用户
vim /home/mydisk/svndata-test/passwd
内容如下(直接内容全部替换即可):
### This file is an example password file for svnserve.
### Its format is similar to that of svnserve.conf. As shown in the
### example below it contains one section labelled [users].
### The name and password for each user follow, one account per line.
[users]
# harry = harryssecret
# sally = sallyssecret
jack = jackssecret
(3.2) 打开服务配置
vim ./conf/svnserve.conf
内容如下:
### This file controls the configuration of the svnserve daemon, if you
### use it to allow access to this repository. (If you only allow
### access through http: and/or file: URLs, then this file is
### irrelevant.)
### Visit http://subversion.apache.org/ for more information.
[general]
### The anon-access and auth-access options control access to the
### repository for unauthenticated (a.k.a. anonymous) users and
### authenticated users, respectively.
### Valid values are "write", "read", and "none".
### Setting the value to "none" prohibits both reading and writing;
### "read" allows read-only access, and "write" allows complete
### read/write access to the repository.
### The sample settings below are the defaults and specify that anonymous
### users have read-only access to the repository, while authenticated
### users have read and write access to the repository.
anon-access = read
auth-access = write
### The password-db option controls the location of the password
### database file. Unless you specify a path starting with a /,
### the file's location is relative to the directory containing
### this configuration file.
### If SASL is enabled (see below), this file will NOT be used.
### Uncomment the line below to use the default password file.
password-db = passwd
### The authz-db option controls the location of the authorization
### rules for path-based access control. Unless you specify a path
### starting with a /, the file's location is relative to the the
### directory containing this file. If you don't specify an
### authz-db, no path-based access control is done.
### Uncomment the line below to use the default authorization file.
authz-db = authz
(3.3) 修改权限
vim /home/mydisk/svndata-test/conf/authz.conf
给jack为admin分组,admin分组的权限是可读可写。另外创建一个visitor分组,只能读取。内容如下:
### This file is an example authorization file for svnserve.
### Its format is identical to that of mod_authz_svn authorization
### files.
### As shown below each section defines authorizations for the path and
### (optional) repository specified by the section name.
### The authorizations follow. An authorization line can refer to:
### - a single user,
### - a group of users defined in a special [groups] section,
### - an alias defined in a special [aliases] section,
### - all authenticated users, using the '$authenticated' token,
### - only anonymous users, using the '$anonymous' token,
### - anyone, using the '*' wildcard.
###
### A match can be inverted by prefixing the rule with '~'. Rules can
### grant read ('r') access, read-write ('rw') access, or no access
### ('').
[aliases]
# joe = /C=XZ/ST=Dessert/L=Snake City/O=Snake Oil, Ltd./OU=Research Institute/CN=Joe Average
[groups]
admin = jack
visitor = mark
[svndata-test:/]
@admin = rw
@visitor = r
(4) 启动SVN项目
svnserve -d -r /home/mydisk/
(5) 测试是否启动成功
注意:3690为svn默认端口,启动时可以手动修改
svn co svn://localhost:3690/svndata-test
成功会显示:取出版本 0.
(6) 服务器的本地check项目(仓库只是版本管理等,并不是项目本身)
svn checkout svn://localhost:3690/svndata-test --username jack --password jackssecret /home/mydisk/mycode
(7) 设置SVN的Hooks
在远程客户端进行commit操作时,服务端的svn会自动操作update。
vim /home/mydisk/svndata-test/hooks/post-commit
内容如下:
#!/bin/sh
REPOS="$1"
REV="$2"
export LANG=en_US.UTF-8
SVN_PATH=/usr/bin
WEB_PATH=/home/mydisk/mycode
SVN_USER=jack
SVN_PASS=jackssecret
LOG_PATH=/tmp/svn.log
echo `date "+%Y-%m-%d %H:%M:%S"` >> $LOG_PATH
echo `whoami`,$REPOS,$REV >> $LOG_PATH
$SVN_PATH/svn update $WEB_PATH --username $SVN_USER --password $SVN_PASS --no-auth-cache >> $LOG_PATH
exit 0
(8) 给予post-commit操作权限
如果不给予权限,那么自动更新会报错。
chmod 777 /home/mydisk/svndata-test/hooks/post-commit
一、客户端(MAC)
(1) 下载svn客户端
由于我用的是mac,自带svn,无需下载(所有客户端原理一样,不再累赘)
(2) checkout获取项目
/Develop是我的一个已存在的开发目录
svn checkout svn://serverip:3690/svndata-test --username jack --password jackssecret /Develop/mycode
(3) 任意创建一个文件new.txt,并上传
执行完后,可以在服务器看到new.txt文件了。
cd /Develop/mycode
mkfile -n 2b new.txt
svn add new.txt
svn commit -m "new file."