[关闭]
@leptune 2017-06-07T10:12:13.000000Z 字数 2510 阅读 315

本地git代码如何自动部署到服务器

技术


  1. ssh root@example.com
  2. ##############################################
  3. # 到服务器了
  4. # 增加git用户
  5. useradd git
  6. # 把git用户加进www组
  7. usermod -a -G www git
  8. # 创建仓库
  9. su git
  10. cd ~
  11. mkdir .gitbase
  12. cd .gitbase
  13. mkdir project
  14. cd project
  15. git init --bare
  16. # 自动部署设置
  17. cd hooks
  18. cat > post-receive <<EOF
  19. #!/bin/sh
  20. #
  21. # git autodeploy script when it matches the string "[deploy]"
  22. #
  23. # @author icyleaf <icyleaf.cn@gmail.com>
  24. # @link http://icyleaf.com
  25. # @version 0.1
  26. #
  27. # Usage:
  28. # 1. 将该脚本放入bare仓库的hooks文件夹,命名为post-receive
  29. # 2. 加入可执行权限: chmod +x post-receive
  30. # 3. 将DEPLOY_DIR改为你要部署的目录(得先将该项目clone到DEPLOY_DIR目录)
  31. # 4. 完成!
  32. DEPLOY_DIR=/var/www/web/project
  33. LOGDIR=$(pwd)/hooks/.post-receive.log
  34. date -d now +'-------------------%Y-%m-%d %H:%M:%S----------------' &>> $LOGDIR
  35. # Check the remote git repository whether it is bare
  36. IS_BARE=$(git rev-parse --is-bare-repository)
  37. if [ -z "$IS_BARE" ]; then
  38. echo "错误: 不是bare仓库! " &>> $LOGDIR
  39. exit 1
  40. fi
  41. # Get the latest commit subject
  42. SUBJECT=$(git log -1 --pretty=format:"%s")
  43. # Deploy the HEAD sources to publish
  44. IS_PULL=$(echo "$SUBJECT" | grep "\[deploy\]")
  45. if [ -z "$IS_PULL" ]; then
  46. echo "未部署:提交信息未包含\"[deploy]\"字符串" &>> $LOGDIR
  47. exit 1
  48. fi
  49. # Check the deploy dir whether it exists
  50. if [ ! -d $DEPLOY_DIR ] ; then
  51. echo "错误:\"$DEPLOY_DIR\"目录不存在!" &>> $LOGDIR
  52. exit 1
  53. fi
  54. # Check the deploy dir whether it is git repository
  55. #
  56. #IS_GIT=$(git rev-parse --git-dir 2>/dev/null)
  57. #if [ -z "$IS_GIT" ]; then
  58. # echo >&2 "fatal: post-receive: IS_NOT_GIT"
  59. # exit 1
  60. #fi
  61. # Goto the deploy dir and pull the latest sources
  62. cd $DEPLOY_DIR
  63. env -i /usr/bin/git pull &>> $LOGDIR
  64. if [ $? != 0 ] ; then
  65. echo '部署失败!' &>> $LOGDIR
  66. exit 1
  67. fi
  68. echo '部署成功!' &>> $LOGDIR
  69. EOF
  70. chmod +x post-receive
  71. cd ~
  72. mkdir .ssh
  73. exit
  74. exit
  75. ##############################################
  76. # 到本地了
  77. # 设置git用户免密登录服务器,push代码设置
  78. cd ~
  79. ssh-keygen -t rsa
  80. cd .ssh
  81. scp id_rsa.pub root@example.com:/home/git/.ssh/
  82. ssh root@example.com
  83. ##############################################
  84. # 到服务器了
  85. # 设置git用户免密登录服务器,push代码设置
  86. chown git:git /home/git/.ssh/id_rsa.pub
  87. su git
  88. cd ~/.ssh
  89. cat id_rsa.pub >> authorized_keys
  90. exit
  91. exit
  92. ##############################################
  93. # 到本地了
  94. # 本地仓库增加远程仓库地址,以将本地代码及提交记录都推送到服务器仓库
  95. cd ~/project
  96. git remote add project git@example.com:/home/git/.gitbase/project
  97. git push project master
  98. ssh root@example.com
  99. ##############################################
  100. # 到服务器了
  101. # 将本地提交的代码部署到站点目录
  102. cd /data/www/web/example.com
  103. mkdir ~/tmp
  104. mv * ~/tmp
  105. mv .* ~/tmp
  106. git clone /home/git/.gitbase/project
  107. mv project/* .
  108. mv project/.* .
  109. rmdir project
  110. # 将所有者改为www用户
  111. chown www:www -R .
  112. # 让www的组员git也拥有写权限,git用户好自动更新该文件夹的代码
  113. chmod g+w -R .
  114. exit
  115. ##############################################
  116. # 到本地了
  117. # 测试是否成功设置自动部署:
  118. cd ~/project
  119. echo '普通提交' > test.txt
  120. git add .
  121. git commit -m '普通提交'
  122. git push project
  123. # 此时检查服务器的www的project目录,并没有更新
  124. echo '自动部署提交' >> test.txt
  125. git add .
  126. git commit -m '[deploy]自动部署提交'
  127. git push project
  128. # 此时检查服务器的www的project目录,已经自动更新了代码,完成!!
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注