GitLab的安装及使用

  • A+
所属分类:Linux
高性能企业级服务器首台5折

前言

GitLab是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。

它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。

团队成员可以利用内置的简单聊天程序(Wall)进行交流。

它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。

Git的家族成员

Git:是一种版本控制系统,是一个命令,是一种工具。

Gitlib:是用于实现Git功能的开发库。

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

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

Gitlab的服务构成

Nginx:静态web服务器

gitlab-shell:用于处理Git命令和修改authorized keys列表。

gitlab-workhorse:轻量级的反向代理服务器。

logrotate:日志文件管理工具。

postgresql:数据库

redis:缓存数据库

sidekiq:用于在后台执行队列任务(异步执行)。

unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。

GitLab工作流程

1

GitLab Shell

GitLab Shell有两个作用:为GitLab处理Git命令、修改authorized keys列表。

当通过SSH访问GitLab Server时,GitLab Shell会:

限制执行预定义好的Git命令(git push, git pull, git annex)

调用GitLab Rails API 检查权限

执行pre-receive钩子(在GitLab企业版中叫做Git钩子)

执行你请求的动作 处理GitLab的post-receive动作

处理自定义的post-receive动作

当通过http(s)访问GitLab Server时,工作流程取决于你是从Git仓库拉取(pull)代码还是向git仓库推送(push)代码。

如果你是从Git仓库拉取(pull)代码,GitLab Rails应用会全权负责处理用户鉴权和执行Git命令的工作;

如果你是向Git仓库推送(push)代码,GitLab Rails应用既不会进行用户鉴权也不会执行Git命令,它会把以下工作交由GitLab Shell进行处理:

  1. 1
    <span class="pun">调用</span><span class="typ">GitLab</span> <span class="typ">Rails</span><span class="pln"> API </span><span class="pun">检查权限</span>
  2. 1
    <span class="pun">执行</span><span class="pln">pre</span><span class="pun">-</span><span class="pln"><span class="hljs-keyword">receive</span></span><span class="pun">钩子(在</span><span class="typ">GitLab</span><span class="pun">企业版中叫做</span><span class="typ">Git</span><span class="pun">钩子)</span>
  3. 1
    <span class="pun">执行你请求的动作</span>
  4. 1
    <span class="pun">处理</span><span class="typ">GitLab</span><span class="pun">的</span><span class="pln">post</span><span class="pun">-</span><span class="pln"><span class="hljs-keyword">receive</span></span><span class="pun">动作</span>
  5. 1
    <span class="pun">处理自定义的</span><span class="pln">post</span><span class="pun">-</span><span class="pln"><span class="hljs-keyword">receive</span></span><span class="pun">动作</span>

GitLab Workhorse

GitLab Workhorse是一个敏捷的反向代理。它会处理一些大的HTTP请求,比如文件上传、文件下载、Git push/pull和Git包下载。其它请求会反向代理到GitLab Rails应用,即反向代理给后端的unicorn。

Gitlab环境部署

ECS配置要求:内存2G以上

方法一:镜像部署

镜像名称:GitLab代码管理(Centos 64位 | GitLab) | 镜像帮助文档

进入镜像详情页面,单击立即购买,按提示步骤购买 ECS 实例。

购买完成之后,登录”ECS 管理控制台”,在左边导航栏里,单击”实例”,进入 ECS 实例列表页,选择所购 ECS 实例所在的地域,并找到所购 ECS 实例,在”IP 地址”列获取该实例的公网 IP 地址。

注意:镜像部署好后默认是禁止远端访问的,所以直接访问ECS服务器的公网IP是不能访问到GitLab的登录界面的,请先运行/alidata目录下的gitlab_opennet.sh脚本,开启远程访问,然后再通过浏览器访问公网IP来访问GitLab的主页。

方法二:手动部署:

1、配置yum源

  1. 1
    <span class="pln">vim </span><span class="pun">/</span><span class="pln">etc</span><span class="pun">/</span><span class="pln">yum</span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">repos</span></span><span class="pun"><span class="hljs-selector-class">.</span></span><span class="pln"><span class="hljs-selector-class">d</span></span><span class="pun">/</span><span class="pln">gitlab</span><span class="pun">-</span><span class="pln">ce</span><span class="pun">.</span><span class="pln">repo</span>

复制以下内容:

  1. 1
    <span class="pun">[</span><span class="pln">gitlab</span><span class="pun">-</span><span class="pln">ce</span><span class="pun">]</span>
  2. 1
    <span class="pln"><span class="hljs-attr">name</span></span><span class="pun">=</span><span class="pln">gitlab</span><span class="pun">-</span><span class="pln">ce</span>
  3. 1
    <span class="pln">baseurl</span><span class="pun">=</span><span class="pln">http</span><span class="pun">:</span><span class="com"><span class="hljs-regexp">//mi</span>rrors.tuna.tsinghua.edu.cn<span class="hljs-regexp">/gitlab-ce/yum</span><span class="hljs-regexp">/el6</span></span>
  4. 1
    <span class="typ"><span class="hljs-attr">Repo_gpgcheck</span></span><span class="pun">=</span><span class="lit"><span class="hljs-number">0</span></span>
  5. 1
    <span class="typ"><span class="hljs-attr">Enabled</span></span><span class="pun">=</span><span class="lit"><span class="hljs-number">1</span></span>
  6. 1
    <span class="typ">Gpgkey</span><span class="pun">=</span><span class="pln"><span class="hljs-string">https</span></span><span class="pun"><span class="hljs-string">:</span></span><span class="com"><span class="hljs-comment">//packages.gitlab.com/gpg.key</span></span>

2

2、更新本地yum缓存

  1. 1
    <span class="pln"><span class="hljs-attribute">sudo yum makecache</span></span>

3

3、安装GitLab社区版

  1. 1
    <span class="pln">sudo yum <span class="hljs-keyword">install</span> gitlab</span><span class="pun">-</span><span class="pln">ce        </span><span class="com"><span class="hljs-comment">#自动安装最新版</span></span>
  2. 1
     
  3. 1
    <span class="pln">sudo yum install gitlab</span><span class="pun">-</span><span class="pln">ce</span><span class="pun">-</span><span class="pln"><span class="hljs-keyword">x</span></span><span class="pun">.</span><span class="pln"><span class="hljs-keyword">x</span></span><span class="pun">.</span><span class="pln"><span class="hljs-keyword">x</span>    </span><span class="com">#安装指定版本</span>

4

GitLab常用命令:

  1. 1
    <span class="pln">sudo gitlab</span><span class="pun">-</span><span class="pln">ctl <span class="hljs-literal">start</span>    </span><span class="com"><span class="hljs-comment"># 启动所有 gitlab 组件;</span></span>
  2. 1
    <span class="pln">sudo gitlab</span><span class="pun">-</span><span class="pln">ctl <span class="hljs-literal">stop</span>        </span><span class="com"><span class="hljs-comment"># 停止所有 gitlab 组件;</span></span>
  3. 1
    <span class="pln"><span class="hljs-attribute">sudo</span> gitlab</span><span class="pun">-</span><span class="pln">ctl restart        </span><span class="com"><span class="hljs-comment"># 重启所有 gitlab 组件;</span></span>
  4. 1
    <span class="pln"><span class="hljs-attribute">sudo</span> gitlab</span><span class="pun">-</span><span class="pln">ctl status        </span><span class="com"><span class="hljs-comment"># 查看服务状态;</span></span>
  5. 1
    <span class="pln"><span class="hljs-attribute">sudo</span> gitlab</span><span class="pun">-</span><span class="pln">ctl reconfigure        </span><span class="com"><span class="hljs-comment"># 启动服务;</span></span>
  6. 1
    <span class="pln">sudo vim </span><span class="pun"><span class="hljs-meta-keyword">/</span></span><span class="pln"><span class="hljs-meta-keyword">etc</span></span><span class="pun"><span class="hljs-meta-keyword">/</span></span><span class="pln">gitlab</span><span class="pun">/</span><span class="pln">gitlab</span><span class="pun">.</span><span class="pln">rb        </span><span class="com"><span class="hljs-meta"># 修改默认的配置文件;</span></span>
  7. 1
    <span class="pln">gitlab</span><span class="pun">-</span><span class="pln">rake gitlab</span><span class="pun">:</span><span class="pln"><span class="hljs-type">check SANITIZE</span></span><span class="pun">=</span><span class="kwd"><span class="hljs-literal">true</span></span> <span class="pun">--</span><span class="pln"><span class="hljs-built_in">trace</span>    </span><span class="com"><span class="hljs-meta"># 检查gitlab;</span></span>
  8. 1
    <span class="pln"><span class="hljs-attribute">sudo</span> gitlab</span><span class="pun">-</span><span class="pln">ctl tail        </span><span class="com"><span class="hljs-comment"># 查看日志;</span></span>

GitLab使用

登录GitLab

1、在浏览器的地址栏中输入ECS服务器的公网IP即可登录GitLab的界面,第一次登录使用的用户名和密码为 root 和 5iveL!fe。

5

2、首次登录会强制用户修改密码。密码修改成功后,输入新密码进行登录。

6

7

创建Project

1、安装Git工具linux:安装Git,使用自带的源安装。

  1. 1
    <span class="pln">yum <span class="hljs-keyword">install</span> git</span>

8

2、生成密钥文件

使用ssh-keygen生成密钥文件.ssh/id_rsa.pub。

9

11

3.在GitLab的主页中新建一个Project

12

13

4.添加ssh key导入步骤2中生成的密钥文件内容:

14

15

ssh key添加完成:

16

项目地址,该地址在进行clone操作时需要用到:

17

简单配置

1、配置使用Git仓库的人员姓名

  1. 1
    <span class="pln">git<span class="hljs-built_in"> config </span></span><span class="pun">--</span><span class="kwd">global</span><span class="pln"> user</span><span class="pun">.</span><span class="pln">name </span><span class="str"><span class="hljs-string">"上海驻云"</span></span>

2、配置使用Git仓库的人员email,填写自己的公司邮箱

  1. 1
    <span class="pln">git<span class="hljs-built_in"> config </span></span><span class="pun">--</span><span class="kwd">global</span><span class="pln"> user</span><span class="pun">.</span><span class="pln">email </span><span class="str"><span class="hljs-string">"support@jiagouyun.com"</span></span>

3、克隆项目,在本地生成同名目录,并且目录中会有所有的项目文件

  1. 1
    <span class="pln">git clone git<span class="hljs-variable">@iZbp1h7fx16gkr9u4gk8v3Z</span></span><span class="pun"><span class="hljs-symbol">:</span></span><span class="pln"><span class="hljs-symbol">root</span></span><span class="pun"><span class="hljs-symbol">/</span></span><span class="pln"><span class="hljs-symbol">test</span></span><span class="pun">.</span><span class="pln">git</span>

18

上传文件

1、进入到项目目录

  1. 1
    <span class="pln"><span class="hljs-built_in">cd</span> <span class="hljs-built_in">test</span></span><span class="pun">/</span>

2、创建需要上传到GitLab中的目标文件

  1. 1
    <span class="pln">echo </span><span class="pun">“</span><span class="pln"><span class="hljs-keyword">test</span></span><span class="pun">”</span> <span class="pun">&gt;</span>  <span class="str">/root/</span><span class="pln"><span class="hljs-keyword">test</span></span><span class="pun">.</span><span class="pln"><span class="hljs-keyword">sh</span></span>

3、将目标文件或者目录拷贝到项目目录下

  1. 1
    <span class="pln">cp </span><span class="pun"><span class="hljs-string">/</span></span><span class="pln"><span class="hljs-string">root</span></span><span class="pun"><span class="hljs-string">/</span></span><span class="pln"><span class="hljs-string">test</span></span><span class="pun"><span class="hljs-string">.</span></span><span class="pln"><span class="hljs-string">sh</span> </span><span class="pun"><span class="hljs-string">./</span></span>

19

4、将test.sh文件加入到索引中

  1. 1
    <span class="pln">git <span class="hljs-keyword">add</span><span class="bash"> test</span></span><span class="pun"><span class="bash">.</span></span><span class="pln"><span class="bash">sh </span></span>

5、将test.sh提交到本地仓库

  1. 1
    <span class="pln">git commit </span><span class="pun">-</span><span class="pln"><span class="hljs-keyword">m</span> </span><span class="pun">“</span><span class="pln"><span class="hljs-keyword">test</span></span><span class="pun">.</span><span class="pln"><span class="hljs-keyword">sh</span></span><span class="pun">”</span>

6、将文件同步到GitLab服务器上

  1. 1
    <span class="pln">git push </span><span class="pun">-</span><span class="pln">u origin <span class="hljs-keyword">master</span> </span>

20

7、在网页中查看上传的test.sh文件已经同步到GitLab中

21

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: