GIT 第12章 post-receive GIT 第12章 post-receive

2022-07-18

git服务器搭建post-receive 钩子部署服务端代码

①、git服务器搭建

安装git

apt-get install git

创建git用户

adduser git
passwd git  //修改git用户密码

创建git仓库

cd /home
mkdir git
mkdir ./git/.ssh
touch./git/.ssh/authorized_keys
cd /home/git
git init --bare test.git    //初始化仓库
chown -R git:git ../git

使用git用户生成git密钥(把客户端的公钥(id_rsa.pb文件内容)添加到authorized_keys文件,git push操作就不需要每次都输入密码了)

sudo -u git ssh-keygen -t rsa -C “test@qq.com”   //生成密钥
cd /home/git/.ssh
cat id_rsa.pub>>authorized_keys  //把公钥加入authorized_keys文件

创建web目录

cd /home
git clone ssh://git@127.0.0.1/home/git/test.git
chown -R git:git test

编写自动更新钩子

cd /home/git/test.git/hooks/
vim post-receive
#!/bin/sh
cd /home/test/
unset GIT_DIR
git pull origin master

给钩子文件添加执行权限

cd /home/git/test.git/hooks/
$ chmod +x post-recevie

②、以上步骤完成git服务器就已经搭建好了,下面来讲一下客户端的使用操作

首先生成客户端密钥,且把公钥复制到authorized_keys文件里

ssh-keygen -t rsa -C "test@qq.com"   //生成密钥
cat /userdir/.ssh/id_rsa.pub    // 把显示的内容复制到服务端的authorized_keys文件里

然后把git仓库拷贝下来

git clone ssh://git@123.456.78.9/home/git/test.git

测试操作(git push完成后git自动对web目录做git pull操作)

cd test
git pull origin master
touch test.txt
git add .
git commit -m "test"
git push origin master

打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,你说多少就多少

打开微信扫一扫,即可进行扫码打赏哦

阅读 557