配置gitlab服务器的hooks钩子实现代码自动部署

让GIT服务器可以自动化更新Git的提交到网站目录,从而达到代码自动更新到测试服。

gitlat服务器的安装:https://www.cnblogs.com/youzhibing/p/12487043.html
gitlat官方安装教程:https://about.gitlab.com/install/#ubuntu
找到gitlab服务器的项目对应的hooks文件夹(/var/opt/gitlab/git-data/repositories/root/xxx.git/hooks/)
这里的是:/var/opt/gitlab/git-data/repositories/root/new_lapu.git/hooks  ("root"这个是你创建些git的账户名为目录;"new_lapu.git"这个是GIT地址后面那个)


但是我们需要到目录/var/opt/gitlab/git-data/repositories/root/xxx.git/下

		[email protected]:~# ##新建 custom_hooks目录##
		[email protected]:~# cd /var/opt/gitlab/git-data/repositories/root/new_lapu.git
		[email protected]:~# ##新建post-receive文件##
		[email protected]:~# mkdir custom_hooks
		[email protected]:~# chown git:git custom_hooks
		[email protected]:~# chmod 755 custom_hooks
		[email protected]:~# ls -al|grep custom_hooks
		lrwxrwxrwx  1 git git     47 Nov 24 10:22 hooks -> /opt/gitlab/embedded/service/gitlab-shell/hooks
		[email protected]:~# cd custom_hooks
		[email protected]:~# ##新建post-receive文件##
		[email protected]:~# vim post-receive
		
		#!/bin/bash
		
		echo "正在同步更新测试服代码..."
		#cd /home/wwwroot/20201114lapu/git_lapu
		##unset GIT_DIR
		##git pull origin master
		
		#自动部署的路径
		GIT_WORK_TREE=/home/wwwroot/20201114lapu/lapu/
		#BRANCH=develop
		#这里设置为要自动部署的分支名
		BRANCH=master  
		
		while read oldrev newrev ref
		do
		    if [[ $ref =~ .*/${BRANCH}$ ]];
		    then
		        echo "$BRANCH ref received.  Deploying $BRANCH branch to testserver..."
		        git --work-tree=${GIT_WORK_TREE} checkout ${BRANCH} -f
		        # chmod -R 777 ${GIT_WORK_TREE} 
		        echo "Git自动化部署测试服代码目录:${GIT_WORK_TREE}"
		    else
		        echo "Ref $ref successfully received.  自动布置测试服什么也没处理:只有分支 $BRANCH 才会自动布暑到测试服! "
		    fi
		done
		
		echo "测试服git代码自动化布置完成!  --临来笑笑生"
		# post-receive文件内容结束
		[email protected]:~# chown git:git post-receive
		[email protected]:~# chmod 755 post-receive
		[email protected]:~# ##记得还要去"自动部署的路径"给git权限:##
		[email protected]:~# chmod 777 -R /home/wwwroot/20201114lapu/lapu/
		[email protected]:~# 
		
到此结束,可以去客户端push代码到自动部署的分支上,然后在服务自动部署路径看有没有自动生成文件了。