Chinaunix首页 | 论坛 | 博客
  • 博客访问: 5119989
  • 博文数量: 921
  • 博客积分: 16037
  • 博客等级: 上将
  • 技术积分: 8469
  • 用 户 组: 普通用户
  • 注册时间: 2006-04-05 02:08
文章分类

全部博文(921)

文章存档

2020年(1)

2019年(3)

2018年(3)

2017年(6)

2016年(47)

2015年(72)

2014年(25)

2013年(72)

2012年(125)

2011年(182)

2010年(42)

2009年(14)

2008年(85)

2007年(89)

2006年(155)

分类: LINUX

2013-06-22 12:15:57

    Web项目的开发过程中我们经常会有这样的需求:当我们向svn服务器commit修改的代码后,能够即时的体现在测试站点上。其实svn已经为我们提供了这样的接口,那就是hooks。

    当我们创建一个svn repository之后,我们可以看到它的repository结构,有一个hooks文件夹,里面有一些.tmpl文件。这是一些示例脚本,我们可以用这些示例脚本作为范本来修改满足自己的项目需求。

下面描述一下我们的项目需求和实现:

    我们的svn代码服务器和web测试服务器是两个不同的服务器,其中svn代码服务器是ubuntu-server系统,web测试服务器是centos。我们要在svn代码服务器收到开发人员commit的变更之后即时的通知(通过ssh连接到测试服务器)web测试服务器使之自动从svn代码服务器获取svn变更(svn update)。所以我们创建了一个post-commit的hook。这个hook脚本使用了expect,在ubuntu中可以通过"sudo apt-get install expect"来安装。

参考了这篇博文,利用博文中提供的脚本,我做了一些小的修改,最后是下面这样的:


  1. #!/usr/bin/expect -f
  2. ## AUTHOR: Meitar Moscovitz
  3. # DATE : Thu Jun 21 16:32:42 EDT 2007#
  4.  
  5. set HOST my.web.server #服务器地址
  6. set USER someuser #用户名
  7. set PASS xxx #密码
  8.  
  9. # the working copy we're going to update
  10. set WC /path/to/working/copy #测试web的svn副本路径
  11. set LOG /path/to/log/file #日志输出(可以查看到最后一次svn update的revision)
  12.  
  13. # the path to the svn executable on the remote web server
  14. set SVNBIN /usr/bin/svn
  15.  
  16. # our network is slow, set a long timeout
  17. set timeout 30
  18.  
  19. ##### DO NOT EDIT PAST THIS LINE! #####
  20. # POST-COMMIT HOOK#
  21. # The post-commit hook is invoked after a commit. Subversion runs
  22. # this hook by invoking a program (script, executable, binary, etc.)
  23. # named 'post-commit' (this file) with the
  24. # following ordered arguments:#
  25. # [1] REPOS-PATH (the path to this repository)
  26. # [2] REV (the number of the revision just committed)#
  27. # Note that Subversion does not provide this program with an environment
  28. # of any kind. That means this program lacks a current working directory,
  29. # a home directory, a $PATH, and so on.
  30.  
  31. set REPOS [lindex $argv 0]
  32. set REV [lindex $argv 1]
  33.  
  34. # Define error codes
  35. set E_NO_SSH 1 ;# can't find a usable SSH on our system
  36. set E_NO_CONNECT 2 ;# failure to connect to remote server (timed out)
  37. set E_WRONG_PASS 3 ;# password provided does not work
  38. set E_UNKNOWN 25 ;# unexpected failure

  39. # find the SSH binary on our system
  40. if {[file executable /usr/bin/ssh]} {
  41. set SSHBIN /usr/bin/ssh
  42. } elseif {[file executable /usr/local/bin/ssh]} {
  43. set SSHBIN /usr/local/bin/ssh
  44. } else {
  45. send_error "Can't find a usable SSH on this system.\n"
  46. exit $E_NO_SSH
  47. }
  48.  
  49. spawn $SSHBIN $USER@$HOST "$SVNBIN update -r HEAD --force $WC >> $LOG"
  50.  
  51. expect {
  52.     "continue connecting (yes/no)? " { send "yes\r"; exp_continue; }
  53.     -nocase "password:" { send "$PASS\r"; }
  54.     timeout {
  55.         send_error "\nWe have timed out after $timeout seconds while trying to connect to $HOST!\n";
  56.         exit $E_NO_CONNECT;
  57.     }
  58. }
  59.  
  60. expect {
  61.         -nocase "password:" { ;# if we are asked for the password again, then we have provided the wrong password
  62.                 send_error "\nCan not log in to $HOST because the password provided for user $USER has been rejected.\n";
  63.                 exit $E_WRONG_PASS;
  64.         }
  65.         -re "revision (\[0-9]+)." {
  66.                 if {$REV == $expect_out(1,string)} {
  67.                         send_user "\nSuccessfully updated $WC on $HOST to revision $REV.\n"
  68.                 } else {
  69.                         send_user "\nUpdated repository to revision $expect_out(1,string), but svn reports that we are at revision number $REV.\n"
  70.                         send_error "CAUTION: Repository updated to revision $expect_out(1,string), but committed revision $REV.\n"
  71.                 }
  72.         }
  73. #将下面几行注释掉的原因是因为我发现#其实这个hook脚本已经成功使web测试服务器更新(svn update)了working copy但是还是会抛出这个错误!
  74. #而且这个错误也会在svn客户端(比如:TortoiseSVN)的commit动作的最后出现!
  75. # default {
  76. # send_error "An unexpected error has occured. The process at spawn ID $spawn_id has produced the following output:\n"
  77. # send_error $expect_out(buffer)
  78. # exit $E_UNKNOWN
  79. # }
  80. }


好了,另外要注意一下这个post-commit脚本文件要具备关联用户(比如:www-data)的可执行权限。可以直接输入"./post-commit"测试一下结果。 



文章来自:http://hi.baidu.com/devylee/item/247db2d0aead56362b35c7f1


阅读(4529) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~