gitlab:搭建与基本使用

发布时间:2024-02-10 15:30

一、git、github、gitlab的区别
Git是版本控制系统,Github是在线的基于Git的代码托管服务。
GitHub是2008年由Ruby on Rails编写而成。GitHub同时提供付费账户和免费账户。这两种账户都可以创建公开的代码仓库,只有付费账户可以创建私有的代码仓库。
Gitlab解决了这个问题, 可以在上面创建免费的私人repo。
二、gitlab server搭建过程

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

gitlab的下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/
安装gitlab:

[root@vm1 ~]# rpm -ivh gitlab-ce-10.7.2-ce.0.el7.x86_64.rpm 
warning: gitlab-ce-10.7.2-ce.0.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID f27eab47: NOKEY
Preparing...                          ################################# [100%]
Updating / installing...
   1:gitlab-ce-10.7.2-ce.0.el7        ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.

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

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

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@vm1 ~]#

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

[root@vm1 ~]# vim /etc/gitlab/gitlab.rb 
[root@vm1 ~]# grep \"^external_url\" /etc/gitlab/gitlab.rb
external_url \'http://192.168.60.119\'   绑定监听的域名或IP(企业中最好是域名)
[root@vm1 ~]# 

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

[root@vm1 ~]# gitlab-ctl reconfigure   <生成 /var/opt/gitlab/ 各种子配置, 该命令建议仅在初始化时使用一次> 
.....

注: 第一次初始化后会产生当前gitlab的配置文件 /var/opt/gitlab/gitlab-rails/etc/gitlab.yml ,在后期维护的过程中尽量配置 /var/opt/gitlab/gitlab-rails/etc/gitlab.yml 文件,然后gitlab-ctl restart 即可生效新的配置;如果没有必要不建议做第二次初始化。
使用gitlab-ctl start 启动gitlab服务,如下所示:

[root@vm1 ~]# 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@vm1 ~]# 
[root@vm1 ~]# 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@vm1 ~]# 

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

[root@vm1 ~]# vim /etc/gitlab/gitlab.rb
[root@vm1 ~]# grep -P \"^[^#].*smtp_|user_email|gitlab_email\" /etc/gitlab/gitlab.rb
gitlab_rails[\'gitlab_email_enabled\'] = true
gitlab_rails[\'gitlab_email_from\'] = \'username@domain.cn\'
gitlab_rails[\'gitlab_email_display_name\'] = \'Admin\'
gitlab_rails[\'gitlab_email_reply_to\'] = \'usernamei@domain.cn\'
gitlab_rails[\'gitlab_email_subject_suffix\'] = \'[gitlab]\'
gitlab_rails[\'smtp_enable\'] = true
gitlab_rails[\'smtp_address\'] = \"smtp.exmail.qq.com\"
gitlab_rails[\'smtp_port\'] = 25 
gitlab_rails[\'smtp_user_name\'] = \"username@domain.cn\"
gitlab_rails[\'smtp_password\'] = \"password\"
gitlab_rails[\'smtp_domain\'] = \"domain.cn\"
gitlab_rails[\'smtp_authentication\'] = \"login\"
gitlab_rails[\'smtp_enable_starttls_auto\'] = true
gitlab_rails[\'smtp_tls\'] = false
user[\'git_user_email\'] = \"username@domain.cn\"

[root@vm1 ~]# gitlab-ctl reconfigure
......

[root@vm1 ~]# gitlab-ctl stop
ok: down: gitaly: 0s, normally up
ok: down: gitlab-monitor: 1s, normally up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: logrotate: 1s, normally up
ok: down: nginx: 0s, normally up
ok: down: node-exporter: 1s, normally up
ok: down: postgres-exporter: 0s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 0s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 1s, normally up
ok: down: sidekiq: 0s, normally up
ok: down: unicorn: 1s, normally up

[root@vm1 ~]# gitlab-ctl start
ok: run: gitaly: (pid 37603) 0s
ok: run: gitlab-monitor: (pid 37613) 0s
ok: run: gitlab-workhorse: (pid 37625) 0s
ok: run: logrotate: (pid 37631) 0s
ok: run: nginx: (pid 37639) 1s
ok: run: node-exporter: (pid 37644) 0s
ok: run: postgres-exporter: (pid 37648) 1s
ok: run: postgresql: (pid 37652) 0s
ok: run: prometheus: (pid 37660) 1s
ok: run: redis: (pid 37668) 0s
ok: run: redis-exporter: (pid 37746) 0s
ok: run: sidekiq: (pid 37750) 1s
ok: run: unicorn: (pid 37757) 0s

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

[root@vm1 ~]# gitlab-rails console 
Loading production environment (Rails 4.2.10)
irb(main):001:0>  Notify.test_email(\'user@destination.com\', \'Message Subject\', \'Message Body\').deliver_now

Notify#test_email: processed outbound mail in 2219.5ms

Sent mail to user@destination.com (2469.5ms)
Date: Fri, 04 May 2018 15:50:10 +0800
From: Admin 
Reply-To: Admin 
To: user@destination.com
Message-ID: <5aec10b24cfaa_93933fee282db10c162d@vm1.mail>
Subject: Message Subject
Mime-Version: 1.0
Content-Type: text/html;
 charset=UTF-8
Content-Transfer-Encoding: 7bit
Auto-Submitted: auto-generated
X-Auto-Response-Suppress: All


Message Body

=> #, >, >, , >, , , , , , > irb(main):002:0>quit [root@vm1 ~]#

三、gitlab的使用
在浏览器中输入 http://192.168.60.119/ ,然后 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 // 退出
\"gitlab:搭建与基本使用_第1张图片\"

如果需要手工修改nginx的port ,可以在gitlab.rb中设置 nginx[\'listen_port\'] = 8000 ,然后再次 gitlab-ctl reconfigure即可

登录gitlab 如下所示:
\"gitlab:搭建与基本使用_第2张图片\"

创建 group ,组名为plat-sp ,如下所示:
\"gitlab:搭建与基本使用_第3张图片\"

\"gitlab:搭建与基本使用_第4张图片\"

去掉用户的自动注册功能:
admin are -> settings -> Sign-up Restrictions 去掉钩钩,然后拉到最下面保存,重新登录
\"gitlab:搭建与基本使用_第5张图片\"

创建用户Tompson如下所示:
\"gitlab:搭建与基本使用_第6张图片\"
同样的方法,再创建Eric 、Hellen 用户。用户添加完毕后,gitlab会给用户发一封修改密码的邮件,各用户需要登录自己的邮箱,并点击相关的链接,设置新密码。

将用户添加到组中,并指定Tompson为本组的owner:
\"gitlab:搭建与基本使用_第7张图片\"

同样的方法将用户Eric、Hellen也添加到组中,并指定他们为Developer:
\"gitlab:搭建与基本使用_第8张图片\"

使用Tompson用户的身份与密码登录到gitlab界面中,并创建Project ,如下所示:
\"gitlab:搭建与基本使用_第9张图片\"

指定项目的存储路径和项目名称,如下所示
\"gitlab:搭建与基本使用_第10张图片\"

\"gitlab:搭建与基本使用_第11张图片\"

为项目创建Dev分支,如下所示:
\"gitlab:搭建与基本使用_第12张图片\"

\"gitlab:搭建与基本使用_第13张图片\"

在 client 上添加Tompson的用户:

[root@vm2 ~]# useradd Tompson
[root@vm2 ~]# useradd Eric
[root@vm2 ~]# useradd Hellen
[root@vm2 ~]# su - Tompson
[Tompson@vm2 ~]$ ssh-keygen -C Tompson@domain.cn
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:SAoAH2zSxqEJqVgKKrxM5XMi6tKe61JMRdwMhwBNIrE Tompson@domain.cn
The key\'s randomart image is:
+---[RSA 2048]----+
|XX==o=.          |
|*BOo+.o          |
|E*=.  .          |
|*+.= + .         |
|=oo = . S        |
|.oo              |
|.o               |
|o...             |
|.+=.             |
+----[SHA256]-----+
[Tompson@vm2 ~]$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDZ8cRGHej+sZzlmK36W5MUXMLOGdTwFI9Jj44mGuabZCrlYW4GDpL6ppezN+Sur0wHtnKonYJzm8ntqS0S0UHyaPPQPl9Mrs/6Z4VrXZ/4RlqHdWeSrmOwCBQld0l8HvrmP4TyGHrOreO8uZqimd/Z+OiMqnYRZzENX11Pti/Px5g1MtJcoCi9uLtF42QYrt1X/fzAyPU9C5/5ZUq4Jln3EF20bzcA52oAzZIl0jrhI0TeTeW6zYq+KxdHGshL+qG7+Ne+akPOe4Ma5BQjcMZ2dQ2kbGuozXmLT8RDcj9YRKceQsUdTI71lJpwrWKGn8Vhra0EaK3hgoTuvMYaGfOF Tompson@domain.cn

将Tompson的公钥复制到gitlab中: 使用Tompson用户的身份与密码登录到gitlab界面中,然后在ssh-key中添加 相关的key ,如下所示:
\"gitlab:搭建与基本使用_第14张图片\"

\"gitlab:搭建与基本使用_第15张图片\"

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

[Tompson@vm2 ~]$ git config --global user.email \"Tompson@domain.cn\"
[Tompson@vm2 ~]$ git config --global user.name \"Tompson\"

[Tompson@vm2 ~]$ git clone git@192.168.60.119:plat-sp/chathall.git
Cloning into \'chathall\'...
The authenticity of host \'192.168.60.119 (192.168.60.119)\' can\'t be established.
ECDSA key fingerprint is SHA256:CDxAQmj6gUkIxB6XUofbZ853GuPM5LS2QO4a5dD7jRo.
ECDSA key fingerprint is MD5:4e:20:72:a7:46:c6:d7:5d:bb:9d:ce:c3:f3:da:43:f9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added \'192.168.60.119\' (ECDSA) to the list of known hosts.
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (3/3), done.
[Tompson@vm2 ~]$ 
[Tompson@vm2 ~]$ cd chathall/
[Tompson@vm2 chathall]$ ls
Readme.txt
[Tompson@vm2 chathall]$

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

[Tompson@vm2 chathall]$ vim test.sh
[Tompson@vm2 chathall]$ cat test.sh 
#!/bin/bash
echo \"gitlab test\"
[Tompson@vm2 chathall]$ git add . 
[Tompson@vm2 chathall]$ git commit -m \'201805101649\'
[master 80edf6b] 201805101649
 1 file changed, 2 insertions(+)
 create mode 100644 test.sh
[Tompson@vm2 chathall]$ 
[Tompson@vm2 chathall]$ git push -u origin master 
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 305 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.60.119:plat-sp/chathall.git
   4611654..80edf6b  master -> master
Branch master set up to track remote branch master from origin.
[Tompson@vm2 chathall]$ 

使用Eric用户登录,并clone 项目,如下所示:

[root@vm2 ~]# su - Eric
[Eric@vm2 ~]$ ssh-keygen -C Eric@domain.cn
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:VZaJvjA5SJZEB+yuRpDBNHCECCZ5R8X0DYcNE0f1B6E Eric@domain.cn
The key\'s randomart image is:
+---[RSA 2048]----+
|*O=..B*o**+o+oo. |
|*.+.. *o.*oooo . |
| . + + ..oo E . .|
|  o   o =..    . |
|   . .  S+ .     |
|    . .   .      |
|   . .           |
|    o            |
|   .             |
+----[SHA256]-----+

[Eric@vm2 ~]$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDxQcn4UjRW/5PT5witeV9+S2w8WK5ouawHxEF7s9wuWsT4pqhcu5BN74NG3CPaq1jJZnkV+aQsTw+60BAd1gOK0FBbKWxmohmE61n9vfpUT5igJ72t2jpXjfKwLIHw+Iq5yM4yUhkwSsoBuZkxYSEltnj8OvXaOlCDYnXuGBa9+xO8f5yVIcOtiwRvv+Y1PRRzSIcazPVZax9FLK26t1R4NPiY4xWkIJyK2OrKMeiaBBzyMfWzHdmsCWa51oSrYSmz3PDBXpzIBs3OdKxcaJs9Lc5u87YCV5RMUjLrPcA7nPK6crOabLXhz3d5GSYggMTOByQkyKOo7WlYpARCHOt/ Eric@domain.cn
[Eric@vm2 ~]$ 

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

[Eric@vm2 ~]$ git config --global user.email \"Eric@domain.cn\"
[Eric@vm2 ~]$ git config --global user.name \"Eric\"
[Eric@vm2 ~]$ git clone git@192.168.60.119:plat-sp/chathall.git
Cloning into \'chathall\'...
The authenticity of host \'192.168.60.119 (192.168.60.119)\' can\'t be established.
ECDSA key fingerprint is SHA256:CDxAQmj6gUkIxB6XUofbZ853GuPM5LS2QO4a5dD7jRo.
ECDSA key fingerprint is MD5:4e:20:72:a7:46:c6:d7:5d:bb:9d:ce:c3:f3:da:43:f9.
Are you sure you want to continue connecting (yes/no)? yes
remote: Counting objects: 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@vm2 ~]$ 

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

[Eric@vm2 chathall]$ git checkout dev 
Branch dev set up to track remote branch dev from origin.
Switched to a new branch \'dev\'
[Eric@vm2 chathall]$ ls
Readme.txt  test.sh
[Eric@vm2 chathall]$ vim test.sh 
[Eric@vm2 chathall]$ cat test.sh 
#!/bin/bash
echo \"gitlab test\"
echo \"brahch test\"
[Eric@vm2 chathall]$ git add . 
[Eric@vm2 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@vm2 chathall]$
[Eric@vm2 chathall]$ git checkout master 
Switched to branch \'master\'
[Eric@vm2 chathall]$ git branch 
  dev
* master
[Eric@vm2 chathall]$

使用Eric 用户登录gitlab web,在界面中 创建一个合并请求:
\"gitlab:搭建与基本使用_第16张图片\"

提×××并请求:
\"gitlab:搭建与基本使用_第17张图片\"

然后使用Tompson用户登录 gitlab web ,找到“合并请求” ,然后将dev分支合并到master分支,如下所示:
\"gitlab:搭建与基本使用_第18张图片\"

\"gitlab:搭建与基本使用_第19张图片\"

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

 

转载自:https://blog.51cto.com/caiyuanji/2114796?cid=707746

ItVuer - 免责声明 - 关于我们 - 联系我们

本网站信息来源于互联网,如有侵权请联系:561261067@qq.com

桂ICP备16001015号