美文网首页
git自动部署代码

git自动部署代码

作者: leptune | 来源:发表于2023-11-01 16:41 被阅读0次
ssh root@example.com
##############################################
# 到服务器了
# 增加git用户
useradd git
# 把git用户加进www组
usermod -a -G www git
usermod -a -G git www
# 创建仓库
su git
cd ~
mkdir .gitbase
cd .gitbase
mkdir project
cd project
git init --bare
# 自动部署设置
cd hooks
# 新建post-receive文件,并写入下面代码:
#!/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=/home/wwwroot/bestalart.com/web
LOGDIR=$(pwd)/hooks/.post-receive.log
 date -d now +'-------------------%Y-%m-%d %H:%M:%S----------------'  &>> $LOGDIR
# Check the remote git repository whether it is bare
IS_BARE=$(git rev-parse --is-bare-repository)
if [ -z "$IS_BARE" ]; then
        echo "错误: 不是bare仓库! " &>> $LOGDIR
        exit 1
fi

# Get the latest commit subject
SUBJECT=$(git log -1 --pretty=format:"%s")

# Deploy the HEAD sources to publish
IS_PULL=$(echo "$SUBJECT" | grep "\[deploy\]")
if [ -z "$IS_PULL" ]; then
        echo "未部署:提交信息未包含\"[deploy]\"字符串" &>> $LOGDIR
        exit 1
fi

# Check the deploy dir whether it exists
if [ ! -d $DEPLOY_DIR ] ; then
        echo "错误:\"$DEPLOY_DIR\"目录不存在!" &>> $LOGDIR
        exit 1
fi

# 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 sources
cd $DEPLOY_DIR
env -i /usr/bin/git pull &>> $LOGDIR
if [ $? != 0 ] ; then
        echo '部署失败!' &>> $LOGDIR
        exit 1
fi
echo '部署成功!' &>> $LOGDIR

#### 到此代码结束

chmod +x post-receive

cd ~
mkdir .ssh
exit
exit
##############################################
# 到本地了
# 设置git用户免密登录服务器,push代码设置
cd ~
ssh-keygen -t rsa
cd .ssh
scp id_rsa.pub root@example.com:/home/git/.ssh/
ssh root@example.com
##############################################
# 到服务器了
# 设置git用户免密登录服务器,push代码设置
chown git:git /home/git/id_rsa.pub
su git
cd ~/.ssh
cat ../id_rsa.pub >> authorized_keys
cd ..
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
exit
# 到root用户了
# 编辑 /etc/ssh/sshd_config 文件,进行如下设置:
RSAAuthentication yes
PubkeyAuthentication yes
# 编辑结束

exit
##############################################
# 到本地了
# 本地仓库增加远程仓库地址,以将本地代码及提交记录都推送到服务器仓库
cd ~/project
git remote add project git@example.com:/home/git/.gitbase/project
git push project master
ssh root@example.com
##############################################
# 到服务器了
# 将本地提交的代码部署到站点目录
cd /data/www/web/example.com
mkdir ~/.tmp
mv * ~/.tmp/
mv .* ~/.tmp/
git clone /home/git/.gitbase/project
mv project/* .
mv project/.* .
rmdir project
# 将所有者改为www用户
chown www:www -R .
# 让www的组员git也拥有写权限,git用户好自动更新该文件夹的代码
chmod g+w -R .
exit
##############################################
# 到本地了
# 测试是否成功设置自动部署:
cd ~/project
echo '普通提交' > test.txt
git add .
git commit -m '普通提交'
git push project
# 此时检查服务器的www的project目录,并没有更新
echo '自动部署提交' >> test.txt
git add .
git commit -m '[deploy]自动部署提交'
git push project
# 此时检查服务器的www的project目录,已经自动更新了代码,完成!!

相关文章

网友评论

      本文标题:git自动部署代码

      本文链接:https://www.haomeiwen.com/subject/nihgidtx.html