持续集成之Gitlab部署与应用

前言

Gitlab 是一个利用 Ruby on Rails 开发的开源应用程序,实现一个自托管的 Git 项目仓库,可通过
Web 界面进行访问公开的或者私人的项目。

emm…

那git,github和gitlab都差不多,到底有啥区别?

  • Git 是一个版本控制系统。
    版本控制是一种用于记录一个或多个文件内容变化,方便我们查阅特定版本修订情况的系统。
    早期出现的版本控制系统有:SVN、CVS等,它们是集中式版本控制系统,而 Git 是分布式版本控制系统。

  • GitHub 和 GitLab 都是基于 web 的 Git 仓库,使用起来二者差不多,它们都提供了分享开源项目的平台,为开发团队提供了存储、分享、发布和合作开发项目的中心化云存储的场所。

  • GitHub 作为开源代码库,拥有超过 900 万的开发者用户,目前仍然是最火的开源项目托管平台,

  • GitHub 同时提供公共仓库和私有仓库,但如果使用私有仓库,是需要付费的。

GitLab 让开发团队对他们的代码仓库拥有更多的控制,相比较 GitHub , 它有不少特色:

  • 允许免费设置仓库权限;

  • 允许用户选择分享一个 project 的部分代码;

  • 允许用户设置 project 的获取权限,进一步提升安全性;

  • 可以设置获取到团队整体的改进进度;

  • 通过 innersourcing 让不在权限范围内的人访问不到该资源;

所以,从代码的私有性上来看,GitLab 是一个更好的选择。但是对于开源项目而言,GitHub 依然是代码托管的首选;对于一些开发类的公司肯定都会单独搭建自己的gitlab服务器,而不是去使用GitHub的共有仓库(不安全且不想共享),私有仓库又要收费,很不划算。

Gitlab安装部署

1.安装前提软件

yum install curl policycoreutils openssh-server openssh-clients postfix -y

2.下载安装清华gitlab安装包,(ce为社区版,ee企业版要收费)

wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
截至目前,gitlab已经发行了12.x版本;
但是最好还是选用10.x或11.x,社区资料丰富,毕竟大佬们把雷排的差不多了
yum -y install gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm

3.修改配置文件,指定ip地址

vim /etc/gitlab/gitlab.rb
修改:
external_url 'http://192.168.10.4'
保存退出

安装出现这个熊样子
在这里插入图片描述
4.配置启动 gitlab

gitlab-ctl reconfigure (时间比较长,稍等...)

这里需要注意的是,内存要给大一点,最少 1 个G,不然在配置的时候会出现报错,还有就是如果打开
界面出现 502,请检查 80 或 8080 端口是否被占用。gitlab会默认占用8080或80,若有其他的服务,比如tomcat也会占用8080,那就自行修改端口;例如下面:

#vim /etc/gitlab/gitlab.rb
修改如下:
external_url "http://ip(或域名):8060"
nginx['listen_port'] = 8060

重新再配置一遍 gitlab-ctl reconfigure,服务重新启动一遍

[root@localhost ~]# gitlab-ctl reconfigure
[root@localhost ~]# gitlab-ctl restart

gitlab服务相关的管理命令(开启/关闭/重启)
gitlab-ctl start/stop/restart

5.登录gitlab
http://192.168.10.4:8060
管理员:root
密码:初始配置新密码,最少8位

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

gitlab的应用

1.取消注册功能
登录gitlab–admin area(顶部菜单栏小扳手图标)–左侧面板settingsSign-up Restrictions–去除勾选sign-up enabled --下拉选save保存

在这里插入图片描述
2.修改登录欢迎界面
登录gitlab–admin area–Appearance–填写管理员联系方式和logo–save
各个分支的程序员不一定都知道管理员的联系方式,欢迎界面修改后方便后期出现问题及时处理。

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

项目创建流程

(1)创建group
登录gitlab ---- admin area(中间靠左上的小扳手) ---- new group

在这里插入图片描述

(2)创建用户
登录gitlab–admin areanew user–添加用户名、邮箱createedit修改密码 ,先创建再添加密码,
在这里插入图片描述
把刚创建的用户添加到组;
在这里插入图片描述
(3)创建项目
登录gitlab–admin areanew project注意选择组

项目名称,项目名称可以为字母、数字、空格、下划线、中划线和英文点号组成,且必须以字母或数字开头,不能使用中文
私有库:只有被赋予权限的用户可见
内部库:登录用户可以下载
公开库:所有人可以下载

在这里插入图片描述
在这里插入图片描述
提示通过SSH方式拉取推送项目代码必须要导入SSH key;
项目地址有HTTPSSH两种方式-------可发送给开发人员下载初始化项目
开发人员下载前需上传ssh-key
本地用户创建秘钥:ssh-keygen -t rsa
复制公钥内容:cat /root/.ssh/id_rsa.pub
拷贝到web界面:右上角–用户–settings–SSH Keys

在这里插入图片描述
(4)克隆项目到本地
先模拟写入一个代码文件
在这里插入图片描述
克隆下载项目到本地

[root@localhost git]# git clone http://192.168.10.4:8060/Vitamin/Vitamin_tasks.git
Cloning into 'Vitamin_tasks'...
Username for 'http://192.168.10.4:8060': root
Password for 'http://[email protected]:8060': 
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
[root@localhost git]# ls
Vitamin_tasks
[root@localhost git]#

开发人员下载下来后,本次修改完成再次上传到gitlab

(1).模拟管理员(项目主管)身份运行(少见)

[root@localhost Vitamin_tasks]# cat README.md 
bbbbb
aaaaa

[root@localhost Vitamin_tasks]# git add .
[root@localhost Vitamin_tasks]# git commit -m 'add one line'
[master 0ecfd57] add one line
 1 file changed, 2 insertions(+), 1 deletion(-)
 
[root@localhost Vitamin_tasks]# git push -u origin master  #修改的内容推送给master
Username for 'http://192.168.10.4:8060': root
Password for 'http://[email protected]:8060': 
Counting objects: 5, done.
Writing objects: 100% (3/3), 237 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To http://192.168.10.4:8060/Vitamin/Vitamin_tasks.git
   b6aa12a..0ecfd57  master -> master
Branch master set up to track remote branch master from origin.
[root@localhost Vitamin_tasks]# 

在这里插入图片描述
(2)普通程序员修改完上传(经常见)

root退出登入,切换为VitaminC用户,第一次登入需要修改密码
在这里插入图片描述
1>上传ssh密钥(另开启一台新设备,模拟新环境)
本地用户创建秘钥:ssh-keygen -t rsa
复制公钥内容:cat /root/.ssh/id_rsa.pub
cxy01登录:右上角–用户–settings–SSH Keys
在这里插入图片描述
2>关联远程仓库并拉去代码修改(也可以去clone)

[root@localhost git]# git remote add origin [email protected]:Vitamin/Vitamin_tasks.git
[root@localhost git]# git pull origin master   #origin是一个关联仓库的名字,可以使用git remote 查看
The authenticity of host '192.168.10.4 (192.168.10.4)' can't be established.
ECDSA key fingerprint is f3:29:4b:3d:c6:ad:b2:f2:0e:1b:1b:ba:ac:a0:15:9c.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.10.4' (ECDSA) to the list of known hosts.
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (6/6), done.
From 192.168.10.4:Vitamin/Vitamin_tasks
 * branch            master     -> FETCH_HEAD

3>创建自己的分支,修改代码

[root@localhost git]# git checkout -b cxy01
Switched to a new branch 'cxy01'
[root@localhost git]# 
[root@localhost git]# echo '12345' > new.txt
[root@localhost git]# git add .
[root@localhost git]# git commit -m 'add new.txt'
[cxy01 233a815] add new.txt
 1 file changed, 1 insertion(+)
 create mode 100644 new.txt

4>推送cxy01分支到gitlab界面(普通用户无权推送master,上交只能推送到自己的分支,再申请合并)
否则,会出现下面的情况(在git里可以合并,合并完但push不到gitlab):

[root@localhost git]# git push -u origin master
Total 0 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@192.168.10.4:Vitamin/Vitamin_tasks.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to '[email protected]:Vitamin/Vitamin_tasks.git'

只能这样做:

[root@localhost git]# git branch
* cxy01
  master
[root@localhost git]# git push -u origin cxy01
Counting objects: 4, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 267 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: 
remote: To create a merge request for cxy01, visit:
remote:   http://192.168.10.4:8060/Vitamin/Vitamin_tasks/merge_requests/new?merge_request%5Bsource_branch%5D=cxy01
remote: 
To [email protected]:Vitamin/Vitamin_tasks.git
 * [new branch]      cxy01 -> cxy01
Branch cxy01 set up to track remote branch cxy01 from origin.

在gitlab的web界面就可以看到自己推送的内容
在这里插入图片描述

解决不能push到gitlab的问题:

需要在web界面手动创建merge请求
切换到管理员登录,进入项目,同意merge请求;
在cxy01分支申请合并:
在这里插入图片描述
切换到管理员或项目主管身份,来同意合并
在这里插入图片描述
在这里插入图片描述
此时,不管在哪里看,cxy01或root,还是master分支或cxy01分支,都有新添的代码或文件 。

原创文章 58 获赞 20 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43815140/article/details/105978518
今日推荐