@leptune
2017-06-07T10:12:13.000000Z
字数 2510
阅读 358
技术
ssh root@example.com############################################### 到服务器了# 增加git用户useradd git# 把git用户加进www组usermod -a -G www git# 创建仓库su gitcd ~mkdir .gitbasecd .gitbasemkdir projectcd projectgit init --bare# 自动部署设置cd hookscat > post-receive <<EOF#!/bin/sh## git autodeploy script when it matches the string "[deploy]"## @author icyleaf <icyleaf.cn@gmail.com># @link http://icyleaf.com# @version 0.1## Usage:# 1. 将该脚本放入bare仓库的hooks文件夹,命名为post-receive# 2. 加入可执行权限: chmod +x post-receive# 3. 将DEPLOY_DIR改为你要部署的目录(得先将该项目clone到DEPLOY_DIR目录)# 4. 完成!DEPLOY_DIR=/var/www/web/projectLOGDIR=$(pwd)/hooks/.post-receive.logdate -d now +'-------------------%Y-%m-%d %H:%M:%S----------------' &>> $LOGDIR# Check the remote git repository whether it is bareIS_BARE=$(git rev-parse --is-bare-repository)if [ -z "$IS_BARE" ]; thenecho "错误: 不是bare仓库! " &>> $LOGDIRexit 1fi# Get the latest commit subjectSUBJECT=$(git log -1 --pretty=format:"%s")# Deploy the HEAD sources to publishIS_PULL=$(echo "$SUBJECT" | grep "\[deploy\]")if [ -z "$IS_PULL" ]; thenecho "未部署:提交信息未包含\"[deploy]\"字符串" &>> $LOGDIRexit 1fi# Check the deploy dir whether it existsif [ ! -d $DEPLOY_DIR ] ; thenecho "错误:\"$DEPLOY_DIR\"目录不存在!" &>> $LOGDIRexit 1fi# Check the deploy dir whether it is git repository##IS_GIT=$(git rev-parse --git-dir 2>/dev/null)#if [ -z "$IS_GIT" ]; then# echo >&2 "fatal: post-receive: IS_NOT_GIT"# exit 1#fi# Goto the deploy dir and pull the latest sourcescd $DEPLOY_DIRenv -i /usr/bin/git pull &>> $LOGDIRif [ $? != 0 ] ; thenecho '部署失败!' &>> $LOGDIRexit 1fiecho '部署成功!' &>> $LOGDIREOFchmod +x post-receivecd ~mkdir .sshexitexit############################################### 到本地了# 设置git用户免密登录服务器,push代码设置cd ~ssh-keygen -t rsacd .sshscp id_rsa.pub root@example.com:/home/git/.ssh/ssh root@example.com############################################### 到服务器了# 设置git用户免密登录服务器,push代码设置chown git:git /home/git/.ssh/id_rsa.pubsu gitcd ~/.sshcat id_rsa.pub >> authorized_keysexitexit############################################### 到本地了# 本地仓库增加远程仓库地址,以将本地代码及提交记录都推送到服务器仓库cd ~/projectgit remote add project git@example.com:/home/git/.gitbase/projectgit push project masterssh root@example.com############################################### 到服务器了# 将本地提交的代码部署到站点目录cd /data/www/web/example.commkdir ~/tmpmv * ~/tmpmv .* ~/tmpgit clone /home/git/.gitbase/projectmv project/* .mv project/.* .rmdir project# 将所有者改为www用户chown www:www -R .# 让www的组员git也拥有写权限,git用户好自动更新该文件夹的代码chmod g+w -R .exit############################################### 到本地了# 测试是否成功设置自动部署:cd ~/projectecho '普通提交' > test.txtgit add .git commit -m '普通提交'git push project# 此时检查服务器的www的project目录,并没有更新echo '自动部署提交' >> test.txtgit add .git commit -m '[deploy]自动部署提交'git push project# 此时检查服务器的www的project目录,已经自动更新了代码,完成!!