GitLab 是利用 Ruby on Rails 一个开源的版本管理系统,实现一个自托管的 Git 项目仓库,可经过 Web 界面进行访问公开的或者私人项目。 与 Github 相似,GitLab 可以浏览源代码,管理缺陷和注释。能够管理团队对仓库的访 问,它很是易于浏览提交过的版本并提供一个文件历史库。团队成员能够利用内置的简单聊天程序(Wall)进行交流。 它还提供一个代码片断收集功能能够轻松实现代码复用,便于往后有须要的时候进行查找。 经常使用的网站: 官网:https://about.gitlab.com/ 国内镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/
安装依赖 yum install curl policycoreutils openssh-server openssh-clients policycoreutils-python –y 下载安装包 wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.6.10-ce.0.el7.x86_64.rpm 安装gitlab rpm -ivh gitlab-ce-11.4.14-ce.0.el7.x86_64.rpm
GitLab 的默认配置文件为于:/etc/gitlab/gitlab.rb,修改下图所示的 external_url为本机 IP 地址或者一个能够访问到本机的域名。
修改完配置文件后,使用gitlab-ctl reconfigure从新配置gitlab,html
从新配置执行完毕后,使用gitlab-ctl restart重启服务
使用10.0.0.11进行访问,首次登录会要求更改root用户的密码,更改完密码以后以下
node
因为咱们Gitlab系统是私有仓库,通常用户都是由管理员建立和分派的,因此咱们须要关闭注册
使用 root 用户登陆,点击页面上方的 Adminarea
进入管理员区域页面,点击页面左侧菜单栏下方的 Setting
进入 Settings 页面,下拉页面找到 Sign-up Restrictions 选项
取消 Sign-up enabled 选项前面的勾选,下拉到页面的下方,点击 Save 按钮完成配
置。退回到系统的登陆页面,发现已经没有用户注册功能
python
[root@ci-node1 tools]# gitlab-ctl status run: alertmanager: (pid 37584) 346s; run: log: (pid 23944) 46279s run: gitaly: (pid 37594) 346s; run: log: (pid 23907) 46282s run: gitlab-monitor: (pid 37606) 345s; run: log: (pid 23895) 46282s run: gitlab-workhorse: (pid 37616) 345s; run: log: (pid 23866) 46282s run: logrotate: (pid 37626) 344s; run: log: (pid 23839) 46283s run: nginx: (pid 37633) 344s; run: log: (pid 23840) 46283s run: node-exporter: (pid 37639) 343s; run: log: (pid 23858) 46283s run: postgres-exporter: (pid 37643) 343s; run: log: (pid 23943) 46280s run: postgresql: (pid 37654) 342s; run: log: (pid 23823) 46284s run: prometheus: (pid 37656) 342s; run: log: (pid 23913) 46281s run: redis: (pid 37669) 341s; run: log: (pid 23824) 46284s run: redis-exporter: (pid 37675) 341s; run: log: (pid 23894) 46282s run: sidekiq: (pid 37683) 338s; run: log: (pid 23825) 46284s run: unicorn: (pid 37695) 337s; run: log: (pid 23826) 46284s
gitlab-ctl start:启动所有服务
gitlab-ctl restart:重启所有服务
gitlab-ctl stop:中止所有服务
gitlab-ctl reconfigure:使配置文件生效(通常修改完主配置文件/etc/gitlab/gitlab.rb,须要执行此命令)
gitlab-ctl show-config :验证配置文件
gitlab-ctl uninstall:删除gitlab(保留数据)
gitlab-ctl cleanse:删除全部数据,重新开始
gitlab-ctl tail
GitLab 由主要由如下服务构成,他们共同承担了 Gitlab 的运做须要git
nginx:静态Web服务器 gitlab-shell:用于处理Git命令和修改authorized keys列表,咱们的gitlab是以Git作为最层的,你操做实际上最后就是调用gitlab-shell命令进行处理。 gitlab-workhorse:轻量级的反向代理服务器 logrotate:日志文件管理工具 postgresql:数据库 redis:缓存数据库 sidekiq:用于在后台执行队列任务(异步执行) unicorn:GitLab Rails应用是托管在这个服务器上面的
/var/opt/gitlab/git-data/repositories:库默认存储目录 /opt/gitlab: 应用代码和相应的依赖程序 /var/opt/gitlab:gitlab-ctl reconfigure命令编译后的应用数据和配置文件,不须要人为修改配置 /etc/gitlab: 配置文件目录 /var/log/gitlab:此目录下存放了gitlab各个组件产生的日志 /var/opt/gitlab/backups/:备份文件生成的目录
GitLab 是经过组(group)的概念来统一管理仓库(project)和用户(user),经过建立组,在组下再建立仓库,再将用户加入到组,从而实现用户与仓库的权限管理web
在管理员页面点击页面顶部的 Admin area 按钮,进入管理员区域,在页面右侧点击绿色的 New group 按钮,进入建立组页面
在建立组页面中,组路径和组名称为必填项,并且此两处内容好一致:
redis
点击页面下的 create group 按钮,完成组的建立,进入组管理页面
sql
在管理员页面点击页面顶部的 Admin area 按钮,进入管理员区域,在页面右侧点击绿色的 New user 按钮,进入建立用户页面:
在建立用户页面,输入用户名昵称、用户名、电子邮件、选择用户级别
点击页面下部的 create user 按钮,完成用户建立,进入用户管理页面,点击页面右上页的 Edit 按钮,为用户设置初始密码,在此页面也可同时修改用户注册信息。
shell
用户建立完成后,咱们就须要对用户进行受权,从而使用户能够管理仓库,有两种方式,一是将用户加入到组,这样用户能够管理组内的仓库,二是直接受权用户管理仓库。一般咱们采用的方式是将用户加入相应的组,并赋予不一样的角色。GitLab 中用户的角色是系统定义好的,不能更改。这一点可能不符合咱们正常的思惟习惯。
下面咱们将刚建立的 dev 用户添加到咱们的 oldboy 组,将赋予 developer 权限,在管理员区域,
点击 oldboy 组,进入组管理页面:
选择咱们刚建立的 dev 用户,选择 developer 角色,而后添加到组:
注:关于每一种角色对应的权限,可参见官方文档相关内容:https://docs.gitlab.com/ee/user/permissions.html数据库
在 GitLab 中,你能够建立 project 用来存储你的程序代码、做为一个问题跟踪器、用于代码协做、用于持续集成中的构建、测试和部署等
在管理员区域点击 New project 按钮,或者点击导航栏中的 选择 New project 选项
进入到新建 project 页面
选择仓库所属的组,输入仓库名称、仓库描述,选择可见级别,便可完成仓库建立。进入仓库主页面:页面左侧部分为仓库操做相关菜单栏,右侧空仓库下显示如何在命令行链接该仓库,非空时显示仓库内容。
咱们将 git_test 仓库加入到 oldboy 组后,组内的成员便可以看到该仓库,咱们使用
dev 用户登陆,便可看到该仓库
前面咱们已经在 GitLab 建立了仓库,而且受权用户可使用仓库,咱们全部的操做都是在 WEB 页面进行,下在咱们介绍如何使用客户端来链接咱们的仓库。 咱们的仓库是私有的,只有受权的用户才能够访问到该仓库,那么只要将客户端的用户与咱们 GitLab 的用户绑定,客户端便可访问到 GitLab 上的仓库,咱们建议使用 SSH 方式实现客户端与 Gitlab 用户的绑定,具体配置以下:
在客户端生成 ssh 密钥对(注 windows 客户端下只能使用 rsa 加密方式)
[root@ci-node1 tools]# ssh-keygen -t rsa Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): /root/.ssh/id_rsa already exists. Overwrite (y/n)? y Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: SHA256:Ijcm2IoY2fwwYZCGEKjvTVciRLkovKYLX26y2woy5UY root@ci-node1 The key's randomart image is: +---[RSA 2048]----+ |Bo ... | |+o o | |+ oo . | |o*.+o . . | |ooE +.=oS | |.O.=.=.o | |X.+oo. | |+=o+o | |..=*o | +----[SHA256]-----+
咱们将该用户与 GitLab 的 root 用户绑定,复制用户的公钥,在 GitLab 主页面点击用户设置
此处只能添加公钥,若是添加私钥系统将报错,此外一个公钥在整个 GitLab 系统中只能添加一次,可是一个 GitLab 用户能够添加多个公钥
首先咱们要将GitLab上的git_data仓库配置为ci-node1上git_data仓库的远程仓库,再使用推送的功能进行推送
[root@ci-node1 git_data]# git remote add gitlab git@10.0.0.11:oldboy/git_data.git [root@ci-node1 git_data]# git push -u gitlab Counting objects: 42, done. Compressing objects: 100% (32/32), done. Writing objects: 100% (42/42), 3.60 KiB | 0 bytes/s, done. Total 42 (delta 8), reused 0 (delta 0) To 10.0.0.11:oldboy/git_data.git * [new branch] master -> master Branch master set up to track remote branch master from gitlab.
推送完后能够在gitlab仓库中看到推上来的内容
首先,咱们配置 ci-node2 客户端与 GitLab 上 dev 用户绑定
使用 git clone 命令克隆仓库到 ci-node2 本地
[root@ci-node2 git_data]# git clone git@10.0.0.11:oldboy/git_data.git Cloning into 'git_data'... remote: Enumerating objects: 42, done. remote: Counting objects: 100% (42/42), done. remote: Compressing objects: 100% (32/32), done. remote: Total 42 (delta 8), reused 0 (delta 0) Receiving objects: 100% (42/42), done. Resolving deltas: 100% (8/8), done. [root@ci-node2 git_data]# ls git_data [root@ci-node2 git_data]# cd git_data/ [root@ci-node2 git_data]# ls ab a.txt b.txt c.txt test [root@ci-node2 git_data]# git remote origin
咱们能够看到已经将 GitLab 上的 git_data 仓库克隆到了 ci-node2 本地,同时为本地仓库添加了一个指向 GitLab 上 git_data 仓库的远程仓库
咱们 ci-node2 的 git_data 上建立一个 dev 分支,并将 dev 分支,推送到 GitLab 上:
[root@ci-node2 git_data]# git branch dev #建立分支 [root@ci-node2 git_data]# git branch #查看分支 dev * master [root@ci-node2 git_data]# git checkout dev #切换分支 Switched to branch 'dev' [root@ci-node2 git_data]# git status #查看分支状态 # On branch dev nothing to commit, working directory clean [root@ci-node2 git_data]# git push -u origin dev #推送到远程仓库 Total 0 (delta 0), reused 0 (delta 0) remote: remote: To create a merge request for dev, visit: remote: http://10.0.0.11/oldboy/git_data/merge_requests/new?merge_request%5Bsource_branch%5D=dev remote: To git@10.0.0.11:oldboy/git_data.git * [new branch] dev -> dev Branch dev set up to track remote branch dev from origin.
完成后能够在gitlab在看到咱们推送上来的分支
在实际使用过程当中,咱们一般会保持 master 分支稳定,用于生产环境的版本发布,只有受权的用户才能够向 master 合并代码。要实现此功能,咱们须要将 master 设置为保护分支,并受权什么用户能够向 master 用户推送代码
使用 root 用户点击 git_data 仓库页面左下角的 Settings
设置完成后,在仓库分支页面,可看到 master 分支后面出现一个绿色的 protected 标记
此时咱们再尝试在 ci-node2 上推送 master 分支到 GitLab
[root@ci-node2 git_data]# git add 123 [root@ci-node2 git_data]# git commit -m "add 123" [master 13225ae] add 123 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 123 [root@ci-node2 git_data]# git push origin master Counting objects: 3, done. Compressing objects: 100% (2/2), done. Writing objects: 100% (2/2), 221 bytes | 0 bytes/s, done. Total 2 (delta 1), reused 0 (delta 0) remote: GitLab: You are not allowed to push code to protected branches on this project. To git@10.0.0.11:oldboy/git_data.git ! [remote rejected] master -> master (pre-receive hook declined) error: failed to push some refs to 'git@10.0.0.11:oldboy/git_data.git'
咱们发现此时咱们已经不能在 ci-node2 上向 GitLab 上推送 master 分支,由于咱们ci-node2 绑定的是 dev 用户,dev 用户属于 developer 角色,master 分支不容许 developer角色向其推送内容
对 gitlab 进行备份将会建立一个包含全部库和附件的归档文件。对备份的恢复只能恢复到与备份时的 gitlab 相同的版本。将 gitlab 迁移到另外一台服务器上的佳方法就是经过备份和还原。gitlab 提供了一个简单的命令行来备份整个 gitlab,而且能灵活的知足需求
备份文件将保存在配置文件中定义的backup_path 中,文件名TIMESTAMP_gitlab_backup.tar,TIMESTAMP 为备份时的时间戳。TIMESTAMP 的格式为:EPOCH_YYYY_MM_DD_Gitlab-version。 默认的备份文件目录为:/var/opt/gitlab/backups,若是自定义备份目录须要赋予目录 git 权限,具体操做以下:
在配置文件/etc/gitlab/gitlab.rb 最后处加入:
gitlab_rails['backup_path'] = '/data/backup/gitlab' gitlab_rails['backup_keep_time'] = 604800 #备份保留的时间(以秒为单位, 这个是七天默认值)
配置完后,使用 gitlab-ctl reconfigure从新加载配置文件
使用命令gitlab-rake gitlab:backup:create进行备份 [root@ci-node1 git_data]# gitlab-rake gitlab:backup:create Dumping database ... Dumping PostgreSQL database gitlabhq_production ... [DONE] done Dumping repositories ... * root/test ... [DONE] [SKIPPED] Wiki * oldboy/git_data ... [DONE] [SKIPPED] Wiki done Dumping uploads ... done Dumping builds ... done Dumping artifacts ... done Dumping pages ... done Dumping lfs objects ... done Dumping container registry images ... [DISABLED] Creating backup archive: 1554132551_2019_04_01_11.4.14_gitlab_backup.tar ... done Uploading backup archive to remote storage ... skipped Deleting tmp directories ... done done done done done done done done Deleting old backups ... done. (0 removed) You have new mail in /var/spool/mail/root #查看备份的文件 [root@ci-node1 git_data]# ll /data/backup/gitlab/ total 264 -rw------- 1 git git 133120 Apr 1 23:24 1554132241_2019_04_01_11.4.14_gitlab_backup.tar -rw------- 1 git git 133120 Apr 1 23:29 1554132551_2019_04_01_11.4.14_gitlab_backup.tar
经过在定时任务里添加: 0 2 * * * /opt/gitlab/bin/gitlab-rake gitlab:backup:create CRON=1 咱们来实现定时备份,因为代码是一个企业很是重要的资产,因此咱们要重视 GitLab的备份工做。至少作到天天备份一次,平时要注意检查备份的完整性。 环境变量 CRON=1 的做用是若是没有任何错误发生时, 抑制备份脚本的全部进度输
GitLab 的恢复只能还原到与备份文件相同的gitlab 版本的系统中,恢复时,中止链接到数据库的进程(也就是中止数据写入服务),可是保持 GitLab 是运行的
中止这两个服务 gitlab-ctl stop unicorn gitlab-ctl stop sidekiq 而后再进行恢复 gitlab-rake gitlab:backup:restore BACKUP=1554132241_2019_04_01_11.4.14
首先,下载新版本的 RPM 包,能够经过官网或者清华镜像站获取。
其次关闭部分 gitlab 服务
gitlab-ctl stop unicorn gitlab-ctl stop sidekiq gitlab-ctl stop nginx
执行升级操做
rpm -Uvh gitlab-ce-11.4.14-ce.0.el7.x86_64.rpm 从新配置 gitlab gitlab-ctl reconfigure 重启 gitlab 服务 gitlab-ctl restart
注:升级操做不建议进行。若是确实须要,也能够采起在一台新的服务器上安装新版本的 Gitlab,而后采用导入库的方式将旧系统的代码仓库导入到新 Gitlab 上。