gitlab安装 和WebHooks实现生产环境代码自动更新

首先我们在vm虚拟机上设置一个 gitlab服务器(内存4g以上) web服务器 本地服务器(windows10)
一、GitLab英文版安装 gitlab服务器
1、官方源

wget --content-disposition https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-10.8.4-ce.0.el7.x86_64.rpm/download.rpm

2、国内源

wget --content-disposition https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.8.4-ce.0.el7.x86_64.rpm

3、下载安装python

yum install policycoreutils-python

4、安装gitlab

rpm -ivh gitlab-ce-10.8.4-ce.0.el7.x86_64.rpm

5.修改配置文件
将external_url变量的地址修改为gitlab所在centos的ip地址(或者域名)

vim  /etc/gitlab/gitlab.rb

在这里插入图片描述
 6.使配置生效并启动GitLab

gitlab-ctl reconfigure
gitlab-ctl restart

二、GitLab汉化 gitlab服务器
1、安装git

	yum install -y git

2.克隆汉化补丁仓库

git clone https://gitlab.com/xhang/gitlab.git

3.查看当前gitlab版本并且获取对应版本的中文补丁

head -1 /opt/gitlab/version-manifest.txt 

cd gitlab

git diff v10.8.4 v10.8.4-zh  > ../v10.8.4-zh.diff

4.备份gitlab-rails

cp -r /opt/gitlab/embedded/service/gitlab-rails/* /home/test/bak

5、cp -rf 依旧会出现提示覆盖命令

[root@xxxx test]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

[root@xxxx test]# vi ~/.bashrc 

# .bashrc

# User specific aliases and functions

alias rm='rm -i'
#alias cp='cp -i'
alias mv='mv -i'

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi



让命令生效:

source ~/.bashrc

6、将中文补丁导入gitlab

gitlab-ctl stop
patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 <  ../v10.8.4-zh.diff   #一路回车知道完成

7、重新配置并启动

gitlab-ctl reconfigure 

gitlab-ctl start

在这里插入图片描述
三、Gitlab的WebHooks实现生产环境代码自动更新
1、设置项目和用户组这里就不单独说了
2、在本地生成密钥,我本地Windows系统。这样就可以push到gitlab服务器上
生成密钥地址

在这里插入图片描述

同样的方法我们在web服务器上也生成一套ssh密钥,把公钥上传到gitlab服务器上。别忘了host文件虚拟指向域名

3 、我们找到项目管理—》设置–》集成

在这里插入图片描述
我们在web服务器上搭建一个钩子,代码如下:

可以参考
https://blog.csdn.net/weixin_36851500/article/details/104011450

<?php
//网站目录
$www_file='/home/www/test/';

//打开网站目录下的hooks.log文件,需要在服务器上创建,并给写权限
$fs = fopen('/home/www/log/hooks.log', 'a');

fwrite($fs, '================ Update Start ==============='.PHP_EOL.PHP_EOL);

//自定义字串掩码 用于验证
$access_token = 'QhNO8YHqym5PHQQsexapF7041xOhzm62DRH';

//接受的ip数组,也就是允许哪些IP访问这个文件 这里是gitlab服务器IP
$access_ip = array('192.168.26.133');

//如果使用www.xxx.com/xxx.php?token=xxxxxxx 的方式来传送验证字符串,则用这个方法获取
# $client_token = $_GET['token'];

// 获取请求端的secret token
$client_token = $_SERVER["HTTP_X_GITLAB_TOKEN"];

//获取请求端的IP
$client_ip = $_SERVER['REMOTE_ADDR'];

//把请求的IP和时间写进log
fwrite($fs, 'Request on ['.date("Y-m-d H:i:s").'] from ['.$client_ip.']'.PHP_EOL);

//验证token 有错就写进日志并退出
if ($client_token !== $access_token)
{
    echo "error 403";
    fwrite($fs, "Invalid token [{$client_token}]".PHP_EOL);
    exit(0);
}

//验证ip
if ( !in_array($client_ip, $access_ip))
{
    echo "error 503";
    fwrite($fs, "Invalid ip [{$client_ip}]".PHP_EOL);
    exit(0);
}

//获取请求端发送来的信息,具体格式参见gitlab的文档
$json = file_get_contents('php://input');
$data = json_decode($json, true);

//如果有需要 可以打开下面,把传送过来的信息写进log
fwrite($fs, 'Data: '.print_r($data, true).PHP_EOL);

//执行shell命令并把返回信息写进日志
$output=shell_exec("cd $www_file && sudo git pull 2>&1");

$res_log = 'Success:'.PHP_EOL;
    $res_log .= $cmd . '结果' . $output.PHP_EOL;
    $res_log .= $content['head_commit']['author']['name'] . ' 在' . date('Y-m-d H:i:s') . '向' . $content['repository']['name'] . '项目的' . $content['ref'] . '分支push了' . count($content['commits']) . '个commit:' . PHP_EOL;
    $res_log .= $res.PHP_EOL;
    $res_log .= '======================================================================='.PHP_EOL;
    echo $res_log;
fwrite($fs, 'Info:'. $output.PHP_EOL);

fwrite($fs,PHP_EOL. '================ Update End ==============='.PHP_EOL.PHP_EOL);

$fs and fclose($fs);

?>

在这里插入图片描述

注:10版本如果有本地虚拟域名可能会报错

URL 'http://gitlab.hook/' is blocked: Requests to the local network are not allowed

即可进入Admin area 管理区域,在Admin area中,在settings标签下面,找到OutBound Request,勾选上Allow requests to the local network from hooks and services ,保存更改即可解决问题
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_36851500/article/details/105302360
今日推荐