版本仓库

作者:行癫(盗版必究)


一:Git、Github、Gitlab 的区别

  1. Git是版本控制系统
  2. Github是在线的基于Git的代码托管服务,GitHub提供付费账户和免费账户,都可以创建公开的代码仓库,可以创建私有的代码仓库
  3. Gitlab 创建免费的私人仓库

二:Git简介

1.GIt介绍

  1. Git是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目
  2. Git Linus Torvalds 为了帮助管理 Linux 内核开发而开发的一个开放源码的版本控制软件
  3. Git 与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,不必服务器端软件支持

2.Git工作流程

  1. 克隆 Git 资源作为工作目录
  2. 在克隆的资源上添加或修改文件
  3. 如果其他人修改了,你可以更新资源
  4. 在提交前查看修改
  5. 提交修改
  6. 在修改完成后,如果发现错误,可以撤回提交并再次修改并提交

版本仓库 - 图1

3.git的工作区、暂存区和版本库

  1. 工作区:就是你在电脑里能看到的目录
  2. 暂存区:英文叫stage, index。一般存放在"git目录"下的index文件(.git/index)中,所以我们把暂存区有时也叫作索引(index
  3. 版本库:工作区有一个隐藏目录.git,这个不算工作区,而是Git的版本库
  4. ![](%E7%89%88%E6%9C%AC%E4%BB%93%E5%BA%93.assets/dnRUEOrwFBKKhx2PCx9LdA.png)

4.Git安装

  1. [root@xingdian-git ~]# yum -y install git curl-devel expat-devel gettext-devel openssl-devel zlib-devel git-core

三:Git使用

  1. Git 提供了一个叫做 git config 的工具,专门用来配置或读取相应的工作环境变量
  2. 这些环境变量,决定了 Git 在各个环节的具体工作方式和行为。这些变量可以存放在以下三个不同的地方:
  3. /etc/gitconfig 文件:系统中对所有用户都普遍适用的配置。若使用 git config 时用 --system 选项,读写的就是这个文件
  4. ~/.gitconfig 文件:用户目录下的配置文件只适用于该用户。若使用 git config 时用 --global 选项,读写的就是这个文件
  5. 当前项目的 Git 目录中的配置文件(也就是工作目录中的 .git/config 文件):这里的配置仅仅针对当前项目有效

1.Git用户信息

  1. 配置个人的用户名称和电子邮件地址:
  1. [root@xingdian-git ~]# git config --global user.name "xingdian"
  2. [root@xingdian-git ~]# git config --global user.email "xingdian@1000phone.com"

2.查看配置信息

  1. [root@xingdian-git ~]# git config --list
  2. user.name=xingdian
  3. user.email=xingdian@1000phone.com
  1. 这些配置我们也可以在 ~/.gitconfig 或 /etc/gitconfig 看到,如下所示:
  1. [root@xingdian-git ~]# cat ~/.gitconfig
  2. [user]
  3. name = xingdian
  4. email = xingdian@1000phone.com

注意:

  1. git config --global 参数
  2. 取消代理:
  3. git config --global --unset http.proxy
  4. git config --global --unset https.proxy
  5. 设置代理:
  6. git config --global http.proxy http://domain.local\vsilva:Passw0rd@proxyServer:8080
  7. 出现各种 SSL certificate problem 的解决办法:
  8. git config --global http.sslVerify false

3.常用的git 命令

  1. [root@xingdian-git ~]# git init
  2. # 初始化
  3. [root@xingdian-git ~]# git add main.cpp
  4. # 将某一个文件添加到暂存区
  5. [root@xingdian-git ~]# git add .
  6. # 将文件夹下的所有的文件添加到暂存区
  7. [root@xingdian-git ~]# git commit -m 'note'
  8. # 将暂存区中的文件保存成为某一个版本
  9. [root@xingdian-git ~]# git log
  10. # 查看所有的版本日志
  11. [root@xingdian-git ~]# git status
  12. # 查看现在暂存区的状况
  13. [root@xingdian-git ~]# git diff
  14. # 查看现在文件与上一个提交-commit版本的区别
  15. [root@xingdian-git ~]# git reset --hard HEAD^
  16. # 回到上一个版本
  17. [root@xingdian-git ~]# git reset --hard XXXXX
  18. # XXX为版本编号,回到某一个版本
  19. [root@xingdian-git ~]# git pull origin master
  20. # 从主分支pull到本地
  21. [root@xingdian-git ~]# git push -u origin master
  22. # 从本地push到主分支
  23. [root@xingdian-git ~]# git pull
  24. # pull默认主分支
  25. [root@xingdian-git ~]# git push
  26. # push默认主分支 ...

4.Git使用

  1. ssh 链接:
  2. 客户机上产生公钥上传到gitlabSSH-Keys里,git clone下载和git push上传都没问题,这种方式很安全
  3. ssh连接github
  4. 登录github,这是github的主页(如果没有账户需要注册)

Git服务器生成秘钥:

  1. [root@xingdian ~]# ssh-keygen
  2. Generating public/private rsa key pair.
  3. Enter file in which to save the key (/root/.ssh/id_rsa):
  4. Enter passphrase (empty for no passphrase):
  5. Enter same passphrase again:
  6. Your identification has been saved in /root/.ssh/id_rsa.
  7. Your public key has been saved in /root/.ssh/id_rsa.pub.
  8. The key fingerprint is:
  9. SHA256:S/zPOA+wrn/FOnpnFzQponuXzDYDoIFpYTxWeiGKC7g root@xingdian
  10. The key's randomart image is:
  11. +---[RSA 2048]----+
  12. | ...o |
  13. |.. .*o . |
  14. |+ .o.=. . |
  15. |.o +.... . . + |
  16. |E . oSo o o . |
  17. | ...=. o . |
  18. | o.o* . . |
  19. | .. B=@ . |
  20. | .o+=.O++ |
  21. +----[SHA256]-----+
  22. [root@xingdian ~]# cat .ssh/id_rsa.pub
  23. ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDjwGC9van2xd2e2u1769g/SZfJadkC2qXwSMy1yQ9xK8FTMfEXV0X6NTGTu4ZIaj9Jjcq2RjUn9QsS2EEePiAZykki7jOcZAxckY/s9iOS2F10wnTGrSpvUjBh3fpziS5c0HB7ejUsonm/jm/BQqwQ9abdWsFJvombdiDifVSYU9s4SbmAjU4gdtgYYxM5vCnGdfCy06grj25lkmYSnQDqDWW2W8a2nPe1UYliBugh2EcTUi2vCnrM6/Jj3P1f3VyB0Y8MXvybksi9oHTqaOA6t8cdArw19nZ0IEy7vffVTUcTNw1gg4UHgb4s5zWHeOnNCFFKDJfLH80CCORoQuut root@xingdian

Github添加Git服务器秘钥:

版本仓库 - 图2

版本仓库 - 图3

版本仓库 - 图4

Github创建仓库:

版本仓库 - 图5

版本仓库 - 图6

版本仓库 - 图7

git服务器上创建项目并上传到github:

  1. [root@xingdian-cloud opt]# mkdir a
  2. [root@xingdian-cloud opt]# cd a
  3. [root@xingdian-cloud a]# git remote add origin git@github.com:blackmed/xingdiancloud-test.git
  4. [root@xingdian-cloud a]# echo "hello xingdian" > xingdian
  5. [root@xingdian-cloud a]# git add .
  6. [root@xingdian-cloud a]# git commit -m "hello"
  7. [master (root-commit) ec3f424] hello
  8. 1 file changed, 1 insertion(+)
  9. create mode 100644 xingdian
  10. [root@xingdian-cloud a]# git push -u origin master
  11. Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
  12. Counting objects: 3, done.
  13. Writing objects: 100% (3/3), 221 bytes | 0 bytes/s, done.
  14. Total 3 (delta 0), reused 0 (delta 0)
  15. remote:
  16. remote: Create a pull request for 'master' on GitHub by visiting:
  17. remote: https://github.com/blackmed/xingdiancloud-test/pull/new/master
  18. remote:
  19. To git@github.com:blackmed/xingdiancloud-test.git
  20. * [new branch] master -> master
  21. Branch master set up to track remote branch master from origin.

github查看:

版本仓库 - 图8

版本仓库 - 图9

注意:这样上传到github的分支不能跟main分支合并

5.版本穿梭

版本回退:

  1. # 用 git log 命令查看:
  2. # 每一个提交的版本都唯一对应一个 commit 版本号,
  3. # 使用 git reset 命令退到上一个版本:
  4. [root@xingdian-git ~]# git reset --hard HEAD^
  1. [root@xingdian-git ~]# git reflog
  2. # 查看命令历史,以便确定要回到哪个版本
  3. [root@xingdian-git ~]# git reset --hard commit_id
  4. # 比如git reset --hard 3628164
  5. 消失的ID号:
  6. 回到早期的版本后再查看git log会发现最近的版本消失,可以使用reflog查看消失的版本ID,用于回退到消失的版本
  7. [root@xingdian-git ~]# git reflog

6.分支管理

创建分支:

  1. [root@xingdian-git ~]# git checkout -b dev #创建dev分支,然后切换到dev分支
  2. [root@xingdian-git ~]# git checkout #命令加上-b参数表示创建并切换,相当于以下两条命令:
  3. [root@xingdian-git ~]# git branch dev
  4. [root@xingdian-git ~]# git branch #命令查看当前分支,
  5. [root@xingdian-git ~]# git branch #命令会列出所有分支,当前分支前面会标一个*号
  6. [root@xingdian-git ~]# git branch
  7. * dev master
  8. [root@xingdian-git ~]# git add readme.txt
  9. git commit -m "branch test" # 在dev分支上正常提交.

分支切换:

  1. [root@xingdian-git ~]# git checkout master # 切换回master分支
  2. # 查看一个readme.txt文件,刚才添加的内容不见了,因为那个提交是在dev分支上,而master分支此刻的提交点并没有变

合并分支:

  1. [root@xingdian-git ~]# git merge dev # 把dev分支的工作成果合并到master分支上
  2. [root@xingdian-git ~]# git merge # 命令用于合并指定分支到当前分支。
  3. # 合并后,再查看readme.txt的内容,就可以看到,和dev分支的最新提交是完全一样的。

删除分支:

  1. [root@xingdian-git ~]# git branch -d dev #删除dev分支了:
  2. 删除后,查看branch,就只剩下master分支了.

7.github分支管理

注意:github默认的分支是main而不是master

创建项目:

版本仓库 - 图10

创建分支:

版本仓库 - 图11

版本仓库 - 图12

版本仓库 - 图13

新分支创建文件:

版本仓库 - 图14

版本仓库 - 图15

版本仓库 - 图16

分支合并:

版本仓库 - 图17

版本仓库 - 图18

版本仓库 - 图19

版本仓库 - 图20

main分支验证是否合并成功:

版本仓库 - 图21

8.使用github上的项目

注意:

  1. 前期的配置略

下载仓库到本地:

  1. [root@xingdian-cloud opt]# git clone git@github.com:blackmed/xingdiancloud-dian.git
  2. Cloning into 'xingdiancloud-dian'...
  3. Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
  4. remote: Enumerating objects: 7, done.
  5. remote: Counting objects: 100% (7/7), done.
  6. remote: Compressing objects: 100% (4/4), done.
  7. remote: Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
  8. Receiving objects: 100% (7/7), done.
  9. [root@xingdian-cloud opt]# ls
  10. xingdiancloud-dian

进入工作目录,模拟代码更新:

  1. [root@xingdian-cloud opt]# cd xingdiancloud-dian/
  2. [root@xingdian-cloud xingdiancloud-dian]# ls
  3. diandian README.md
  4. [root@xingdian-cloud xingdiancloud-dian]# echo "diange is very good" > xingdian
  5. [root@xingdian-cloud xingdiancloud-dian]# git add .
  6. [root@xingdian-cloud xingdiancloud-dian]# git commit -m "this is diange"
  7. [main 73139aa] this is diange
  8. 1 file changed, 1 insertion(+)
  9. create mode 100644 xingdian
  10. [root@xingdian-cloud xingdiancloud-dian]# git push -u origin main
  11. Warning: Permanently added 'github.com,20.205.243.166' (ECDSA) to the list of known hosts.
  12. Counting objects: 4, done.
  13. Delta compression using up to 2 threads.
  14. Compressing objects: 100% (2/2), done.
  15. Writing objects: 100% (3/3), 322 bytes | 0 bytes/s, done.
  16. Total 3 (delta 0), reused 0 (delta 0)
  17. To git@github.com:blackmed/xingdiancloud-dian.git
  18. ec322d0..73139aa main -> main
  19. Branch main set up to track remote branch main from origin.

github验证:

版本仓库 - 图22

四:Gitlab部署与使用

1.资源环境

主机名 IP地址 服务
gitlab 10.0.0.110 gitab-ce

2.环境部署

  1. 修改主机名:
  1. [root@localhost ~]# hostnamectl --static set-hostname cicd-gitlab
  1. 关闭防火墙和selinux
  1. [root@cicd-gitlab ~]# systemctl stop iptables firewalld
  2. [root@cicd-gitlab ~]# systemctl disable iptables firewalld
  3. [root@cicd-gitlab ~]# setenforce 0
  1. 开启邮件服务:
  1. [root@cicd-gitlab ~]# systemctl start postfix
  2. [root@cicd-gitlab ~]# systemctl enable postfix
  1. 添加本地解析:
  1. [root@cicd-gitlab ~]# vim /etc/hosts
  2. 10.0.1.86 gitlab.qfedu.com

3.安装gitlab依赖包

  1. [root@cicd-gitlab ~]# yum install -y curl openssh-server openssh-clients postfix cronie policycoreutils-python

4.添加gitlab安装源

  1. 阿里源
  2. [root@cicd-gitlab ~]# vim /etc/yum.repos.d/gitlab-ce.repo
  3. [gitlab-ce]
  4. name=gitlab-ce
  5. baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
  6. Repo_gpgcheck=0
  7. Enabled=1
  8. gpgcheck=0
  9. 清华源:
  10. [root@cicd-gitlab ~]# vim gitlab-ce.repo
  11. [gitlab-ce]
  12. name=Gitlab CE Repository
  13. baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
  14. gpgcheck=0
  15. enabled=1
  16. [root@cicd-gitlab ~]# vim gitlab-ee.repo
  17. [gitlab-ee]
  18. name=Gitlab EE Repository
  19. baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ee/yum/el$releasever/
  20. gpgcheck=0
  21. enabled=1
  22. 官方源:
  23. [root@cicd-gitlab ~]# vim runner_gitlab-ci-multi-runner.repo
  24. [runner_gitlab-ci-multi-runner]
  25. name=runner_gitlab-ci-multi-runner
  26. baseurl=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/$basearch
  27. repo_gpgcheck=1
  28. gpgcheck=0
  29. enabled=1
  30. gpgkey=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
  31. sslverify=1
  32. sslcacert=/etc/pki/tls/certs/ca-bundle.crt
  33. metadata_expire=300
  34. [runner_gitlab-ci-multi-runner-source]
  35. name=runner_gitlab-ci-multi-runner-source
  36. baseurl=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/el/7/SRPMS
  37. repo_gpgcheck=1
  38. gpgcheck=0
  39. enabled=1
  40. gpgkey=https://packages.gitlab.com/runner/gitlab-ci-multi-runner/gpgkey
  41. sslverify=1
  42. sslcacert=/etc/pki/tls/certs/ca-bundle.crt
  43. metadata_expire=300

5.安装gitlab

  1. [root@cicd-gitlab ~]# yum -y install gitlab-ce
  2. # 自动安装最新版

6.查看gitlab版本

  1. [root@xingdian-git ~]# head -1 /opt/gitlab/version-manifest.txt
  2. gitlab-ce 13.6.1

7.Gitlab 配置登录链接

  1. #设置登录链接
  2. [root@cicd-gitlab ~]# vim /etc/gitlab/gitlab.rb
  3. ***
  4. ## GitLab URL
  5. ##! URL on which GitLab will be reachable.
  6. ##! For more details on configuring external_url see:
  7. ##! https://docs.gitlab.com/omnibus/settings/configuration.html#configuring-the-external-url-for-gitlab
  8. # 没有域名,可以设置为本机IP地址
  9. external_url 'http://10.0.1.86'
  10. ***
  11. [root@cicd-gitlab ~]# grep "^external_url" /etc/gitlab/gitlab.rb
  12. external_url 'http://10.0.1.86' #绑定监听的域名或IP

8.初始化 Gitlab

  1. [root@cicd-gitlab ~]# gitlab-ctl reconfigure
  2. .....

9.启动 Gitlab 服务

  1. [root@cicd-gitlab ~]# gitlab-ctl start
  2. [root@cicd-gitlab ~]# lsof -i:80
  3. COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME
  4. nginx 22500 root 7u IPv4 50923 0t0 TCP *:http (LISTEN)
  5. nginx 22501 gitlab-www 7u IPv4 50923 0t0 TCP *:http (LISTEN)

10.Gitlab服务管理

  1. [root@cicd-gitlab ~]# gitlab-ctl start # 启动所有 gitlab 组件;
  2. [root@cicd-gitlab ~]# gitlab-ctl stop # 停止所有 gitlab 组件;
  3. [root@cicd-gitlab ~]# gitlab-ctl restart # 重启所有 gitlab 组件;
  4. [root@cicd-gitlab ~]# gitlab-ctl status # 查看服务状态;
  5. [root@cicd-gitlab ~]# gitlab-ctl reconfigure # 初始化服务;
  6. [root@cicd-gitlab ~]# vim /etc/gitlab/gitlab.rb # 修改默认的配置文件;
  7. [root@cicd-gitlab ~]# gitlab-ctl tail # 查看日志;

11.登录Gitlab

  1. 在浏览器中输入 http://10.0.0.110,然后 change password: ,并使用root用户登录 即可 (后续动作根据提示操作)
  1. 1. 执行命令, 进入交互界面
  2. gitlab-rails console -e production
  3. 2. 执行如下命令, 修改root密码为你想要的密码, 无需重启即可完成设置
  4. u=User.where(id:1).first # u定义为root用户
  5. u.password='你的密码' # 设置root密码, 注意需要符合密码强度
  6. u.password_confirmation='你的密码' # 确认当前密码
  7. u.save! # 保存操作
  8. quit # 退出交互界面

设置中文:

版本仓库 - 图23

11.Gitlab部署https方式

  1. 创建私有密钥:
  1. [root@cicd-gitlab ~]# mkdir -p /etc/gitlab/ssl
  2. [root@cicd-gitlab ~]# openssl genrsa -out "/etc/gitlab/ssl/gitlab.example.com.key" 2048
  3. Generating RSA private key, 2048 bit long modulus
  4. ...............+++
  5. ...............................................................................+++
  6. e is 65537 (0x10001)
  1. 创建私有证书:
  1. [root@cicd-gitlab ~]# openssl req -new -key "/etc/gitlab/ssl/gitlab.example.com.key" -out "/etc/gitlab/ssl/gitlab.example.com.csr"
  2. You are about to be asked to enter information that will be incorporated
  3. into your certificate request.
  4. What you are about to enter is what is called a Distinguished Name or a DN.
  5. There are quite a few fields but you can leave some blank
  6. For some fields there will be a default value,
  7. If you enter '.', the field will be left blank.
  8. -----
  9. Country Name (2 letter code) [XX]:cn
  10. State or Province Name (full name) []:sh
  11. Locality Name (eg, city) [Default City]:sh
  12. Organization Name (eg, company) [Default Company Ltd]: #输入空格,然后回车
  13. Organizational Unit Name (eg, section) []: #输入空格,然后回车
  14. Common Name (eg, your name or your server's hostname) []:gitlab.example.com
  15. Email Address []:admin@example.com
  16. Please enter the following 'extra' attributes
  17. to be sent with your certificate request
  18. A challenge password []:123456
  19. An optional company name []: #直接回车
  20. 查看
  21. [root@cicd-gitlab ~]# ll /etc/gitlab/ssl/
  22. total 8
  23. -rw-r--r-- 1 root root 1066 Jan 2 15:32 gitlab.example.com.csr
  24. -rw-r--r-- 1 root root 1679 Jan 2 15:30 gitlab.example.com.key
  1. 创建CRT签署证书:
  1. [root@cicd-gitlab ~]# openssl x509 -req -days 365 -in "/etc/gitlab/ssl/gitlab.example.com.csr" -signkey "/etc/gitlab/ssl/gitlab.example.com.key" -out "/etc/gitlab/ssl/gitlab.example.com.crt"
  2. Signature ok
  3. subject=/C=cn/ST=sh/L=sh/O= /OU= /CN=gitlab.example.com/emailAddress=admin@example.com
  4. Getting Private key
  5. 查看
  6. [root@cicd-gitlab ~]# ll /etc/gitlab/ssl/
  7. total 12
  8. -rw-r--r-- 1 root root 1265 Jan 2 15:39 gitlab.example.com.crt
  9. -rw-r--r-- 1 root root 1066 Jan 2 15:32 gitlab.example.com.csr
  10. -rw-r--r-- 1 root root 1679 Jan 2 15:30 gitlab.example.com.key
  1. 创建pem证书: 利用openssl命令输出pem证书
  1. [root@gitlab ~]# openssl dhparam -out /etc/gitlab/ssl/dhparam.pem 2048
  2. Generating DH parameters, 2048 bit long safe prime, generator 2
  3. This is going to take a long time
  4. ........................................................+................................................................................+.....................................+..................................................................................+..............................................+..................................................................................................................................+..+........................................................................................................................................+..............................................................................................................................................................................+......+..............+.....................................................+.................+.......................................................................................+..+.................................................................................................................................................+..........................................................+.............+.........+...........................................................+........................................................................................................................................................................................................................................+...................................................................................................................................................................................................................................................................................................................++*++*
  1. 查看生成的证书:
  1. [root@cicd-gitlab ~]# ll /etc/gitlab/ssl/
  2. total 16
  3. -rw-r--r-- 1 root root 424 Jan 2 15:46 dhparam.pem
  4. -rw-r--r-- 1 root root 1265 Jan 2 15:39 gitlab.example.com.crt
  5. -rw-r--r-- 1 root root 1066 Jan 2 15:32 gitlab.example.com.csr
  6. -rw-r--r-- 1 root root 1679 Jan 2 15:30 gitlab.example.com.key
  1. 更改文件权限:
  1. [root@cicd-gitlab ~]# chmod 600 /etc/gitlab/ssl/*
  2. [root@cicd-gitlab ~]# ll /etc/gitlab/ssl/
  3. total 16
  4. -rw------- 1 root root 424 Jan 2 15:46 dhparam.pem
  5. -rw------- 1 root root 1265 Jan 2 15:39 gitlab.example.com.crt
  6. -rw------- 1 root root 1066 Jan 2 15:32 gitlab.example.com.csr
  7. -rw------- 1 root root 1679 Jan 2 15:30 gitlab.example.com.key
  1. 配置 gitlab
  1. [root@cicd-gitlab ~]# cp /etc/gitlab/gitlab.rb{,.bak}
  2. [root@cicd-gitlab ~]# vim /etc/gitlab/gitlab.rb
  3. ## 更改如下
  4. 13 external_url 'https://gitlab.example.com' 13行左右
  5. 952 nginx['redirect_http_to_https'] = true
  6. 964 nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt"
  7. 965 nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key"
  8. 979 # nginx['ssl_dhparam'] = "/etc/gitlab/ssl/dhparam.pem" # Path to dhparams.pem, eg. /etc/gitlab/ssl/dhparams.pem
  1. 初始化gitlab相关服务配置:
  1. [root@cicd-gitlab ~]# gitlab-ctl reconfigure
  2. Starting Chef Client, version 13.6.4
  3. resolving cookbooks for run list: ["gitlab"]
  4. Synchronizing Cookbooks:
  5. - gitlab (0.0.1)
  6. - package (0.1.0)
  7. - postgresql (0.1.0)
  8. - redis (0.1.0)
  9. - mattermost (0.1.0)
  10. - registry (0.1.0)
  11. - gitaly (0.1.0)
  12. - consul (0.0.0)
  13. - nginx (0.1.0)
  14. - runit (0.14.2)
  15. - letsencrypt (0.1.0)
  16. - acme (3.1.0)
  17. - crond (0.1.0)
  18. - compat_resource (12.19.0)
  19. Installing Cookbook Gems:
  20. Compiling Cookbooks...
  21. Recipe: gitlab::default
  22. * directory[/etc/gitlab] action create
  23. - change mode from '0755' to '0775'
  24. Converging 493 resources
  25. * directory[/etc/gitlab] action create (up to date)
  26. * directory[Create /var/opt/gitlab] action create
  27. - create new directory /var/opt/gitlab
  28. - change mode from '' to '0755'
  29. - change owner from '' to 'root'
  30. - change group from '' to 'root'
  31. * directory[/opt/gitlab/embedded/etc] action create
  32. - create new directory /opt/gitlab/embedded/etc
  33. - change mode from '' to '0755'
  34. - change owner from '' to 'root'
  35. - change group from '' to 'root'
  36. * template[/opt/gitlab/embedded/etc/gitconfig] action create
  37. - create new file /opt/gitlab/embedded/etc/gitconfig
  38. - update content in file /opt/gitlab/embedded/etc/gitconfig from none to 987af3
  39. 。。。。过程有点长,需要等一会(看个人服务器配置了)
  40. Running handlers:
  41. Running handlers complete
  42. Chef Client finished, 454/655 resources updated in 02 minutes 16 seconds
  43. gitlab Reconfigured!
  44. # 出现这个表示配置没有问题!
  1. 重启 gitlab
  1. [root@cicd-gitlab ~]# gitlab-ctl restart
  2. ok: run: alertmanager: (pid 6526) 1s
  3. ok: run: gitaly: (pid 6543) 0s
  4. ok: run: gitlab-monitor: (pid 6556) 0s
  5. ok: run: gitlab-workhorse: (pid 6579) 1s
  6. ok: run: logrotate: (pid 6589) 0s
  7. ok: run: nginx: (pid 6597) 1s
  8. ok: run: node-exporter: (pid 6681) 0s
  9. ok: run: postgres-exporter: (pid 6687) 1s
  10. ok: run: postgresql: (pid 6698) 0s
  11. ok: run: prometheus: (pid 6706) 0s
  12. ok: run: redis: (pid 6722) 0s
  13. ok: run: redis-exporter: (pid 6856) 0s
  14. ok: run: sidekiq: (pid 6866) 0s
  15. ok: run: unicorn: (pid 6880) 0s
  16. # 可以看出gitlab的所有服务重启完成

12.浏览器登录 gitlab

  1. [https://gitlab.example.com](https://gitlab.example.com)

版本仓库 - 图24

13.Gitlab 添加 SMTP 邮件功能

  1. 配置邮件功能:
  1. [root@cicd-gitlab ~]# grep -P "^[^#].*smtp_|user_email|gitlab_email" /etc/gitlab/gitlab.rb
  2. gitlab_rails['smtp_enable'] = true
  3. gitlab_rails['smtp_address'] = "smtp.163.com"
  4. gitlab_rails['smtp_port'] = 465
  5. gitlab_rails['smtp_user_name'] = "zhuangyaovip@163.com"
  6. gitlab_rails['smtp_password'] = "FULLECVMSVNBSDJH"
  7. gitlab_rails['smtp_domain'] = "163.com"
  8. gitlab_rails['smtp_authentication'] = "login"
  9. gitlab_rails['smtp_enable_starttls_auto'] = true
  10. gitlab_rails['smtp_tls'] = true
  11. gitlab_rails['gitlab_email_enabled'] = true
  12. gitlab_rails['gitlab_email_from'] = 'zhuangyaovip@163.com'
  13. gitlab_rails['gitlab_email_display_name'] = 'Example'
  14. gitlab_rails['gitlab_email_reply_to'] = '1311529042@qq.com'
  15. gitlab_rails['gitlab_email_subject_suffix'] = 'gitlab'
  16. # gitlab_rails['gitlab_email_smime_enabled'] = false
  17. # gitlab_rails['gitlab_email_smime_key_file'] = '/etc/gitlab/ssl/gitlab_smime.key'
  18. # gitlab_rails['gitlab_email_smime_cert_file'] = '/etc/gitlab/ssl/gitlab_smime.crt'
  19. # gitlab_rails['gitlab_email_smime_ca_certs_file'] = '/etc/gitlab/ssl/gitlab_smime_cas.crt'
  20. # user['git_user_email'] = "gitlab@#{node['fqdn']}"
  1. 停止gitlab服务:
  1. [root@cicd-gitlab ~]# gitlab-ctl stop
  2. ok: down: gitaly: 0s, normally up
  3. ok: down: gitlab-monitor: 1s, normally up
  4. ok: down: gitlab-workhorse: 0s, normally up
  5. ok: down: logrotate: 1s, normally up
  6. ok: down: nginx: 0s, normally up
  7. ok: down: node-exporter: 1s, normally up
  8. ok: down: postgres-exporter: 0s, normally up
  9. ok: down: postgresql: 0s, normally up
  10. ok: down: prometheus: 0s, normally up
  11. ok: down: redis: 0s, normally up
  12. ok: down: redis-exporter: 1s, normally up
  13. ok: down: sidekiq: 0s, normally up
  14. ok: down: unicorn: 1s, normally up
  1. 修改配置后需要初始化配置:
  1. [root@cicd-gitlab ~]# gitlab-ctl reconfigure
  2. ......
  1. 启动服务:
  1. [root@cicd-gitlab ~]# gitlab-ctl start
  2. ok: run: gitaly: (pid 37603) 0s
  3. ok: run: gitlab-monitor: (pid 37613) 0s
  4. ok: run: gitlab-workhorse: (pid 37625) 0s
  5. ok: run: logrotate: (pid 37631) 0s
  6. ok: run: nginx: (pid 37639) 1s
  7. ok: run: node-exporter: (pid 37644) 0s
  8. ok: run: postgres-exporter: (pid 37648) 1s
  9. ok: run: postgresql: (pid 37652) 0s
  10. ok: run: prometheus: (pid 37660) 1s
  11. ok: run: redis: (pid 37668) 0s
  12. ok: run: redis-exporter: (pid 37746) 0s
  13. ok: run: sidekiq: (pid 37750) 1s
  14. ok: run: unicorn: (pid 37757) 0s
  1. Gitlab 发送邮件测试:
  1. [root@cicd-gitlab ~]# gitlab-rails console
  2. Loading production environment (Rails 4.2.10)
  3. irb(main):001:0> Notify.test_email('1311529042@qq.com', 'Message Subject', 'Message Body').deliver_now
  4. Notify#test_email: processed outbound mail in 2219.5ms
  5. Sent mail to user@destination.com (2469.5ms)
  6. Date: Fri, 04 May 2018 15:50:10 +0800
  7. From: Admin <username@domain.cn>
  8. Reply-To: Admin <username@domain.cn>
  9. To: user@destination.com
  10. Message-ID: <5aec10b24cfaa_93933fee282db10c162d@vm1.mail>
  11. Subject: Message Subject
  12. Mime-Version: 1.0
  13. Content-Type: text/html;
  14. charset=UTF-8tt
  15. Content-Transfer-Encoding: 7bit
  16. Auto-Submitted: auto-generated
  17. X-Auto-Response-Suppress: All
  18. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
  19. <html><body><p>Message Body</p></body></html>
  20. => #<Mail::Message:70291731344240, Multipart: false, Headers: <Date: Fri, 04 May 2018 15:50:10 +0800>, <From: Admin <username@domain.cn>>, <Reply-To: Admin <username@domain.cn>>, <To: user@destination.com>, <Message-ID: <5aec10b24cfaa_93933fee282db10c162d@vm1.mail>>, <Subject: Message Subject>, <Mime-Version: 1.0>, <Content-Type: text/html; charset=UTF-8>, <Content-Transfer-Encoding: 7bit>, <Auto-Submitted: auto-generated>, <X-Auto-Response-Suppress: All>>
  21. irb(main):002:0>quit
  1. 邮箱客户端查看邮件:

版本仓库 - 图25

五:Gitlab 开发代码提交处理流程

1.公司代码提交合并流程

  1. PM(项目主管/项目经理)在gitlab创建任务,分配给开发人员
  2. 开发人员领取任务后,在本地使用git clone拉取代码库
  3. 开发人员创建开发分支(git checkout -b dev),并进行开发
  4. 开发人员完成之后,提交到本地仓库(git commit
  5. 开发人员在gitlab界面上申请分支合并请求(Merge request
  6. PMgitlab上查看提交和代码修改情况,确认无误后,确认将开发人员的分支合并到主分支(master
  7. 开发人员在gitlabMark done确认开发完成,并关闭