企业级CICD环境搭建-gitlab基本使用

  • 2021年12月13日
  • DevOps

一、Gitlab 的使用
在浏览器中输入 http://192.168.1.178/ ,然后 change password: ,并使用root用户登录 即可 (后续动作根据提示操作)
1、Gitlab 命令行修改密码

[root@qfedu.com ~]# 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 												# 退出

2、Gitlab服务管理

[root@qfedu.com ~]# gitlab-ctl start                        # 启动所有 gitlab 组件;
[root@qfedu.com ~]# gitlab-ctl stop                         # 停止所有 gitlab 组件;
[root@qfedu.com ~]# gitlab-ctl restart                      # 重启所有 gitlab 组件;
[root@qfedu.com ~]# gitlab-ctl status                       # 查看服务状态;
[root@qfedu.com ~]# gitlab-ctl reconfigure                  # 初始化服务;
[root@qfedu.com ~]# vim /etc/gitlab/gitlab.rb               # 修改默认的配置文件;
[root@qfedu.com ~]# gitlab-ctl tail                         # 查看日志;

3、登陆 Gitlab

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

登录 gitlab 如下所示(首次登陆设置 root 密码):

4、创建项目组group ,组名为plat-sp

5、去掉用户的自动注册功能(安全)

admin are -> settings -> Sign-up Restrictions 去掉钩钩,然后拉到最下面保存,重新登录

二、Gitlab 开发代码提交处理流程

1、公司代码提交合并流程

– PM(项目主管/项目经理)在gitlab创建任务,分配给开发人员

– 开发人员领取任务后,在本地使用git clone拉取代码库

– 开发人员创建开发分支(git checkout -b dev),并进行开发

– 开发人员完成之后,提交到本地仓库(git commit )

– 开发人员在gitlab界面上申请分支合并请求(Merge request)

– PM在gitlab上查看提交和代码修改情况,确认无误后,确认将开发人员的分支合并到主分支(master)

– 开发人员在gitlab上Mark done确认开发完成,并关闭issue。这一步在提交合并请求时可以通过描述中填写”close #1″等字样,可以直接关闭issue

2、创建项目管理用户 Tompson

同样的方法,再创建 Eric 、Hellen 用户。用户添加完毕后,gitlab 会给用户发一封修改密码的邮件,各用户需要登录自己的邮箱,并点击相关的链接,设置新密码。

3、将 Tompson 用户添加到组中,指定T为本组的 owner

4、同样的方法将用户Eric、Hellen 添加到组中,并指定为Developer

5、使用 Tompson 用户的身份与密码登录到 gitlab 界面中,并创建项目 Project

6、指定项目的存储路径和项目名称

把客户端的公钥添加到这里之后才会出现下图的内容

7、为项目创建 Dev分支

注意:因为版本的原因,需要先在项目里面添加一个文件,才能显示下图中创建分支的操作


8、在 client 上添加 Tompson 的用户

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

9、将 Tompson 的公钥复制到 gitlab 中

使用 Tompson 用户的身份与密码登录到 gitlab界面中,然后在 ssh-key 中添加相关的key
8.png
9.png

10、为 Tompson 用户配置 git

[Tompson@qfedu.com ~]$ git config --global user.email "222@qq.com"
[Tompson@qfedu.com ~]$ git config --global user.name "tom"

[Tompson@qfedu.com ~]$ 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@qfedu.com ~]$ 
[Tompson@qfedu.com ~]$ cd chathall/
[Tompson@qfedu.com chathall]$ ls
Readme.txt
[Tompson@qfedu.com chathall]$

11、创建新文件,添加内容,并提交到 master 分支

[Tompson@qfedu.com chathall]$ vim test.sh
[Tompson@qfedu.com chathall]$ cat test.sh 
#!/bin/bash
echo "gitlab test"
[Tompson@qfedu.com chathall]$ git add . 
[Tompson@qfedu.com chathall]$ git commit -m '201805101649'
[master 80edf6b] 201805101649
 1 file changed, 2 insertions(+)
 create mode 100644 test.sh
[Tompson@qfedu.com chathall]$ 
[Tompson@qfedu.com 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@qfedu.com chathall]$

12、使用 Eric 用户登录,并 clone 项目

[git@qfedu.com ~]# su - Eric
[Eric@qfedu.com ~]$ 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@qfedu.com ~]$ cat .ssh/id_rsa.pub 
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDxQcn4UjRW/5PT5witeV9+S2w8WK5ouawHxEF7s9wuWsT4pqhcu5BN74NG3CPaq1jJZnkV+aQsTw+60BAd1gOK0FBbKWxmohmE61n9vfpUT5igJ72t2jpXjfKwLIHw+Iq5yM4yUhkwSsoBuZkxYSEltnj8OvXaOlCDYnXuGBa9+xO8f5yVIcOtiwRvv+Y1PRRzSIcazPVZax9FLK26t1R4NPiY4xWkIJyK2OrKMeiaBBzyMfWzHdmsCWa51oSrYSmz3PDBXpzIBs3OdKxcaJs9Lc5u87YCV5RMUjLrPcA7nPK6crOabLXhz3d5GSYggMTOByQkyKOo7WlYpARCHOt/ Eric@domain.cn
[Eric@qfedu.com ~]$

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

[Eric@qfedu.com ~]$ git config –global user.email “Eric@domain.cn”
[Eric@qfedu.com ~]$ git config –global user.name “Eric”
[Eric@qfedu.com ~]$ 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@qfedu.com ~]$

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

[Eric@qfedu.com chathall]$ git checkout dev 
Branch dev set up to track remote branch dev from origin.
Switched to a new branch 'dev'
[Eric@qfedu.com chathall]$ ls
Readme.txt  test.sh
[Eric@qfedu.com chathall]$ vim eric.sh 
[Eric@qfedu.com chathall]$ cat eric.sh
#!/bin/bash
echo "brahch test"
[Eric@qfedu.com chathall]$ git add . 
[Eric@qfedu.com chathall]$ git commit -m '201805101658'
[dev 6687039] 201805101658
 1 file changed, 1 insertion(+)
[Eric@qfedu.com 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@qfedu.com chathall]$
[Eric@qfedu.com chathall]$ git checkout master 
Switched to branch 'master'
[Eric@qfedu.com chathall]$ git branch 
  dev
* master
[Eric@qfedu.com chathall]$

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

提交合并请求:

16、使用Tompson用户登录 gitlab web ,找到“合并请求” ,然后将dev分支合并到master分支

发表评论

您的电子邮箱地址不会被公开。