devops-Gitlab的部署与使用

郝杰
2023-12-01

Gitlab 服务的安装文档:
https://about.gitlab.com/install/
环境要求:
https://docs.gitlab.com/ee/install/requirements.html

1、Ubuntu 系统环境准备

#1:配置 ubuntu 远程连接: 
jack@ubuntu:~$ sudo su - root 
[sudo] password for jack: 
root@ubuntu:~# passwd 
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully 
root@ubuntu:~# vim /etc/ssh/sshd_configPermitRootLogin yes 
PasswordAuthentication yes 
#2:配置 ubuntu 网卡和主机名: 
root@ubuntu:~# cat /etc/netplan/01-netcfg.yaml 
# This file describes the network interfaces available on your system 
# For more information, see netplan(5). 
network: 
version: 2 
renderer: networkd 
ethernets: 
eth0: 
dhcp4: no 
addresses: [192.168.8.2/21] 
gateway4: 192.168.15.254 
nameservers: 
addresses: [192.168.15.254] 
root@ubuntu:~# cat /etc/hostname 
jenkins.example.com 
root@ubuntu:~# reboot 
#3:配置 ubuntu 仓库: 
root@ubuntu:~#vim /etc/apt/sources.list 
deb http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-security main restricted universe 
multiversedeb http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-updates main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-proposed main restricted universe multiverse 
deb http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 
deb-src http://mirrors.aliyun.com/ubuntu/ bionic-backports main restricted universe multiverse 
root@jenkins:~# apt update 
root@jenkins:~# apt install iproute2 ntpdate tcpdump telnet traceroute nfs-kernel-server nfs-common lrzsz tree openssl libssl-dev libpcre3 libpcre3-dev zlib1g-dev ntpdate tcpdump telnet traceroute gcc openssh-server lrzsz tree openssl libssl-dev libpcre3 libpcre3- dev zlib1g-dev ntpdate tcpdump telnet traceroute iotop unzip zip ipmitool 

2、Centos 系统环境准备

# yum install vim gcc gcc-c++ wget net-tools lrzsz iotop lsof iotop bash-completion -y 
# yum install curl policycoreutils openssh-server openssh-clients postfix -y 
# wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo 
# systemctl disable firewalld 
# sed -i '/SELINUX/s/enforcing/disabled/' /etc/sysconfig/selinux 
# hostnamectl set-hostname gitlab.example.com 
# reboot

3、gitlab 安装及使用

安装包下载地址:https://packages.gitlab.com/gitlab/gitlab-ce
rpm 包国内下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/

root@gitlab:~# dpkg -i gitlab-ce_11.3.11-ce.0_amd64.deb
#1:gitlab 配置使用: 
root@gitlab:~# grep "^[a-Z]" /etc/gitlab/gitlab.rb
external_url 'http://192.168.8.3'
#可选邮件通知设置
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "2973707860@qq.com"
gitlab_rails['smtp_password'] = "授权码"
gitlab_rails['smtp_domain'] = "qq.com"
gitlab_rails['smtp_authentication'] = :login
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = "2973707860@qq.com"
user["git_user_email"] = "2973707860@qq.com"

#2:初始化服务: 执行配置并启动服务:
# gitlab-ctl reconfigure #修改完配置文件要执行此操作

#3:常用命令: 
# gitlab-rails #用于启动控制台进行特殊操作,比如修改管理员密码、打开数据库控制台( gitlab-rails dbconsole)等。
# gitlab-psql #数据库命令行
# gitlab-rake #数据备份恢复等数据操作
# gitlab-ctl #客户端命令行操作行
# gitlab-ctl stop #停止 gitlab
# gitlab-ctl start #启动 gitlab
# gitlab-ctl restar #重启 gitlab
# gitlab-ctl status #查看组件运行状态
# gitlab-ctl tail nginx #查看某个组件的日志

#4:验证 gitlab 启动完成:
root@gitlab:~# gitlab-ctl status #查看组件运行状态

#5:验证端口及状态:
#80 端口是在初始化 gitlib 的时候启动的,因此如果之前的有程序占用会导致初始化失败或无法访问!
root@gitlab:~# lsof -i:80

#6:登录gitlab web界面并设置密码,最少8位
#7:登录 gitlab,默认用户为 root:
#8:默认首页:
#9:关闭账号注册: 
#默认情况下可以直接注册账号,因此一般都关闭次功能:取消账户注册功能之后点 save 
#10:验证是否还有注册选项:
#11:创建 git 账户: 
#12:重新设置密码: 
#第一次使用新账号登录要设置密码:通过邮件重置用户密码: 
#13:创建组: 
#使用管理员 root 创建组,一个组里面可以有多个项目分支,可以将开发添加到组里面进行设置权限,不同的组就是公司不同的开发项目或者服务模块,不同的组添加不同的开发即可实现对开发设置权限的管理。
#14:使用管理员创建项目:
#15:将用户添加到组: 
https://docs.gitlab.com/ee/user/permissions.html (更多权限)
#16:创建一个测试页面: 

#17:git 客户端测试 clone 项目: 
root@jenkins:/source# git clone http://192.168.8.3/test-service/test-project.git
root@jenkins:/source# cat test-project/index.html 
<h1>11111111111</h1>
#编辑文件并测试提交:
root@jenkins:/source# cd test-project/
root@jenkins:/source/test-project# git config --global user.name "jack"
root@jenkins:/source/test-project# git config --global user.email 2973707860@qq.com
root@jenkins:/source/test-project# vim index.html 
root@jenkins:/source/test-project# cat index.html 
<h1>11111111111</h1>
<h1>22222222222</h1>
root@jenkins:/source/test-project# git add index.html 
root@jenkins:/source/test-project# git commit -m "v1"
#18:git web 端验证数据: 

#19:git 常用命令: 
git config --global user.name “name“ #设置全局用户名
git config --global user.email xxx@xx.com #设置全局邮箱
git config --global --list #列出用户全局设置
git add index.html / . #添加指定文件、目录或当前目录下所有数据到暂存区
git commit -m “11“ #提交文件到工作区
git status #查看工作区的状态
git push #提交代码到服务器
git pull #获取代码到本地
git log #查看操作日志
vim .gitignore #定义忽略文件
git reset --hard HEAD^^ #git 版本回滚, HEAD 为当前版本,加一个^为上一个,^^为上上一个版本
git reflog # #获取每次提交的 ID,可以使用--hard 根据提交的 ID 进行版本回退
git reset --hard 5ae4b06 #回退到指定 id 的版本
git branch #查看当前所处的分支
git checkout -b develop #创建并切换到一个新分支
git checkout develop #切换分支

#20:gitlab 数据备份恢复: 
#停止 gitlab 数据服务:
root@s1:~# gitlab-ctl stop unicorn
ok: down: unicorn: 1s, normally up
root@s1:~# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
#手动备份数据:
# gitlab-rake gitlab:backup:create #在任意目录即可备份当前 gitlab 数据
# gitlab-ctl start #备份完成后确定 gitlab
#查看要恢复的文件:
/var/opt/gitlab/backups/ # Gitlab 数据备份目录,需要使用命令备份的
/var/opt/gitlab/nginx/conf #nginx 配置文件
/etc/gitlab/gitlab.rb #gitlab 配置文件
root@s1:~# ll /var/opt/gitlab/backups/
total 408
drwx------ 4 git root 4096 Jul 17 10:42 ./
drwxr-xr-x 20 root root 4096 Jul 17 10:11 ../
-rw------- 1 git git 92160 Jul 17 10:20 1563330030_2019_07_17_11.11.5_gitlab_backup.tar
-rw------- 1 git git 92160 Jul 17 10:23 1563330216_2019_07_17_11.11.5_gitlab_backup.tar
-rw------- 1 git git 92160 Jul 17 10:30 1563330647_2019_07_17_11.11.5_gitlab_backup.tar
-rw------- 1 git git 92160 Jul 17 10:32 1563330751_2019_07_17_11.11.5_gitlab_backup.tar
#执行恢复:
# gitlab-ctl stop unicorn
# gitlab-ctl stop sidekiq #恢复数据之前停止服务
root@s1:~# gitlab-rake gitlab:backup:restore BACKUP=备份文件名
#确认恢复数据:
#启动服务:
root@s1:~# gitlab-ctl start sidekiq
ok: run: sidekiq: (pid 16859) 0s
root@s1:~# gitlab-ctl start unicorn
ok: run: unicorn: (pid 16882) 1s

#21:gitlab 汉化: 
#虽然不推荐,但是有需求,基于第三方开发爱好者实现
#1\下载语言包替换:
https://gitlab.com/xhang/gitlab/-/archive/v11.11.5-zh/gitlab-v11.11.5-zh.tar
https://gitlab.com/xhang/gitlab/-/archive/v11.9.8-zh/gitlab-v11.9.8-zh.tar
#(1)首次安装 gitlab 步骤
# vim /etc/gitlab/gitlab.rb #修改配置
# gitlab-ctl reconfigure
#(2)已经安装 gitlab 步骤
# gitlab-ctl stop
# tar xvf gitlab-vX.Y.Z-zh.tar
# cp -rp /opt/gitlab/embedded/service/gitlab-rails /opt/gitlab-rails.bak #备份源文件
# cp -rf gitlab-vX.Y.Z-zh/* /opt/gitlab/embedded/service/gitlab-rails/ #替换文件
# gitlab-ctl reconfigure
# gitlab-ctl start
#Web 界面更改语言:右上角的账户下拉框选 Settings 然后左侧 Preferences 设置项,然后语言选择中文
#保存后刷新界面:
#2\通过源码汉化:
https://gitlab.com/xhang/gitlab #汉化包地址
# git clone https://gitlab.com/xhang/gitlab.git
# head -1 /opt/gitlab/version-manifest.txt #查看当前 gitlab 版本
# cd gitlab
# git diff v11.9.8 v11.9.8-zh
# git diff v11.9.8 v11.9.8-zh > /root/v11.9.8-zh.diff
# gitlab-ctl stop
# patch -d /opt/gitlab/embedded/service/gitlab-rails -p1 < /root/v11.9.8-zh.diff
# gitlab-ctl reconfigure
# gitlab-ctl start
 类似资料: