gitlab理论、配置、常用命令、邮件系统、数据备份、忘记密码、权限管理、项目管理

gitlab服务

服务概述

服务介绍

gitlab作为一个开源项目开始帮助团队在团建开发上进行合作。通过以业界领先的步伐交付新的功能为整个软件开发和操作生命周期提供了一个单独的应用平台。
gitlab提供了管理、计划、创建、验证、打包、发布、配置、监视和保护应用程序所需的一切。
gitlab是一个基于git实现的在线代码仓库托管软件,一般用于企业、学校等内部网络搭建git私服。gitlab是一个提供代码托管、提交审核和问题跟踪的代码管理平台。gitlab分区社区版(CE)和企业版(EE)。配置建议CPU2核心,内存4G以上。

服务组成

nginx:静态web服务器

gitlab-shell:用于处理git命令和修改authorized keys列表(ruby)

gitlab-workhorse:轻量级反向代理服务器(go)

gitlab-workhorse是一个敏捷的反向代理。它会处理一些大的HTTP请求,比如文件上传、文件下载、git push/pull 和git包下载。其他请求会反向代理到gitlab rails应用,即反向代理 给后端的unicorn

logrotate:日志文件管理工具

postgresql:数据库

redis:缓存数据库

sidekiq:用于在后台执行队列任务(异步执行) (ruby)

unicorn: 用ruby编写的一个HTTP服务器,gitlab rails应用是托管在这个服务器上

功能介绍

  • 基于web方式的管理平台
  • 拥有完善的权限控制和身份认证模块
  • 问题追踪和代码质量管理
  • 代码review功能
  • 文档管理、知识库管理
  • 自身集成CI/CD,实现快速迭代

安装和运行

安装基础组件

[root@localhost ~]# yum -y install curl openssh-server openssh-clients postfix cronie policycoreutils-python

配置yum并安装gitlab-ce

下载并运行官方脚本(添加yum源)

curl -s https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | bash

yum安装gitlab-ce

yum -y install gitlab-ce-11.0.3

修改配置文件

将地址修改为自己想要的地址

[root@localhost ~]# vim /etc/gitlab/gitlab.rb

external_url 'http://gitlab.bdqn.cn'

在hosts文件写入域名解析

vim /etc/hosts
192.168.43.74 gitlab.bdqn.cn

重新配置

gitlab-ctl reconfigure

启动运行gitlab

[root@localhost ~]# gitlab-ctl start

启动gitlab服务需要关闭80端口的服务,启动完成后使用浏览器访问本机IP,第一次访问会强制设定新密码,密码设定完成后可登陆控制面板
访问gitlab
firefox http://gitlab.bdqn.cn

常用的管理命令

  • gitlab-ctl start 启动所有gitlab组件
  • gitlab-ctl stop 停止所有gitlab组件
  • gitlab-ctl restart 重启所有gitlab组件
  • gitlab-ctl status 查看服务状态
  • gitlab-ctl reconfigure 重新编译gitlab的配置
  • gitlab-ctl tail 查看日志 gitlab-ctl tail nginx/gitlab_access.log

常用配置

配置gitlab邮件功能

启动已安装的postfix

systemctl start postfix

修改gitlab配置文件

vim /etc/gitlab/gitlab.rb

 53  gitlab_rails['gitlab_email_from'] = '[email protected]'

 456  gitlab_rails['smtp_enable'] = true
 457  gitlab_rails['smtp_address'] = "smtp.163.com"
 458  gitlab_rails['smtp_port'] = 25
 459  gitlab_rails['smtp_user_name'] = "[email protected]"
 460  gitlab_rails['smtp_password'] = "sqm186111"
 461  gitlab_rails['smtp_domain'] = "163.com"
 462  gitlab_rails['smtp_authentication'] = "login"
 463  gitlab_rails['smtp_enable_starttls_auto'] = true
 464  gitlab_rails['smtp_tls'] = false

 602  user['git_user_email'] = "[email protected]"

修改完之后需要使用gitlab-ctl reconfigure重新配置,再重新启动gitlab: gitlab-ctl restart

测试邮箱是否配置好

[root@localhost ~]# gitlab-rails console
-------------------------------------------------------------------------------------
GitLab:       11.0.3 (aa62075)
GitLab Shell: 7.1.4
postgresql:   9.6.8
-------------------------------------------------------------------------------------
Loading production environment (Rails 4.2.10)
irb(main):001:0> Notify.test_email('[email protected]', 'Test email', '此处略去一万字').deliver_now
Notify#test_email: processed outbound mail in 300.5ms

Sent mail to [email protected] (1302.2ms)
Date: Sun, 27 Jan 2019 17:22:57 +0800
From: GitLab <[email protected]>
Reply-To: GitLab <[email protected]>
To: [email protected]
Message-ID: <[email protected]>
Subject: Test email
Mime-Version: 1.0
Content-Type: text/html;
charset=UTF-8
Content-Transfer-Encoding: quoted-printable
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www=
.w3.org/TR/REC-html40/loose.dtd">
<html><body><p>=E6=AD=A4=E5=A4=84=E7=95=A5=E5=8E=BB=E4=B8=80=E4=B8=87=E5=AD=
=97</p></body></html>

=> #<Mail::Message:70169145486580, Multipart: false, Headers: <Date: Sun, 27 Jan 2019 17:22:57 +0800>, <From: GitLab <[email protected]>>, <Reply-To: GitLab <[email protected]>>, <To: [email protected]>, <Message-ID: <[email protected]>>, <Subject: Test email>, <Mime-Version: 1.0>, <Content-Type: text/html; charset=UTF-8>, <Content-Transfer-Encoding: quoted-printable>, <Auto-Submitted: auto-generated>, <X-Auto-Response-Suppress: All>>
irb(main):002:0> 

执行完之后出来第二个命令提示符

日常维护

备份

[root@localhost ~]# gitlab-rake gitlab:backup:create

备份文件默认位置:/var/opt/gitlab/backups/,是以时间戳和日期命名的tar包,如下所示。另外,也可将上述任务加入计划任务进行定期备份。

[root@localhost ~]# cd /var/opt/gitlab/backups/
[root@localhost backups]# ls
1548581316_2019_01_27_11.0.3_gitlab_backup.tar

恢复

先停止unicorn和sidekiq,保证数据库没有新的连接,不会写入数据

[root@localhost ~]# gitlab-ctl stop unicorn
ok: down: unicorn: 0s, normally up
[root@localhost ~]# gitlab-ctl stop sidekiq
ok: down: sidekiq: 0s, normally up
[root@localhost ~]# cd /var/opt/gitlab/backups/
[root@localhost backups]# gitlab-rake gitlab:backup:restore BACKUP=1548581316_2019_01_27_11.0.3
Unpacking backup ... done
Before restoring the database, we will remove all existing
tables to avoid future upgrade problems. Be aware that if you have
custom tables in the GitLab database these tables and all data will be
removed.

Do you want to continue (yes/no)? yes
Removing all tables. Press `Ctrl-C` within 5 seconds to abort
Cleaning the database ... 
done
###BACKUP=后边跟的是时间戳和日期

[root@localhost backups]# gitlab-ctl start unicorn
ok: run: unicorn: (pid 13289) 1s
[root@localhost backups]# gitlab-ctl start sidekiq
ok: run: sidekiq: (pid 13343) 0s
[root@localhost backups]# 

忘记root密码

[root@localhost backups]# gitlab-rails console production
-------------------------------------------------------------------------------------
 GitLab:       11.0.3 (aa62075)
 GitLab Shell: 7.1.4
 postgresql:   9.6.8
-------------------------------------------------------------------------------------
Loading production environment (Rails 4.2.10)
irb(main):001:0> user = User.where(id:1).first
=> #<User id:1 @root>
irb(main):002:0> user.password='beidaqingniao'
=> "beidaqingniao"
irb(main):003:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 3cc678cc-4854-46dc-913e-3d1d2d9d726f) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", gid://gitlab/User/1
=> true
irb(main):004:0> exit

>>>irb(main):001:0>是命令提示符,另外,密码长度最短8位<<<

日志的查看

  1. gitlab-ctl tail 查看整体日志
  2. gitlab-ctl tail redis 查看redis的日志
  3. gitlab-ctl tail postgresql 检查postgresql的日志
  4. gitlab-ctl tail workhorse 检查gitlab-workhorse的日志
  5. gitlab-ctl tail logrotate 检查logrotate的日志
  6. gitlab-ctl tail nginx 检查nginx的日志
  7. gitlab-ctl tail sidekiq 检查sidekiq的日志
  8. gitlab-ctl tail unicorn 检查unicorn的

服务使用

用户和组的管理

用户管理

  • 添加用户

    进入后,点击最上边边栏的扳手,点击左侧的user,点击右上角的new user,填写用户名、全名和邮箱(用户名和全名可以设置一样),gitlab会发送邮件并进行验证,其他的先不选,直接点击页面左下角的create,之后在邮箱里点击链接,首次进入会让设置密码,设置之后就可以登陆了。在root登陆的页面再次点击左侧的user,可以看到用户列表里多了lisi

在这里插入图片描述
在这里插入图片描述

用户组管理

  • 添加组

在root登陆界面,点击扳手,点击左侧group,点击右边的new group,在第一行填写group path,第二行会自己填写组名,描述信息可以自己写,其他的可以不写,点击页面左下侧的create
在这里插入图片描述
在这里插入图片描述

  • 向用户组添加成员

在上面的界面中继续操作,即在ios组中操作,点击右侧的第一个空白框,选择lisi用户,在第二个框选择它的角色,然后点击add user to group,之后gitlab会发邮件通知,告诉lisi已经被加入组中,身份是developer

在这里插入图片描述

项目管理

新建项目

点击最上边边栏的+,选择new project,然后进行项目信息的填写和权限的设置,首先点击右上角的空白框,写入项目名称,项目名称前边的空白框可以选择组或者用户名,作为项目url的一部分,可以自己选择一下。下边的项目描述可以根据情况进行填写,可见级别默认即可,点击页面下侧的create,创建完成之后会弹出新的页面,提示你创建完成病且告诉你这个项目的URL是http://gitlab.bdqn.cn/ios/ios.git

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
此处,如果发现项目地址还是以前的而没有改过来,那么可以通过修改配置文件来实现变化,或者gitlab-ctl reconfigure — gitlab-ctl restart
[root@localhost .ssh]# vim /var/opt/gitlab/gitlab-rails/etc/gitlab.yml
[root@localhost .ssh]# vim /var/opt/gitlab/gitlab-rails/etc/gitlab.yml

权限管理

用户管理

点击扳手,点击user,点击lisi后边的edit,即可对lisi用户进行权限的管理,projects limit表示用户可以创建的最大项目数/can create group 用户是否可以创建组/regular 用户为普通用户身份,另:admin为管理员身份,可管理所有;external为外部员工,权限较低,修改完之后点击页面左下侧的保存
在这里插入图片描述

组管理

点击扳手,点击group,在能看到的组的后边点击edit进入组权限管理的页面,group avatar为群头像;private为私有项目,具备访问权限的成员可看;internal表示任何登陆的用户都可以;public表示公开项目,任何人可以访问;allow user to request access表示允许发送访问请求;Large file storage表示允许允许此组访问Git LFS功能;two-factor

在这里插入图片描述

项目权限

扳手—projects—edit,点击左下角的member,可以往项目里添加用户,如果创建项目的时候选择了组则默认该组下的所有成员都具备权限,也可以指定单个用户的开发权限。也可以把这个项目分享给另外的组,点击share with group

在这里插入图片描述
在这里插入图片描述

默认权限控制

扳手—左侧菜单最下角的setting—Visibility and access controls—default branch protection选择合适的权限

在这里插入图片描述
在这里插入图片描述

Linux用户配置公钥

终端生成公钥
在这里插入图片描述

[root@localhost ~]# ssh-keygen -t rsa -C "[email protected]"
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa): 
Created directory '/root/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:hTZpLZlDfGNdGoFRFGX+/bAFaFZGlPbo5garIZpPjyc [email protected]
The key's randomart image is:
+---[RSA 2048]----+
|       .. .**BO. |
|       ..*= .Oo  |
|        @oo.=.oo |
|       o = o  .oo|
|        S    .. +|
|            . o+.|
|        o .  =. .|
|       +E+... o  |
|      o.ooo. .   |
+----[SHA256]-----+
[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ls
id_rsa  id_rsa.pub
[root@localhost .ssh]# cat id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQChKiTep6xU/fK77wauf2G/WTaS3oguL+WbTYjcBd/A6ZL7U8cjTlX5CsBljV2XIWaS5Wx0TnnnA5U8Ly0J8SOFmi81QK/I9caqAzygCjd5/9laQttRFmedPUeLeh0j870IZrgmg5eyvmZl+L67X+qHPVozuDfxInzT+gLrw8iS73DfY59y71Yg4GKcK56tQSzA2JWBc+/QDFLAj9kf9COkxkWgKrFmgmisVVgm87pjv/TgjdFxdXUpcMgWVBWeOAt5/ZbgCHOWil7m2COLEEEk9aZlg8euSaJ3p3FPcR1Kg2pz8KUprgJANGK2Q555Eg+ec62on/LtnIvOcMZBFrLH [email protected]

上传公钥到gitlab上

为了把本地的仓库传到github,还需要配置ssh key

点击页面右上角root用户头像后的下拉三角号,点击Settings,点击左侧菜单的SSH Keys,把刚才cat的内容粘贴到key的框框里,点击add key

在这里插入图片描述

验证: ssh -T [email protected]

[root@localhost .ssh]# ssh -T [email protected]
The authenticity of host 'gitlab.bdqn.cn (192.168.1.145)' can't be established.
ECDSA key fingerprint is SHA256:AAVS/5ORVvMlEZUxXtGwVCj39bPYFHcSmoIrBwgEU4w.
ECDSA key fingerprint is MD5:2f:57:89:03:9d:52:91:0d:9d:b5:6d:f9:ff:1b:2f:3d.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'gitlab.bdqn.cn,192.168.1.145' (ECDSA) to the list of known hosts.
Welcome to GitLab, @root!

可以添加多个key以实现不同终端同时使用

git客户端使用

使用git命令进行全局配置

[root@localhost .ssh]# git config --global user.name "lisi"
[root@localhost .ssh]# git config --global user.email "[email protected]"
[root@localhost .ssh]# git config -l
user.name=lisi
[email protected]

git 是一种开源的版本控制系统,可以高效的管理项目版本。同时也是一个版本控制软件

安装好git后,在命令行或终端中使用下面的命令可以设置git自己的名字和电子邮件。这是因为Git是分布式版本控制系统,所以,每个机器都必须自报家门:你的名字和Email地址

注意git config命令的–global参数,用了这个参数,表示你这台机器上所有的Git仓库都会使用这个配置,当然也可以对某个仓库指定不同的用户名和Email地址

场景一: 全新项目,全新克隆

git clone [email protected]:ios/ios.git 
cd ios
touch README.md
git add README.md
git commit -m "add README"

场景二: 本地已存在代码目录,关联到远程仓库

cd existing_folder
git init
git remote add origin [email protected]:ios/ios.git
git add .
git commit -m "Initial commit"

场景三: 本地已存在代码仓库,备份远程仓库,以本地仓库为基石,关联到远程仓库

cd existing_repo  #进入本地存在的代码仓库
git remote rename origin old-origin #修改一个仓库的简写名,比如把grant修改为gt
git remote add origin [email protected]:ios/ios.git

演示:以实验一为实验,全新克隆完成基础操作

克隆仓库,提交一个测试文件,并且推送到远程master仓库

[root@localhost .ssh]# git config --global user.name "lisi"
[root@localhost .ssh]# git config --global user.email "[email protected]"
[root@localhost ~]# mkdir test
[root@localhost ~]# cd test/
[root@localhost test]# git clone [email protected]:ios/ios.git
Cloning into 'ios'...
warning: You appear to have cloned an empty repository.
[root@localhost test]# cd ios/
[root@localhost ios]# ls -a
.  ..  .git
[root@localhost ios]# touch README.md
[root@localhost ios]# echo 111 >> README.md 
[root@localhost ios]# git add README.md
[root@localhost ios]# git commit -m "test111"
[master (root-commit) fb1aa00] test111
 1 file changed, 1 insertion(+)
 create mode 100644 README.md
[root@localhost ios]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 206 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:ios/ios.git
 * [new branch]      master -> master
[root@localhost ios]# 

注: 一般不直接对master主干进行push,而是应该在开发分支上修改,然后进行合并请求

web端查看提交的文件、说明、文件内容—> 扳手—project—edit—点击左侧菜单project—下拉页面可以看到刚才添加的README.md

关于gitlab报错处理

访问gitlab时报502错误,说服务器巴拉巴拉的

  1. gitlab-ctl status 看一下后没有谁的pid一直在变动,如果有说明相应的端口被占用
  2. 官方的解决办法 gitlab-ctl restart unicorn ; gitlab-ctl restart sidekiq
  3. 使用free -m命令 每隔一两秒就执行一次,看看free的内存是不是越来越少,如果是,则说明gitlab正在启动,稍等片刻在进行访问即可
  4. 检查80端口和8080端口是否被占用

猜你喜欢

转载自blog.csdn.net/weixin_43557605/article/details/87521598
今日推荐