GitLab企业级代码管理仓库

GitLab企业级代码管理仓库

四,GitLab服务器

前面我们已经知道Git人人都是中心,那他们怎么交互数据呢?

  • 使用GitHub或者码云等公共代码仓库
  • 使用GitLab私有仓库

4.1 GitLab是什么?

是一个用于仓库管理系统的开源项目,使用Git作为代码管理工具,并在此基础上搭建起来的web服务。基础功能免费,高级功能收费。

4.2 为什么要使用GitLab?

基础功能开源,可自行搭建 
可以进行权限控制,使得代码对部分人可见 
gitlab使用方便

主机名

IP

备注

特殊要求

G01

192.168.200.186

Git客户端

G02

192.168.200.187

GitLab服务器

内存2G

4.3 GitLab安装

官方安装文档:https://about.gitlab.com/installation/#centos-7

GitLab国内源下载地址:https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/

  1. #在Git02上安装GitLab
  2. #初始环境
  3. [root@Git02 ~]# cat /etc/redhat-release
  4. CentOS Linux release 7.5.1804 (Core) 
  5. [root@Git02 ~]# uname -r
  6. 3.10.0-862.3.3.el7.x86_64
  7. [root@Git02 ~]# echo "Git02 127.0.0.1" >> /etc/hosts
  8. [root@Git02 ~]# tail -1 /etc/hosts
  9. Git02 127.0.0.1
  10. [root@Git02 ~]# tree
  11. .
  12. ├── anaconda-ks.cfg
  13. ├── gitlab-11-2-stable-zh.tar.gz    #GitLab-11.2.3版汉化包
  14. └── gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm    #GitLab-11.2.3版安装包
  15. 0 directories, 3 files
  16. #下载GitLab最新版本安装包
  17. [root@Git02 ~]# wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm
  18. [root@Git02 ~]# yum localinstall gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm -y
  19. 重要说明:
  20. yum localinstall 利用yum安装本地指定的rpm包,好处是自动解决依赖问题
  21. #初始化GitLab,只需要执行一次
  22. [root@Git02 ~]# gitlab-ctl reconfigure
  23. #查看gitlab启动状态
  24. [root@Git02 ~]# gitlab-ctl status
  25. run: alertmanager: (pid 17077) 202s; run: log: (pid 17085) 202s
  26. run: gitaly: (pid 17016) 204s; run: log: (pid 17027) 204s
  27. run: gitlab-monitor: (pid 17050) 203s; run: log: (pid 17052) 203s
  28. run: gitlab-workhorse: (pid 17000) 205s; run: log: (pid 17007) 204s
  29. run: logrotate: (pid 16640) 263s; run: log: (pid 17010) 204s
  30. run: nginx: (pid 16614) 269s; run: log: (pid 17011) 204s
  31. run: node-exporter: (pid 16714) 251s; run: log: (pid 17030) 204s
  32. run: postgres-exporter: (pid 17092) 202s; run: log: (pid 17173) 201s
  33. run: postgresql: (pid 16347) 308s; run: log: (pid 17035) 204s
  34. run: prometheus: (pid 17061) 203s; run: log: (pid 17171) 201s
  35. run: redis: (pid 16287) 314s; run: log: (pid 16990) 205s
  36. run: redis-exporter: (pid 16844) 238s; run: log: (pid 17056) 203s
  37. run: sidekiq: (pid 16586) 276s; run: log: (pid 17036) 204s
  38. run: unicorn: (pid 16548) 282s; run: log: (pid 16993) 205s
  39. #查看GitLab版本号
  40. [root@Git02 ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
  41. 11.2.3

在宿主机输入http://IP地址就可以访问了

接下来我们设定管理员初始密码,最少8

 

登陆管理员账号:root 密码:66666666

登陆后,我们就可以选择如下功能使用了

4.4 GitLab中文社区版补丁包安装

GitLab中文社区:https://gitlab.com/xhang/gitlab/tree/11-2-stable-zh

  1. [root@Git02 ~]# ls
  2. anaconda-ks.cfg gitlab-11-2-stable-zh.tar.gz  gitlab-ce-11.2.3-ce.0.el7.x86_64.rpm
  3. #解压GitLab中文版补丁包
  4. [root@Git02 ~]# tar xf gitlab-11-2-stable-zh.tar.gz
  5. #查看系统已经安装的GitLab版本号
  6. [root@Git02 ~]# cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
  7. 11.2.3
  8. #查看解压后的补丁包版本号
  9. [root@Git02 ~]# cat gitlab-11-2-stable-zh/VERSION
  10. 11.2.3
  11. 说明:
  12. 补丁包版本号和安装的GitLab版本号需要一致
  13. #备份英文版GitLab
  14. [root@Git02 ~]# cp -r /opt/gitlab/embedded/service/gitlab-rails{,.bak}
  15. #将中文补丁包的内容覆盖英文版
  16. [root@Git02 ~]# /bin/cp -rf gitlab-11-2-stable-zh/* /opt/gitlab/embedded/service/gitlab-rails/
  17. /bin/cp: 无法以目录"gitlab-11-2-stable-zh/log" 来覆盖非目录"/opt/gitlab/embedded/service/gitlab-rails/log"
  18. /bin/cp: 无法以目录"gitlab-11-2-stable-zh/tmp" 来覆盖非目录"/opt/gitlab/embedded/service/gitlab-rails/tmp"
  19. 说明:
  20. 此报错不用管,因为已经设置过root密码,登陆过,所以会报错
  21. #重新配置GitLab
  22. [root@Git02 ~]# gitlab-ctl reconfigure
  23. #重新启动GitLab
  24. [root@Git02 ~]# gitlab-ctl restart

重新刷新浏览器访问GitLab

4.5 使用GitLab

4.5.1 创建一个新项目

4.5.2 修改GitLab配置文件/etc/gitlab/gitlab.rb

我们需要修改GitLab的默认访问域名(因为我们是在内网搭建的GitLab 
GitLab默认的监听端口为80,但是在企业中,这个端口经常被别的服务占用,所以我们还需要更换端口号

  1. [root@Git02 ~]# cat -n /etc/gitlab/gitlab.rb.bak | sed -n '13p;943p'
  2. 13  external_url 'http://gitlab.example.com'
  3. 943  # nginx['listen_port'] = nil
  4. [root@Git02 ~]# cat -n /etc/gitlab/gitlab.rb | sed -n '13p;943p'
  5. 13  external_url 'http://192.168.200.187:8888'
  6. 943  nginx['listen_port'] = 8888
  7. #重新配置GitLab
  8. [root@Git02 ~]# gitlab-ctl reconfigure
  9. #重启动GitLab
  10. [root@Git02 ~]# gitlab-ctl restart

重新访问浏览器http://192.168.200.187:8888

4.5.3 尝试将本地仓库代码推送到私有GitLab

  1. #在Git01客户端上
  2. [root@Git01 mycode]# pwd
  3. /mycode
  4. [root@Git01 mycode]# ls
  5. 10  5  6  7  8  9  benet.txt  test.txt  xx.idea
  6. [root@Git01 mycode]# git branch
  7. linux
  8. * master        #当前在master分支
  9. #添加远程GitLab仓库管理
  10. [root@Git01 mycode]# git remote add origin http://192.168.200.187:8888/root/yunjisuan.git
  11. [root@Git01 mycode]# git remote -v
  12. origin  http://192.168.200.187:8888/root/yunjisuan.git (fetch)
  13. origin  http://192.168.200.187:8888/root/yunjisuan.git (push)
  14. test    https://github.com/yinsendemogui/yunjisuan.git (fetch)
  15. test    https://github.com/yinsendemogui/yunjisuan.git (push)
  16. #推送本地仓库master分支到远程仓库GitLab上
  17. [root@Git01 mycode]# git push origin master
  18. Username for 'http://192.168.200.187:8888': root
  19. Password for 'http://root@192.168.200.187:8888': 
  20. 对象计数中: 57, 完成.
  21. 压缩对象中: 100% (38/38), 完成.
  22. 写入对象中: 100% (57/57), 4.77 KiB | 0 bytes/s, 完成.
  23. Total 57 (delta 15), reused 0 (delta 0)
  24. To http://192.168.200.187:8888/root/yunjisuan.git
  25. * [new branch]      master -> master
  26. #推送本地仓库Linux分支到远程仓库GitLab上
  27. [root@Git01 mycode]# git branch
  28. linux
  29. * master
  30. [root@Git01 mycode]# git checkout linux
  31. 切换到分支 'linux'
  32. [root@Git01 mycode]# git branch
  33. * linux
  34. master
  35. [root@Git01 mycode]# git push origin linux
  36. Username for 'http://192.168.200.187:8888': root
  37. Password for 'http://root@192.168.200.187:8888': 
  38. Total 0 (delta 0), reused 0 (delta 0)
  39. remote: 
  40. remote: To create a merge request for linux, visit:
  41. remote:   http://192.168.200.187:8888/root/yunjisuan/merge_requests/new?merge_request%5Bsource_branch%5D=linu
  42. remote: 
  43. To http://192.168.200.187:8888/root/yunjisuan.git
  44. * [new branch]      linux -> linux
  45. #推送本地仓库标签V1.0到远程仓库GitLab上
  46. [root@Git01 mycode]# git tag
  47. v1.0
  48. [root@Git01 mycode]# git push origin v1.0
  49. Username for 'http://192.168.200.187:8888': root
  50. Password for 'http://root@192.168.200.187:8888': 
  51. 对象计数中: 4, 完成.
  52. 压缩对象中: 100% (3/3), 完成.
  53. 写入对象中: 100% (4/4), 444 bytes | 0 bytes/s, 完成.
  54. Total 4 (delta 1), reused 0 (delta 0)
  55. To http://192.168.200.187:8888/root/yunjisuan.git
  56. * [new tag]         v1.0 -> v1.0

4.5.4 尝试在本地仓库克隆GitLab的项目Linux分支到本地仓库

  1. #创建Git工作目录
  2. [root@Git01 ~]# mkdir -p /GitLab
  3. [root@Git01 ~]# cd /GitLab
  4. [root@Git01 GitLab]# git init
  5. 初始化空的 Git 仓库于 /GitLab/.git/
  6. #克隆远程仓库GitLab的Linux分支
  7. [root@Git01 GitLab]# git clone -b linux http://192.168.200.187:8888/root/yunjisuan.git
  8. 正克隆到 'yunjisuan'...
  9. Username for 'http://192.168.200.187:8888': root
  10. Password for 'http://root@192.168.200.187:8888': 
  11. remote: Enumerating objects: 61, done.
  12. remote: Counting objects: 100% (61/61), done.
  13. remote: Compressing objects: 100% (41/41), done.
  14. remote: Total 61 (delta 16), reused 0 (delta 0)
  15. 展开对象中: 100% (61/61), 完成.
  16. 检查连接... 完成。
  17. [root@Git01 GitLab]# ls
  18. yunjisuan
  19. [root@Git01 GitLab]# cd yunjisuan/
  20. [root@Git01 yunjisuan]# ls
  21. 10  5  6  7  8  9  benet.txt  test.txt  xx.idea
  22. [root@Git01 yunjisuan]# git branch
  23. * linux

4.5.5 配置GitLab的SSH密钥连接方式

  1. #在Git01客户端生成密钥对
  2. [root@Git01 ~]# ssh-keygen -t rsa -C '215379068@qq.com'
  3. Generating public/private rsa key pair.
  4. Enter file in which to save the key (/root/.ssh/id_rsa): 
  5. Enter passphrase (empty for no passphrase): 
  6. Enter same passphrase again: 
  7. Your identification has been saved in /root/.ssh/id_rsa.
  8. Your public key has been saved in /root/.ssh/id_rsa.pub.
  9. The key fingerprint is:
  10. SHA256:ECF+/zlO9WMrd9t9wX6v1KKOt12RictT5CLT++YW9Vc 215379068@qq.com
  11. The key's randomart image is:
  12. +---[RSA 2048]----+
  13. |    . o.         |
  14. |   . . .         |
  15. |    . o        . |
  16. |     . o    . + E|
  17. |        S  o.+.*+|
  18. |         . o+.==+|
  19. |          =  =B *|
  20. |         o oo=+O*|
  21. |          oo++B*O|
  22. +----[SHA256]-----+
  23. [root@Git01 ~]#
  24. [root@Git01 ~]# cat /root/.ssh/id_rsa.pub   #公钥内容,复制到GitLab上
  25. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDQrNb1Ce+/2/MjeDNRiWrrrShpk9ARQmCfsUu5TWUJvFE9MeLY4bAYj67GdymDvrt7eRuieJCeZNu5gGYoDdFzOroZigv8me4LaVdycnigonRjQt0gCeWltNpg7s7XQcJtceFstNvE/d53SRF2hgSELDVoV8nKcZUMhIFZ62ewevq7AR1kLIuujyh83jPjwVcPJqTVc4b4png5vSQk0uTeeT5+UVxBSVZYTiTwXDTv/SrqqGTYNREfPm+WF6qU8DqIXhA3wshYRcIy5+Q7sPqTz5WT/+QileHnLZ/iVIu9Pw7JRS7bHktWOIl8/7i+/Qv+BDhhh0KNt2HKMKU7GHBH 215379068@qq.com

在浏览器端打开GitLab

 

4.5.6 利用ssh方式克隆GitLab上的一个项目

  1. [root@Git01 ~]# mkdir -p /test
  2. [root@Git01 ~]# cd /test
  3. [root@Git01 test]# git init
  4. 初始化空的 Git 仓库于 /test/.git/
  5. #将远程GitLab的Linux分支克隆到本地
  6. [root@Git01 test]# git clone -b linux git@192.168.200.187:root/yunjisuan.git
  7. 正克隆到 'yunjisuan'...
  8. remote: Enumerating objects: 61, done.
  9. remote: Counting objects: 100% (61/61), done.
  10. remote: Compressing objects: 100% (41/41), done.
  11. remote: Total 61 (delta 16), reused 0 (delta 0)
  12. 接收对象中: 100% (61/61), 5.15 KiB | 0 bytes/s, 完成.
  13. 处理 delta : 100% (16/16), 完成.
  14. 检查连接... 完成。
  15. [root@Git01 test]# ls
  16. yunjisuan
  17. [root@Git01 test]# cd yunjisuan/
  18. [root@Git01 yunjisuan]# ls
  19. 10  5  6  7  8  9  benet.txt  test.txt  xx.idea
  20. [root@Git01 yunjisuan]# git branch
  21. * linux

4.6 配置GitLab邮件服务

  • 配置邮箱服务的用途: 
    • 有合并请求时,邮件通知
    • 账号注册时,邮件验证
    • 修改密码时,通过邮件修改
  • 配置步骤: 
    • 开启QQ邮箱的smtp服务
    • 修改gitlab配置
    • 测试邮件服务是否正常

4.6.1 开启GitLab服务postfix服务并开启QQ邮箱的smtp服务

  1. [root@Git02 ~]# systemctl start postfix
  2. [root@Git02 ~]# systemctl enable postfix
  3. [root@Git02 ~]# systemctl status postfix

 

4.6.2 修改gitlab配置文件/etc/gitlab/gitlab.rb

  1. [root@Git02 ~]# cat -n /etc/gitlab/gitlab.rb.bak | sed -n '53p;472,480p;618p'
  2. 53  # gitlab_rails['gitlab_email_from'] = 'example@example.com'
  3. 472  # gitlab_rails['smtp_enable'] = true
  4. 473  # gitlab_rails['smtp_address'] = "smtp.server"
  5. 474  # gitlab_rails['smtp_port'] = 465
  6. 475  # gitlab_rails['smtp_user_name'] = "smtp user"
  7. 476  # gitlab_rails['smtp_password'] = "smtp password"
  8. 477  # gitlab_rails['smtp_domain'] = "example.com"
  9. 478  # gitlab_rails['smtp_authentication'] = "login"
  10. 479  # gitlab_rails['smtp_enable_starttls_auto'] = true
  11. 480  # gitlab_rails['smtp_tls'] = false
  12. 618  # user['git_user_email'] = "gitlab@#{node['fqdn']}"
  13. [root@Git02 ~]# cat -n /etc/gitlab/gitlab.rb | sed -n '53p;472,480p;618p'
  14. 53  gitlab_rails['gitlab_email_from'] = '215379068@qq.com'  #GitLab默认邮箱
  15. 472  gitlab_rails['smtp_enable'] = true                      #开启功能
  16. 473  gitlab_rails['smtp_address'] = "smtp.qq.com"            #QQ的smtp服务
  17. 474  gitlab_rails['smtp_port'] = 465                         
  18. 475  gitlab_rails['smtp_user_name'] = "215379068@qq.com"     #smtp用户邮箱
  19. 476  gitlab_rails['smtp_password'] = "开通QQsmtp时返回的字符串"
  20. 477  gitlab_rails['smtp_domain'] = "qq.com"                  #smtp域名
  21. 478  gitlab_rails['smtp_authentication'] = "login"
  22. 479  gitlab_rails['smtp_enable_starttls_auto'] = true
  23. 480  gitlab_rails['smtp_tls'] = true
  24. 618  user['git_user_email'] = "215379068@qq.com"             #git用户邮箱
  25. #重新加载GitLab配置文件
  26. [root@Git02 ~]# gitlab-ctl reconfigure
  27. #重启动GitLab服务
  28. [root@Git02 ~]# gitlab-ctl restart
  29. #进入GitLab控制台进行邮件发送测试
  30. [root@Git02 ~]# gitlab-rails console    #开启GitLab控制台
  31. -------------------------------------------------------------------------------------
  32. GitLab:       11.2.3 (06cbee3)
  33. GitLab Shell: 8.1.1
  34. postgresql:   9.6.8
  35. -------------------------------------------------------------------------------------
  36. Loading production environment (Rails 4.2.10)
  37. irb(main):001:0> Notify.test_email('513809552@qq.com','this is title','hello').deliver_now  #输入左边内容
  38. 特别说明:
  39. Notify.test_email('收件人邮箱','邮件标题','邮件内容')

4.7 GitLab的账号注册及分组

4.7.1 账户注册测试

注册的时候,用的是乱写的邮箱都能注册并登陆; 
显然这在企业里是绝对不允许的。

4.7.2 开启GitLab邮箱验证功能

重新登陆管理员用户root,密码:66666666 ,我们进行如下操作

退出时别忘了保存修改的设置如下图

 

然后我们退出管理员的登陆,进行重新注册

由上图,我们发现,此时注册用户,已经进不去GitLab了; 
这是因为GitLab已经自动发了一封邮件给注册用户的邮箱 
需要用户进行邮件确认后,方可登陆

4.7.3 创建一个项目组

4.7.4 给组添加成员并授权

 

  • Guest访客: 
    • 只能发表评论,不能读写项目库
  • Reporter报告者: 
    • 只能克隆代码(读),不能提交代码(修改)(产品测试)
  • Developer开发人员: 
    • 可以进行代码的读写(普通程序员)
  • Master主程序员: 
    • 可以添加项目成员,添加标签,创建和保护分支(产品经理)
  • Owner所有者: 
    • 拥有所有权限(一般不用此权限)

添加权限成功后,GitLab会自动邮件通知这个用户

 

4.7.5 给项目组创建一个新项目

4.7.5 进行组成员的项目提交和克隆测试

  1. #进行组成员代码提交测试
  2. [root@Git01 ~]# mkdir -p /test
  3. [root@Git01 ~]# cd /test
  4. [root@Git01 test]# ls
  5. [root@Git01 test]# git init
  6. 初始化空的 Git 仓库于 /test/.git/
  7. [root@Git01 test]# echo "welcome to yunjisuan" >> test
  8. [root@Git01 test]# ls
  9. test
  10. [root@Git01 test]# git add *
  11. [root@Git01 test]# git commit -m "提交測試"
  12. [master(根提交) b448074] 提交測試
  13. 1 file changed, 1 insertion(+)
  14. create mode 100644 test
  15. #推送代码到远程GitLab
  16. [root@Git01 test]# git push http://192.168.200.187:8888/test/benet.git master
  17. Username for 'http://192.168.200.187:8888': 215379068@qq.com
  18. Password for 'http://215379068@qq.com@192.168.200.187:8888': 
  19. 对象计数中: 3, 完成.
  20. 写入对象中: 100% (3/3), 235 bytes | 0 bytes/s, 完成.
  21. Total 3 (delta 0), reused 0 (delta 0)
  22. To http://192.168.200.187:8888/test/benet.git
  23. * [new branch]      master -> master

  1. #进行组成员代码的克隆测试
  2. [root@Git01 ~]# mkdir -p /test2
  3. [root@Git01 ~]# cd /test2
  4. [root@Git01 test2]# git init
  5. 初始化空的 Git 仓库于 /test2/.git/
  6. [root@Git01 test2]# git clone http://192.168.200.187:8888/test/benet.git
  7. 正克隆到 'benet'...
  8. Username for 'http://192.168.200.187:8888': 215379068@qq.com
  9. Password for 'http://215379068@qq.com@192.168.200.187:8888': 
  10. remote: Enumerating objects: 3, done.
  11. remote: Counting objects: 100% (3/3), done.
  12. remote: Total 3 (delta 0), reused 0 (delta 0)
  13. 展开对象中: 100% (3/3), 完成.
  14. 检查连接... 完成。
  15. [root@Git01 test2]# ls
  16. benet
  17. [root@Git01 test2]# cat benet/test
  18. welcome to yunjisuan
  19. [root@Git01 test2]# cd benet/
  20. [root@Git01 benet]# git branch
  21. * master

五,GitLab的主从同步(待完善)

主从复制架构:

测试环境上的gitlab仓库,只同步master主分支到从库也就是生产环境上,然后生产环境jenkins只需要抓取从库gitlab就可以了。

其他方法:生产环境上的gitlab上有公网IP,可以通过ip找到公司的gitlab仓库拉取代码。

 

 

原文地址:https://www.cnblogs.com/heroke/p/10303084.html