git利用hooks实现自动部署
准备工作:
1、一台虚拟linux环境和window
开始工作
1、安装git(略)
2、创建git用户和创建test.git裸仓库
[root@localhost ~]# useradd -m git #创建用户,shell是bash [root@localhost ~]# su git [git@localhost root]$ cd ~ [git@localhost ~]$ mkdir repository && cd repository [git@localhost repository]$ git init --bare test.git
此时在repository目录生成test.git,作为远程仓库
3、生成公私钥(多种工具生成),此处使用ssh-keygen,大概情形如下
[git@localhost .ssh] cd /home/git/.ssh #没有.ssh目录,自行创建 [git@localhost .ssh]$ ssh-keygen -t rsa -C 'git@master.com' Generating public/private rsa key pair. Enter file in which to save the key (/home/git/.ssh/id_rsa): Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /home/git/.ssh/id_rsa. Your public key has been saved in /home/git/.ssh/id_rsa.pub. The key fingerprint is: SHA256:J06qIJSTy2n0kSfmAwavvxrv2fwK1pd2UwifUg6axl4 git@master.com The key's randomart image is: +---[RSA 2048]----+ | | | | |. o . | |..o..o * o | | O.==.E S o | |=.O++. * + | |+=++o = + | |.* *.+ . . | |.o*.=o. | +----[SHA256]-----+
会生成两个文件id_rsa和id_rsa.pub,以.pub结尾的是公钥,将公钥放到authorized_keys文件中,实现免密登录
[git@localhost .ssh]$ cat id_rsa.pub >> ./authorized_keys
4、window客户端
$ git clone git@ip:/home/git/repository/test.git
5、Linux客户端
[git@localhost ~]$ mkdir test && cd test [git@localhost test]$ git init #初始化仓库 [git@localhost test]$ git remote add origin git@ip:/home/git/repostroy/test.git [git@localhost test]$ git pull origin master The authenticity of host 'ip (ip)' can't be established. ECDSA key fingerprint is SHA256:uSKcAOh8Q8Lca9vlPQhjVGm8lPtEpc09Ze4GTW70lFs. ECDSA key fingerprint is MD5:66:f8:99:85:bc:e2:20:51:f0:4e:59:88:30:2e:5a:2d. Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added 'ip' (ECDSA) to the list of known hosts. remote: Counting objects: 100, done. remote: Compressing objects: 100% (73/73), done. remote: Total 100 (delta 5), reused 0 (delta 0) Receiving objects: 100% (100/100), 7.68 KiB | 0 bytes/s, done. Resolving deltas: 100% (5/5), done. From 192.168.0.159:/home/git/repostroy/test * branch master -> FETCH_HEAD
6、使用post-update实现自动化
[git@localhost ~]$ /home/git/repostroy/test.git/hooks/post-update # 添加如下内容 unset GIT_DIR cd /home/git/fail git pull origin master
7、测试(window客户端)
成功!!!!
碰到的几个问题
1、post-receive和post-update的区别、
2、远程无法执行
答、需要给post-receive或者post-update执行权限
3、如图
答:要在git用户生成公私钥,因为git远程库是用git用户做pull操作的
4、cannot open .git/FETCH_HEAD: Permission denied
答:查看FETCH_HEAD的所属主、所属组是不是git,不是用户chown命令修改
5、暂时没有