版本控制gitlab

一、gitlab介绍

​ GitLab 是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具emma_inlink,并在此基础上搭建起来的Web服务。安装方法是参考GitLab在GitHub上的Wiki页面。Gitlab是被广泛使用的基于git的开源代码管理平台, 基于Ruby on Rails构建, 主要针对软件开发过程中产生的代码和文档进行管理, Gitlab主要针对group和project两个维度进行代码和文档管理, 其中group是群组, project是工程项目, 一个group可以管理多个project, 可以理解为一个群组中有多项软件开发任务, 而一个project中可能包含多个branch, 意为每个项目中有多个分支, 分支间相互独立, 不同分支可以进行归并。

二、gitlab的优势和应用场景

1.开源免费,适合中小型公司将代码放置在该系统中
2.差异化的版本管理,离线同步以及强大分支管理功能
3.便捷的GUI操作界面以及强大账户权限管理功能
4.集成度很高,能够集成绝大数的开发工具
5.支持内置HA,保证在高并发下仍旧实现高可用性

三、gitlab部署

关闭防火墙和selinux,将selinux状态修改为禁用,重启虚拟机

[root@controller ~]# systemctl disable --now firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@controller ~]# 
[root@controller ~]# setenforce 0
[root@controller ~]# vi /etc/selinux/config
[root@controller ~]# cat /etc/selinux/config 
...
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
...
[root@controller ~]# reboot
[root@controller ~]# getenforce
Disabled
[root@controller ~]# 

配置yum源

[root@controller ~]# cd /etc/yum.repos.d/
[root@controller yum.repos.d]# ls
[root@controller yum.repos.d]# 
[root@controller yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0  12412      0 --:--:-- --:--:-- --:--:-- 12351
[root@controller yum.repos.d]# 
[root@controller yum.repos.d]# ls
CentOS-Base.repo
[root@controller yum.repos.d]# sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
[root@controller yum.repos.d]# 
[root@controller yum.repos.d]# cd
[root@controller ~]# yum -y install https://mirrors.aliyun.com/epel/epel-release-latest-8.noarch.rpm
...
Installed:
  epel-release-8-19.el8.noarch                                                                                     
Complete!
[root@controller ~]# sed -i 's|^#baseurl=https://download.example/pub|baseurl=https://mirrors.aliyun.com|' /etc/yum.repos.d/epel*
[root@controller ~]# sed -i 's|^metalink|#metalink|' /etc/yum.repos.d/epel*
[root@controller ~]# 

安装依赖包

[root@controller ~]# yum -y install wget perl git curl openssh-server openssh-clients postfix cronie
...
  perl-parent-1:0.237-1.el8.noarch                                perl-podlators-4.11-1.el8.noarch                 
  perl-threads-1:2.21-2.el8.x86_64                                perl-threads-shared-1.58-2.el8.x86_64             
  postfix-2:3.5.8-2.el8.x86_64                                                           
Complete!
[root@controller ~]# 

将postfix服务设置为开机自启,并查看端口

[root@controller ~]# systemctl enable --now postfix
Created symlink /etc/systemd/system/multi-user.target.wants/postfix.service → /usr/lib/systemd/system/postfix.service.
[root@controller ~]# 
[root@controller ~]# ss -anlt
State          Recv-Q         Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              100                          127.0.0.1:25                          0.0.0.0:*                            
LISTEN         0              128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              100                              [::1]:25                             [::]:*                            
LISTEN         0              128                               [::]:22                             [::]:*                            
[root@controller ~]# 

下载gitlab软件包和policycoreutils-python依赖包

[root@controller ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm
...
gitlab-ce-16.4.1-ce.0.el7.x86_64. 100%[===========================================================>]   1.21G  6.33MB/s    in 3m 30s  
2023-10-07 15:09:41 (5.89 MB/s) - ‘gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm’ saved [1295805808/1295805808]
[root@controller ~]# 
[root@controller ~]# ls
anaconda-ks.cfg  gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm
[root@controller ~]# 
[root@controller ~]# wget http://mirror.centos.org/centos/7/os/x86_64/Packages/policycoreutils-python-2.5-34.el7.x86_64.rpm
...
policycoreutils-python-2.5-34.el7 100%[===========================================================>] 457.34K  35.4KB/s    in 15s     
2023-10-07 15:14:06 (31.4 KB/s) - ‘policycoreutils-python-2.5-34.el7.x86_64.rpm’ saved [468316/468316]
[root@controller ~]# 
[root@controller ~]# ls
anaconda-ks.cfg  gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm  policycoreutils-python-2.5-34.el7.x86_64.rpm
[root@controller ~]# 

先安装 policycoreutils-python依赖包再安装gitlab软件包
由于policycoreutils依赖包会与policycoreutils-python依赖包冲突先将其卸载

[root@controller ~]# ls
anaconda-ks.cfg  gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm  policycoreutils-python-2.5-34.el7.x86_64.rpm
[root@controller ~]# 
[root@controller ~]# yum -y remove policycoreutils
[root@controller ~]# 
[root@controller ~]# rpm -ivh policycoreutils-python-2.5-34.el7.x86_64.rpm --nodeps
warning: policycoreutils-python-2.5-34.el7.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID f4a80eb5: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:policycoreutils-python-2.5-34.el7################################# [100%]
[root@controller ~]# 
[root@controller ~]# rpm -ivh gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm
warning: gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Verifying...                          ################################# [100%]
Preparing...                          ################################# [100%]
Updating / installing...
   1:gitlab-ce-16.4.1-ce.0.el7        ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
     _______ __  __          __
    / ____(_) /_/ /   ____ _/ /_
   / / __/ / __/ /   / __ `/ __ \
  / /_/ / / /_/ /___/ /_/ / /_/ /
  \____/_/\__/_____/\__,_/_.___/

Thank you for installing GitLab!
...
Help us improve the installation experience, let us know how we did with a 1 minute survey:
https://gitlab.fra1.qualtrics.com/jfe/form/SV_6kVqZANThUQ1bZb?installation=omnibus&release=16-4
[root@controller ~]# 

出现狐狸界面说明安装成功

修改gitlab.rb配置文件,将external_url这一行后面修改为服务器的ip地址

[root@controller ~]# vim /etc/gitlab/gitlab.rb 
external_url 'http://192.168.200.10'

安装libselinux-utils依赖包,缺少这个依赖包会导致后面重载配置文件时报错

[root@controller ~]# yum provides *bin/getenforce
Last metadata expiration check: 0:20:17 ago on Sat 07 Oct 2023 03:27:33 PM CST.
libselinux-utils-2.9-5.el8.x86_64 : SELinux libselinux utilities
Repo        : base
Matched from:
Other       : *bin/getenforce
[root@controller ~]# 
[root@controller ~]# rpm -qa | grep libselinux-utils
[root@controller ~]# 
[root@controller ~]# yum -y install libselinux-utils
Last metadata expiration check: 0:21:30 ago on Sat 07 Oct 2023 03:27:33 PM CST.
...
Installed:
  libselinux-utils-2.9-5.el8.x86_64                                                                                 
Complete!
[root@controller ~]# 

重载gitlab配置文件,最后出现gitlab Reconfigured! 则表示成功

[root@controller ~]# gitlab-ctl reconfigure
...
Notes:
Default admin account has been configured with following details:
Username: root
Password: You didn't opt-in to print initial root password to STDOUT.
Password stored to /etc/gitlab/initial_root_password. This file will be cleaned up in first reconfigure run after 24 hours.
NOTE: Because these credentials might be present in your log files in plain text, it is highly recommended to reset the password following https://docs.gitlab.com/ee/security/reset_user_password.html#reset-your-root-password.
gitlab Reconfigured!
[root@controller ~]#

查看端口

[root@controller ~]# ss -anlt
State          Recv-Q         Send-Q                   Local Address:Port                   Peer Address:Port         Process         
LISTEN         0              1024                         127.0.0.1:8151                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:8153                        0.0.0.0:*                            
LISTEN         0              100                          127.0.0.1:25                          0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:8154                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:8155                        0.0.0.0:*                            
LISTEN         0              128                          127.0.0.1:8092                        0.0.0.0:*                            
LISTEN         0              511                            0.0.0.0:8060                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9121                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9090                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9187                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9093                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9100                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:9229                        0.0.0.0:*                            
LISTEN         0              128                          127.0.0.1:9168                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:8080                        0.0.0.0:*                            
LISTEN         0              511                            0.0.0.0:80                          0.0.0.0:*                            
LISTEN         0              128                          127.0.0.1:8082                        0.0.0.0:*                            
LISTEN         0              128                          127.0.0.1:9236                        0.0.0.0:*                            
LISTEN         0              1024                         127.0.0.1:8150                        0.0.0.0:*                            
LISTEN         0              128                            0.0.0.0:22                          0.0.0.0:*                            
LISTEN         0              100                              [::1]:25                             [::]:*                            
LISTEN         0              1024                                 *:9094                              *:*                            
LISTEN         0              128                              [::1]:9168                           [::]:*                            
LISTEN         0              128                               [::]:22                             [::]:*                            
[root@controller ~]# 

重启gitlab服务

[root@controller ~]# gitlab-ctl restart
ok: run: alertmanager: (pid 12975) 1s
ok: run: gitaly: (pid 12985) 0s
ok: run: gitlab-exporter: (pid 13003) 1s
ok: run: gitlab-kas: (pid 13014) 0s
ok: run: gitlab-workhorse: (pid 13024) 0s
ok: run: logrotate: (pid 13036) 1s
ok: run: nginx: (pid 13042) 0s
ok: run: node-exporter: (pid 13050) 0s
ok: run: postgres-exporter: (pid 13056) 1s
ok: run: postgresql: (pid 13066) 1s
ok: run: prometheus: (pid 13075) 0s
ok: run: puma: (pid 13091) 1s
ok: run: redis: (pid 13096) 0s
ok: run: redis-exporter: (pid 13103) 0s
ok: run: sidekiq: (pid 13112) 1s
[root@controller ~]# 

查看gitlab版本号

[root@controller ~]# head -1 /opt/gitlab/version-manifest.txt
gitlab-ce 16.4.1
[root@controller ~]# 

访问网页

image-20231007160247552

四、gitlab修改登录密码操作

方法一、使用初始密码登录gitlab,并修改密码

查看initial_root_password配置文件,使用Password后面的初始密码登录gitlab

[root@controller ~]# cd /etc/gitlab/
[root@controller gitlab]# ls
gitlab.rb  gitlab-secrets.json  initial_root_password  trusted-certs
[root@controller gitlab]# 
[root@controller gitlab]# cat initial_root_password 
...
Password: orG859XwWfaRF71sQ5OAY2GCx2t8ITNSttKL2/ZkrfQ=
# NOTE: This file will be automatically deleted in the first reconfigure run after 24 hours.
[root@controller gitlab]# 

image-20231007160531178

成功登录

image-20231007160740995

更改管理员密码

输入你想要设置的密码保存即可

输入你更改后的密码登录

image-20231007161327952

image-20231007161354733

方法二、破解管理员密码

在不知道有初始密码,无法登录gitlab的情况下,使用破解管理员密码的方法修改密码登录gitlab

id为1的是超级管理员
密码必须至少8个字符

[root@controller ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
 Ruby:         ruby 3.0.6p216 (2023-03-30 revision 23a532679b) [x86_64-linux]
 GitLab:       16.4.1 (e6801ed8d44) FOSS
 GitLab Shell: 14.28.0
 PostgreSQL:   13.11
------------------------------------------------------------[ booted in 25.50s ]
Loading production environment (Rails 7.0.6)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
irb(main):002:0> user.password = 'redhat123!'
=> "redhat123!"
irb(main):003:0> user.password_confirmation = 'redhat123!'
=> "redhat123!"
irb(main):004:0> user.save
=> true
irb(main):005:0> quit
[root@controller ~]# 

使用破解时修改的密码登录gitlab

image-20231007162248103

五、项目管理

创建一个空项目

ip地址后面选择root用户

访问级别选择internal

项目创建成功过后会显示一个帮助文档,可以上传一些文件到项目中

image-20231007162935260

上传项目文件

将已有的仓库克隆到本地

[root@controller ~]# git config --global user.name "root"         
[root@controller ~]# git config --global user.email "123@qq.com"       
[root@controller ~]# git clone http://192.168.200.10/root/huayu.git
Cloning into 'huayu'...
Username for 'http://192.168.200.10': root
Password for 'http://root@192.168.200.10': 
warning: You appear to have cloned an empty repository.
[root@controller ~]# 
[root@controller ~]# ls
anaconda-ks.cfg  gitlab-ce-16.4.1-ce.0.el7.x86_64.rpm  huayu  policycoreutils-python-2.5-34.el7.x86_64.rpm
[root@controller ~]# 

创建一个主分支和文件,添加并提交该文件

[root@controller ~]# cd huayu/
[root@controller huayu]# 
[root@controller huayu]# git switch --create main
Switched to a new branch 'main'
[root@controller huayu]# 
[root@controller huayu]# echo "hello world" > test
[root@controller huayu]# ls
test
[root@controller huayu]# 
[root@controller huayu]# git add test
[root@controller huayu]# git commit -m "first commit"
[main (root-commit) 9a77365] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 test
[root@controller huayu]# 

获取上传文件

[root@controller huayu]# git push --set-upstream origin main
Username for 'http://192.168.200.10': root
Password for 'http://root@192.168.200.10': 
Enumerating objects: 3, done.
Counting objects: 100% (3/3), done.
Writing objects: 100% (3/3), 206 bytes | 206.00 KiB/s, done.
Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
To http://192.168.200.10/root/huayu.git
 * [new branch]      main -> main
Branch 'main' set up to track remote branch 'main' from 'origin'.
[root@controller huayu]# 


上传成功

六、用户和组管理

创建组

创建用户

用户名和邮箱自定义

用户创建后设置密码

将用户添加到组中

将用户添加到项目中

更改用户权限

切换用户登录

登录后会提示你设置新密码

使用新密码登录

在项目中添加文件

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐