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 ,保存更改即可解決問題
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章