美文网首页
Gitlab server搭建过程

Gitlab server搭建过程

作者: 胖虎喜欢小红 | 来源:发表于2020-01-20 17:52 被阅读0次

一、检查环境

[root@biudefor ~]# systemctl stop firewalld
[root@biudefor ~]# setenforce 0
[root@biudefor ~]# yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python
# 10.x以后开始依赖 policycoreutils-python
[root@biudefor ~]# systemctl start postfix
[root@biudefor ~]# systemctl enable postfix

二、下载安装

1. 下载rpm包
gitlab的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

image.png
鼠标右击,复制链接地址
使用 wget 下载到本地
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-12.6.3-ce.0.el7.x86_64.rpm

2. 安装

[root@biudefor ~]# rpm -ivh gitlab-ce-12.6.3-ce.0.el7.x86_64.rpm
警告:gitlab-ce-12.6.3-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中...                          ################################# [100%]
正在升级/安装...
   1:gitlab-ce-12.6.3-ce.0.el7       ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.r

       *.                  *.
      ***                 ***
     *****               *****
    .******             *******
    ********            ********
   ,,,,,,,,,***********,,,,,,,,,
  ,,,,,,,,,,,*********,,,,,,,,,,,
  .,,,,,,,,,,,*******,,,,,,,,,,,,
      ,,,,,,,,,*****,,,,,,,,,.
         ,,,,,,,****,,,,,,
            .,,,***,,,,
                ,*,.
  


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

Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
  sudo gitlab-ctl reconfigure

For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md

[root@biudefor ~]# 

3、配置gitlab
gitlab的配置文件 /etc/gitlab/gitlab.rb, 编辑如下:
配置时区

gitlab_rails['time_zone'] = 'Asia/Shanghai'

绑定监听的域名或IP

external_url 'http://192.168.1.10'

使用gitlab-ctl reconfigure 自动配置,并安装数据库,初始化信息,如下所示(第一次使用配置时间较长):

[root@biudefor ~]# gitlab-ctl reconfigure

使用gitlab-ctl start 启动gitlab服务,如下所示:

[root@biudefor ~]# gitlab-ctl start
ok: run: gitaly: (pid 22896) 2922s
ok: run: gitlab-monitor: (pid 22914) 2921s
ok: run: gitlab-workhorse: (pid 22882) 2922s
ok: run: logrotate: (pid 22517) 2987s
ok: run: nginx: (pid 22500) 2993s
ok: run: node-exporter: (pid 22584) 2974s
ok: run: postgres-exporter: (pid 22946) 2919s
ok: run: postgresql: (pid 22250) 3047s
ok: run: prometheus: (pid 22931) 2920s
ok: run: redis: (pid 22190) 3053s
ok: run: redis-exporter: (pid 22732) 2962s
ok: run: sidekiq: (pid 22472) 3005s
ok: run: unicorn: (pid 22433) 3011s
[root@biudefor ~]# 
[root@biudefor ~]# lsof -i:80
COMMAND   PID       USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
nginx   22500       root    7u  IPv4  50923      0t0  TCP *:http (LISTEN)
nginx   22501 gitlab-www    7u  IPv4  50923      0t0  TCP *:http (LISTEN)
[root@biudefor ~]# 

3.2 修改配置文件,添加 smtp 邮件功能

假如你想让互联网的邮箱服务提供商,帮你的 gitlab 发送邮件,就需要在配置文件中设置,并且需要在邮件服务提供商那里开通 SMTPPOP3 功能。
关于 SMTP 和 POP3 的区别: 百度一下,你就知道
简单说一下, SMTP 用于发邮件, POP3 用于收邮件。

这里我以 163 邮箱为例,演示一下

  1. 首先我们在邮箱服务提供商那里开通 SMTP 。


    image.png
image.png image.png
  1. 配置系统使用的发件邮箱地址等信息
    在配置文件: /etc/gitlab/gitlab.rb 中做如下修改
    这部分,不管是使用postmail或者SMTP都需要做如下的配置。
# 是否开启系统邮箱,默认开启
gitlab_rails['gitlab_email_enabled'] = true

# 用这个账号去发送邮件
gitlab_rails['gitlab_email_from'] = 'my@163.com'

# 发送邮件中要显示的发件人名称
gitlab_rails['gitlab_email_display_name'] = 'Admin'

# 系统接收邮件的地址,一般系统发送的邮件都不要对其回复邮件,生产中如果需要,那也肯定不是你的邮箱
gitlab_rails['gitlab_email_reply_to'] = 'noreply@example.com'

# 邮件的标题前缀
gitlab_rails['gitlab_email_subject_suffix'] = '[gitlab]'
  1. 配置完系统的发件信息,接下来设置邮件服务提供商的账户登录验证信息。
    要想使用邮件服务商为 gitlab 系统发送邮件,是需要进行登录, 登录就需要先验证的。
    所以要填写相关用于登录验证的信息
    值的注意的是,上面 gitlab_email_from 的账户必须和这里的用户名(smtp_user_name)一致。
gitlab_rails['smtp_enable'] = true

# 这个配置的密码不是邮箱的登陆密码而是网易邮箱的客户端授权密码, 
# 在网易邮箱web页面的设置-POP3/SMTP/IMAP-客户端授权密码查看。
# 见下图
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"     //stmp地址
gitlab_rails['smtp_port'] = 25   //不加密

# 使用这个账号发送邮件
gitlab_rails['smtp_user_name'] = "my@163.com"       //SMTP用户
gitlab_rails['smtp_password'] = "xxx"              //SMTP密码
gitlab_rails['smtp_domain'] = "163.com"            //域名
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_openssl_verify_mode'] = 'peer'

# 发件人账号
user['git_user_email'] = "my@163.com"
# 更多查看下方网址
# https://docs.gitlab.com/omnibus/settings/smtp.html

# 如果你不配置发件人, 有些邮件服务器会发送失败, 
# 所以我们最好把账号和发件人都配置了, 并且保持一致, 这样保证兼容问题
注意:收件人的SMTP也要处于开启状态
  1. 重器相关服务,使最新的配置文件生效
[root@biudefor ~]# gitlab-ctl reconfigure

# 再重启一下
[root@biudefor ~]# gitlab-ctl restart

使用gitlab-rails console命令进行发送邮件测试,如下所示:

[root@biudefor ~]# gitlab-rails console
irb(main):001:0> Notify.test_email('收件人邮箱', '邮件标题', '邮件正文').deliver_now

示例:

[root@biudefor ~]# gitlab-rails console 
Loading production environment (Rails 4.2.10)
irb(main):001:0>  Notify.test_email('12345678@qq.com', 'Message Subject', 'Message Body').deliver_now
...
irb(main):002:0>quit
[root@biudefor ~]# 

4、gitlab的使用

在浏览器中输入git_lab机器的ip ,然后 change password: ,并使用root用户登录 即可 (后续动作根据提示操作)

修改密码也可以:gitlab-rails console production
irb(main):001:0> user = User.where(id: 1).first // id为1的是超级管理员
irb(main):002:0>user.password = 'yourpassword' // 密码必须至少8个字符

irb(main):003:0>user.save! // 如没有问题 返回true
exit // 退出

1.png

如果需要修改默认监听端口 ,可以在gitlab.rb中设置 nginx['listen_port'] = 8000 ,然后再次 gitlab-ctl reconfigure即可

登录gitlab

用户名为 :root
密码 :是你刚才设定的密码

image.png
sign in 登陆 image.png

创建 group ,组名为plat-sp ,如下所示:

image.png image.png image.png

去掉用户的自动注册功能:

admin area -> settings -> Sign-up Restrictions 去掉钩钩,然后拉到最下面保存,重新登录
将浏览器页面放大会出现一个小扳手,如果没有放大就需要点击More里的admin area


image.png
image.png
image.png
image.png

创建用户Tompson如下所示:

image.png
image.png
image.png
image.png
image.png
image.png
同样的方法,再创建Eric 、Hellen 用户,注意邮箱不能重复。用户添加完毕后,gitlab会给用户发一封修改密码的邮件,各用户需要登录自己的邮箱,并点击相关的链接,设置新密码。 image-20180627191851344.png
将用户添加到组中,并指定Tompson为本组的owner:
image.png image.png
同样的方法将用户Eric、Hellen也添加到组中,并指定他们为Developer:
image.png
使用Tompson用户的身份与密码登录到gitlab界面中,并创建Project ,如下所示:
如果你的邮箱是假的,就需要给tompson设置一个密码
image.png
image.png
image.png
登入时,都会要求设置新密码,管理员指定的密码仅为初始密码
image.png
设置密码后需要使用新密码重新登录
image.png image.png
image.png
image.png
image.png

为项目创建Dev分支,如下所示:

首先需要先给项目创建一个新的文件


image.png image.png
image.png
image.png
image.png
image.png
在 client 上添加Tompson等 用户:
[root@client ~]# useradd Tompson
[root@client ~]# useradd Eric
[root@client ~]# useradd Hellen
[root@client ~]# passwd Tompson
Changing password for user Tompson.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@client ~]# su - Tompson
Last login: Mon Jan 20 17:38:36 CST 2020 on pts/0
[Tompson@client ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/Tompson/.ssh/id_rsa): 
Created directory '/home/Tompson/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/Tompson/.ssh/id_rsa.
Your public key has been saved in /home/Tompson/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:A6klbnJAWqtKaqu0KMeH0SkZTtgtwACfuXfImtNhy0A Tompson@client
The key's randomart image is:
+---[RSA 2048]----+
|* o              |
|.* +   .         |
|.+E.. +          |
|.o==.= .         |
|.++=@.. S        |
|+ =%o+   .       |
|o++++            |
|= *..            |
|=+ .             |
+----[SHA256]-----+
[Tompson@client ~]$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDBaqvaiIK1O+retx1dsHj6R0fQ/R+1SUlA26Ko5vo9SLExChAH6emZsUprJWWWf00lqAIxRNK8ZYvnb4D4aAOtPrDZMAkcdLE9ckWUp3FsndCQ/6r9vJhAadCuClYOiAxTwcbSrzgthzyCOu5KuzKMkyntNiyDXost36MHTrZG6Mrlp2rv4eAiHo/2IlddFiVgbkzMUwX6gXkhMM82537/crwIiW75pVVWKGv/yo6gJibZwomIuyHYql6E3ML8Ss1AB6rz+G2P66rr+Y+cNq01P2c65MWfgBJPGg/K4EPoUXZIgjsCXGppMZqYkRufIgNOOAZAD4C4mDqIndkw8+EH Tompson@client

将Tompson的公钥复制到gitlab中: 使用Tompson用户的身份与密码登录到gitlab界面中,然后在ssh-key中添加 相关的key ,如下所示:

image.png
image.png
image.png
然后将这一行进行复制
image.png

为Tompson用户配置git ,如下所示:

[Tompson@client ~]$ git config --global user.name "Tompson"
[Tompson@client ~]$ git clone git@192.168.0.104:plat-sp/chathall.git
Cloning into 'chathall'...
The authenticity of host '192.168.0.104 (192.168.0.104)' can't be established.
ECDSA key fingerprint is SHA256:1Jr6AMpwmBmKjpMQYcFU+VCLthGiGEh7pfLY5eLuB7I.
ECDSA key fingerprint is MD5:75:4c:63:e1:69:0c:3e:5b:b4:98:e2:13:9a:e0:ca:cf.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.0.104' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[Tompson@client ~]$ ls
chathall
[Tompson@client ~]$ cd chathall/
[Tompson@client chathall]$ ls
readme

创建一个新文件,添加内容,并提交到master分支:

[Tompson@client chathall]$ vi test.sh
[Tompson@client chathall]$ cat test.sh 
#!/bin/bash
echo "gitlab test"
[Tompson@client chathall]$ git add .
[Tompson@client chathall]$ git commit -m "add test.sh"
[master b5a4ffc] add test.sh
 1 file changed, 2 insertions(+)
 create mode 100644 test.txt

[Tompson@client chathall]$ git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 307 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.0.104:plat-sp/chathall.git
   64287dc..b5a4ffc  master -> master
Branch master set up to track remote branch master from origin.
image.png
使用Eric用户登录,并clone 项目,如下所示:
[root@client ~]# passwd Eric
Changing password for user Eric.
New password: 
BAD PASSWORD: The password is a palindrome
Retype new password: 
passwd: all authentication tokens updated successfully.
[root@client ~]# su - Eric
[Eric@client ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/Eric/.ssh/id_rsa): 
Created directory '/home/Eric/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/Eric/.ssh/id_rsa.
Your public key has been saved in /home/Eric/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:txIRdGd1HuNbhy+DaiOvPf7xkXdajqtrc82NQk03HE0 Eric@client
The key's randomart image is:
+---[RSA 2048]----+
|       .o . o..+E|
|         o o  .=+|
|        .     o.=|
|         .   ..+=|
|        S . .oooo|
|         o o. .+ |
|        o =.. o+=|
|         *..+ooB=|
|        .o+++*=..|
+----[SHA256]-----+
[Eric@client ~]$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC8LdQGs/0KByve6Tq/x+9H5XjCLFkWMCqLxW2ipRj/oH2kwYW6HNjHu60y/Ysxsd44FAE/MBXbN2CbGx5aXE0ca6I+sLZgcN/8Ra9fU+t8Ht5c6m8jAuQJGX+ecdRmk0UHq+n4vS9INcqojl515n50jwGKlU6F31ZfCmbV+iXQtvEPmdrUfg7exm7XR+goBt+2GpXhVg1KuHxXRd3OFlTkh94bquo7MG559mzf6f6HqoeDyHXjXMXzHhHZ2q1v3eNrDEwlNwya0Wi6x+jU8fZWaLPnwVilunoUI6hEt3b1D++pWSJWTlXF44TdQRcWCVY/9OIxRZ9mhqZh6D9zLsKz Eric@client

同样需要使用Eric用户登录gitlab web 界面,并添加相应的ssh-key。然后设置git ,并clone项目:


image.png

添加完之后:

[Eric@client ~]$ git config --global user.name "Eric"
[Eric@client ~]$ git clone git@192.168.0.104:plat-sp/chathall.git
Cloning into 'chathall'...
The authenticity of host '192.168.0.104 (192.168.0.104)' can't be established.
ECDSA key fingerprint is SHA256:1Jr6AMpwmBmKjpMQYcFU+VCLthGiGEh7pfLY5eLuB7I.
ECDSA key fingerprint is MD5:75:4c:63:e1:69:0c:3e:5b:b4:98:e2:13:9a:e0:ca:cf.
Are you sure you want to continue connecting (yes/no)? yes   
Warning: Permanently added '192.168.0.104' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 6, done.
remote: Counting objects: 100% (6/6), done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (6/6), done.
[Eric@client ~]$ ls
chathall

切换到dev分支,修改文件内容,并将新code提交到dev分支(Developer角色默认并没有提交master的权限):

[Eric@client chathall]$ cd chathall
[Eric@client chathall]$ git checkout dev 
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[Eric@client chathall]$ ls
readme  test.sh
[Eric@client chathall]$ vim test.sh 
[Eric@client chathall]$ cat test.sh 
#!/bin/bash
echo "gitlab test"
echo "brahch test"
[Eric@client chathall]$ git add . 
[Eric@client chathall]$ git commit -m '201805101658'
[dev 6687039] 201805101658
 1 file changed, 1 insertion(+)
[Eric@vm2 chathall]$ git push -u origin dev 
Counting objects: 5, done.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 306 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for dev, visit:
remote:   http://192.168.60.119/plat-sp/chathall/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote: 
To git@192.168.60.119:plat-sp/chathall.git
   80edf6b..6687039  dev -> dev
Branch dev set up to track remote branch dev from origin.
[Eric@client chathall]$
[Eric@client chathall]$ git checkout master 
Switched to branch 'master'
[Eric@client chathall]$ git branch 
  dev
* master
[Eric@client chathall]$

使用Eric 用户登录gitlab web,在界面中 创建一个合并请求:

20482597-b1b6485fa0efaa74[1].png 16.png

然后使用Tompson用户登录 gitlab web ,找到“合并请求” ,然后将dev分支合并到master分支,如下所示:

17.png 18.png

三、大部分公司的处理流程如下

PM在gitlab创建任务,分配给开发人员
开发人员领取任务后,在本地使用git clone拉取代码库
开发人员创建开发分支(git checkout -b dev),并进行开发
开发人员完成之后,提交到本地仓库(git commit )
开发人员接着把本地仓库的 dev 分支提交到远程仓库的 dev 分支
开发人员在gitlab登录自己的账户,在gitlab界面上申请分支合并请求(Merge request)
PM在gitlab上查看提交和代码修改情况,确认无误后,确认将开发人员的分支合并到主分支(master)
开发人员在gitlab上Mark done确认开发完成,并关闭issue。这一步在提交合并请求时可以通过描述中填写"close #1"等字样,可以直接关闭issue

四、Git基本常用命令

git init
git add XX
git commit -m "XX"
git status
git diff  XX 
git log
git reset  --hard HEAD^ 
git reset  --hard HEAD~
git reset --hard HEAD~100
git reflog
git checkout -- XX
git rm XX 
git remote add origin https://github.com/RTplay/testgit.git
git push -u(第一次要用-u 以后不需要) origin master 
git clone https://github.com/RTplay/testgit.git
git checkout -b dev
git branch
git checkout master
git merge dev
git branch -d dev
git branch name
git stash
git stash list
git stash apply
git stash drop
git stash pop
git remote
git remote -v
git push origin master
git tag "tag_name"
git push -u origin "tag_name"

五、 其他功能

设置通告
image.png
image.png

自定义登录首页

image.png image.png

相关文章

网友评论

      本文标题:Gitlab server搭建过程

      本文链接:https://www.haomeiwen.com/subject/uwxczctx.html