黑猴子的家:搭建属于自己的 GitLab

1、”git” 家族成员的认识

这是一个非常容易混淆的问题

git
是一种版本控制系统,是一个命令,是一种工具

gitlib
是用于实现git功能的开发库

github
是一个基于git实现的在线代码托管仓库,包含一个网站界面,向互联网开放

gitlab
是一个基于git实现的在线代码仓库托管软件,你可以用gitlab自己搭建一个类似于github一样的系统,一般用于在企业、学校等内部网络搭建git私服

2、安装 gitlab 依赖包

[root@hadoop102 ~]# yum install curl policycoreutils openssh-server openssh-clients

[root@hadoop102 ~]# systemctl enable sshd  &&  systemctl start sshd

[root@hadoop102 ~]# yum install postfix

[root@hadoop102 ~]# systemctl enable postfix  &&  systemctl start postfix

 //永久设置http服务开放
[root@hadoop102 ~]# firewall-cmd --permanent --add-service=http   

[root@hadoop102 ~]# systemctl reload firewalld

3、安装 gitlab-ce 社区版 (yum方式,ee是企业版,收费)

[root@hadoop102 ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh |sudo bash

[root@hadoop102 ~]# yum install -y gitlab-ce

4、安装 gitlab-ce 社区版(rpm包安装方式)

[root@hadoop102 ~]# wget https://packages.gitlab.com/gitlab/gitlab-ce/packages/el/7/gitlab-ce-XXX.rpm/download

[root@hadoop102 ~]# rpm -i gitlab-ce-XXX.rpm

5、配置并开启 gitlab

//此时会输入如下很多信息,并启动好多服务
[root@hadoop102 ~]# gitlab-ctl reconfigure

6、GitLab常用命令

启动gitlab:gitlab-ctl start,默认8080端口,设置端口需要修改配置vi /etc/gitlab/gitlab.rb,external_url设置域名和端口号,没有域名则设置ip

 # 启动所有 gitlab 组件;
sudo gitlab-ctl start

# 停止所有 gitlab 组件;
sudo gitlab-ctl stop

# 重启所有 gitlab 组件;
sudo gitlab-ctl restart

# 查看服务状态;
sudo gitlab-ctl status

# 启动服务;
sudo gitlab-ctl reconfigure

# 修改默认的配置文件;
sudo vim /etc/gitlab/gitlab.rb

# 检查gitlab;
gitlab-rake gitlab:check SANITIZE=true --trace

# 查看日志;
sudo gitlab-ctl tail

7、访问 gitlab 的 hostname 主机IP映射

首次登陆会跳出设置密码的界面,设置完后自动跳转到登录界面,默认用户名root。

登陆进去后,可以更改用户名、密码等。

初始登入时,总报502,也没有防火墙,经检查不是 内存不足 就是 端口被占用

《黑猴子的家:搭建属于自己的 GitLab》

这里修改的external_url会影响到后面创建工程的git远程服务器地址,假如我修改为localhost
external_url ‘http://localhost:8099′,则出现下图的情况,最好还是填自己的域名或者直接ip地址。

《黑猴子的家:搭建属于自己的 GitLab》

每次修改配置文件/etc/gitlab/gitlab.rb后需要gitlab-ctl reconfigure才能生效

8、说明

缺点:这种方式虽然说简单方便,但是定制型很差,默认只能使用 postgrenginx

主配置文件:/etc/gitlab/gitlab.rb //可以自定义一些邮件服务等

日志地址:/var/log/gitlab/ // 对应各服务

服务地址:/var/opt/gitlab/ // 对应各服务的主目录

仓库地址:/var/opt/gitlab/git-data //记录项目仓库等提交信息

重置配置:gitlab-ctl reconfigure //不要乱用,会重置为最原始的配置的

重启服务:gitlab-ctl stop/start/restart //启动命令

默认安装:postgres、nginx、redis、unicorn ……

9、配置(就是点点点,熟悉熟悉这个应用)

(1)创建一个项目组groups,生成路径/var/opt/gitlab/git-data/repositories/;

(2)创建一个仓库,可用三种方式链接,新的仓库、已存在的文件夹、已存在的仓库;

创建时可导入 github、gitlab、googlecode 等其他地方的仓库,需要对方token

Git global setup

$ git config --global user.name "liufengji"

$ git config --global user.email "liufengji@aliyun.com"

Create a new repository

$ git clone git@hadoop102:/opt/module/project.git

$ cd project

$ touch README.md

$ git add README.md

$ git commit -m "add README"

$ git push -u origin master

Existing folder

$ cd existing_folder

$ git init

$ git remote add origin git@hadoop102:/opt/module/project.git

$ git add ./

$ git commit -m "init"

$ git push -u origin master

Existing Git Repository

$ cd existing_repo

$ git remote add origin git@hadoop102:/opt/module/project.git

(3)创建用户、不加入或加入项目组或者项目或项目子组

《黑猴子的家:搭建属于自己的 GitLab》

该用户在此界面上面 Users 中设置密码后登录,根据得到的链接地址 git 到仓库
当然把你这台机器上的公钥拷到 seting -> ssh key 即可省去输入如密码一项

《黑猴子的家:搭建属于自己的 GitLab》

(4)你的每次提交都会有记录在服务端 该组或该用户所在repositories中

10、注意事项

(1)端口占用问题
80 端口被系统的 nginx 占用了,所以只能监听非 80 端口;
443 端口也被系统的 nginx 占用,所以也一直没增加对 https 的支持;

(2)Docker简化配置问题
做了两个Docker容器: nginx和gitlab,相当于nginx和gitlab运行在局域网的不同主机,所以端口上没冲突。nginx是对外的服务器,它做一层反向代理到gitlab就能让gitlab提供对外的服务。
然而。。。这个做法却带来了一个新问题:gitlab需要的是22,80,443端口,80与443通过反向代理解决了,22却没办法解决。因为正常来讲,宿主机的SSH肯定也在使用,所以gitlab的SSH监听端口映射到宿主机会有冲突。

当然了,解决办法还是有的,不过非常繁琐。我们做Docker的目的不就是为了降低布署难度吗?如果使用Docker的配置比在宿主机上还繁琐,那使用起来就没太大意义了。
于是gitlab就没有放在Docker中,而是直接搭在宿主机上。

点赞