1. GitLab简介
GitLab 是一个用于仓库管理系统的开源项目。使用Git作为代码管理工具,并在此基础上搭建起来的web服务。可通过Web界面进行访问公开的或者私人项目。它拥有与Github类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用。
常用的网站:
官网:https://about.gitlab.com/
国内镜像:https://mirrors.tuna.tsinghua.edu.cn/gitlab‐ce/yum/
安装环境:
1、 CentOS 6或者7
2、 2G内存(实验)生产(至少4G)
3、 安装包:gitlab‐ce‐10.2.2‐ce
4、 禁用防火墙,关闭selinux
2. 安装配置
https://about.gitlab.com/installation/#centos‐7 # git官网
yum install ‐y curl policycoreutils‐python openssh‐server # 安装依赖
2.1 官方安装文档
gitlab官网:https://about.gitlab.com/
选择需要在那个系统上安装,我们这里选择CentOS7
2.2 安装和配置文件
[root@git ~]#: rpm -ivh gitlab-ce-11.9.9-ce.0.el7.x86_64.rpm
警告:gitlab-ce-11.9.9-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:gitlab-ce-11.9.9-ce.0.el7 ################################# [100%]
It looks like GitLab has not been configured yet; skipping the upgrade script.
*. *.
*** ***
***** *****
.****** *******
******** ********
,,,,,,,,,***********,,,,,,,,,
,,,,,,,,,,,*********,,,,,,,,,,,
.,,,,,,,,,,,*******,,,,,,,,,,,,
,,,,,,,,,*****,,,,,,,,,.
,,,,,,,****,,,,,,
.,,,***,,,,
,*,.
_______ __ __ __
/ ____(_) /_/ / ____ _/ /_
/ / __/ / __/ / / __ `/ __ \
/ /_/ / / /_/ /___/ /_/ / /_/ /
\____/_/\__/_____/\__,_/_.___/
Thank you for installing GitLab!
GitLab was unable to detect a valid hostname for your instance.
Please configure a URL for your GitLab instance by setting `external_url` 配置你的URL在external_url选项里
configuration in /etc/gitlab/gitlab.rb file. 通过这个文件里配置你的URL
Then, you can start your GitLab instance by running the following command:
sudo gitlab-ctl reconfigure 用这个命令重新配置gitlab
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
[root@git ~]#: vim /etc/gitlab/gitlab.rb
13 external_url 'http://10.0.0.20' 配置你的ip地址
[root@git ~]#: gitlab-ctl reconfigure
看到这个结果就显示配置成功
Running handlers:
Running handlers complete
Chef Client finished, 473/1268 resources updated in 03 minutes 36 seconds
gitlab Reconfigured!
Gitlab配置文件说明
gitlab‐ctl reconfigure # 更改配置文件后需重新配置
/opt/gitlab/ # gitlab的程序安装目录
/var/opt/gitlab # gitlab目录数据目录
/var/opt/gitlab/git‐dfata # 存放仓库数据
gitlab‐ctl status # 查看目前gitlab所有服务运维状态
gitlab‐ctl stop # 停止gitlab服务
gitlab‐ctl stop nginx # 单独停止某个服务
gitlab‐ctl tail # 查看所有服务的日志
Gitlab的服务构成:
nginx: 静态web服务器
gitlab‐workhorse: 轻量级的反向代理服务器
logrotate:日志文件管理工具
postgresql:数据库
redis:缓存数据库
sidekiq:用于在后台执行队列任务(异步执行),(Ruby)
unicorn:An HTTP server for Rack applications,GitLab Rails应用是托管在这个服务器上面的。(Ruby
Web Server,主要使用Ruby编写)
Gitlab汉化:
1、下载汉化补丁
[root@git ~]#:git clone https://gitlab.com/xhang/gitlab.git
2、查看版本信息
[root@git ~]#: cat gitlab/VERSION
11.10.0
[root@git ~]#: cd gitlab
3、对比版本、生成补丁包
[root@git gitlab]#: git diff v11.10.0 v11.10.0-zh > ../11.10.0-zh.diff
4、停止服务器
gitlab‐ctl stop
5、打补丁
patch ‐d /opt/gitlab/embedded/service/gitlab‐rails ‐p1 < /root/11.10.0‐zh.diff
6、启动和重新配置
gitlab‐ctl start
gitlab‐ctl reconfigure
下面在浏览器中输入刚才配置的URL信息登陆
2.3 设置外观
重新登陆一下看看效果
3. Gitlab配置
如果之前没有gitlab,就要按照先创建组再创建项目,创建人员再添加到组里。也就是说项目属于组的,把要管理项目的人员也添加到组里,那这个人员就可以管理这个项目。
3.1 创建一个组
3.2 创建一个项目
这个就跟之前创建github的界面差不多了
3.3 删除之前github创建的
[root@git ~]#: cd git_data/
[root@git git_data]#: git remote
origin
[root@git git_data]#: git remote remove origin
[root@git git_data]#: git remote
3.4 创建远程仓库
[root@git git_data]#: git remote add origin git@10.0.0.20:test/git_data.git
3.5 添加ssh认证并推送代码
[root@git ~]#: cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC41tBVR4hlbt3UBdRHDZrHlZWPVUtukum6Etseub4N826QhKeZ/BZmtJu/dUgi322odazuObUU6Z1ee5PV37DLREyayrevkcG/VwOycGaq4J78029wMW5Kq1mDvHGlXgw2x5kFl0s+6dtMSECudzuOAZYrS9QgaCP+h/hSB8JKD/rKW56QvEQ9hpK/wRdgF/xNgDao0EPxwjGgALZbIPYzu41jqGjFvavf1Ucu5EicJelw8ZDc6e3MQ8RvioC42vlroC87B/6tsQjLlp5Y1WZFBm9tpzgWoZ2EKhzRROC7LVEbPF98cM1o1DxL5kMfM7T3bP7G1dE8LrQ/3HsNk5NH root@git
[root@git git_data]#: git push -u origin master
The authenticity of host '10.0.0.20 (10.0.0.20)' can't be established.
ECDSA key fingerprint is SHA256:D7wN1reekHLE8oukJlQ+vEI/uqO4ideb7yfInM9VSu8.
ECDSA key fingerprint is MD5:8f:3e:85:3a:bc:5b:c2:e1:e4:d2:76:11:8a:71:b7:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.20' (ECDSA) to the list of known hosts.
Counting objects: 3, done.
Writing objects: 100% (3/3), 199 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.20:test/git_data.git
* [new branch] master -> master
Branch master set up to track remote branch master from origin.
我们这么做的目的就是实现共同开发,让所有的开发人员都能连接带我的gitlab
3.6 开通用户
设置密码
新开一个窗口用dev用户登陆
3.7 把用户添加到组里
4. 开启另外一台jenkins服务器添加ssh
[root@jenkins ~]#: ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Created directory '/root/.ssh'.
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:z7THM9Ox8COx9A/GmTCm/u3kMwZrETXBPdP5+DqL+P4 root@jenkins
The key's randomart image is:
+---[RSA 2048]----+
| ..oo|
| ++o|
| . o+|
| . . .|
| S . B... |
| + *o@ =.|
| = O+&. |
| . oo@*+ |
| o==+E=.|
+----[SHA256]-----+
[root@jenkins ~]#: cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/+BBsKUrJ5dICUQVXOC2EpUdEmzbTJRv78XOWNsE1jvEGTq7yG7cILw2Gij9Nd8cuD7phQ609vW+eJRlZEsBv8T9jNIMcw6gmGs6POIxpjiYKhWCf+NQO4l15HwWF63iD1toT/11LGn7t83hpokF6NWElKxdw1RcGtFO+Fjf90EV9v/XjttOWG2u4kJ+HuNf38XWeiZkGAnpki3vV8jqc9nS9BYDfUsNKTlkUuWstomniycJw+H4TiPEUIxOxz/oQpP5R6CmOwHypca1hXWNDBgf0cXFv90SErTQu0kLGSY60RRM+tKuF5hdFpaUk0m7Oqvc3AHlo6KwNXltMUGpB root@jenkins
添加ssh密钥
克隆代码
[root@jenkins ~]#: git clone git@10.0.0.20:test/git_data.git
Cloning into 'git_data'...
The authenticity of host '10.0.0.20 (10.0.0.20)' can't be established.
ECDSA key fingerprint is SHA256:D7wN1reekHLE8oukJlQ+vEI/uqO4ideb7yfInM9VSu8.
ECDSA key fingerprint is MD5:8f:3e:85:3a:bc:5b:c2:e1:e4:d2:76:11:8a:71:b7:c9.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.20' (ECDSA) to the list of known hosts.
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0
4.1 jenkins配置git
[root@jenkins git_data]#: git config --global user.email "dev@email.com"
[root@jenkins git_data]#: git config --global user.name "dev"
推送代码
[root@jenkins git_data]#: echo 11 > test.txt
[root@jenkins git_data]#: git commit -am 'add'
[root@jenkins git_data]#: git push -u origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote: GitLab: You are not allowed to push code to protected branches on this project.
To git@10.0.0.20:test/git_data.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'git@10.0.0.20:test/git_data.git'
测试环境下gitlab默认开启了保护分支,不让dev用户推送代码到master主分支上。
关闭保护分支在管理端操作
再次推送
[root@jenkins git_data]#: git push -u origin master
Counting objects: 5, done.
Writing objects: 100% (3/3), 227 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.20:test/git_data.git
5929a40..ac73233 master -> master
Branch master set up to track remote branch master from origin.
5 保护分支
不允许开发用户直接往master推送。只能推送到dev分支上,然后进行请求合并,用管理员用户在进行代码合并。
管路员用户端操作
5.1 创建dev分支推送代码
[root@jenkins git_data]#: git checkout -b dev
Switched to a new branch 'dev'
[root@jenkins git_data]#: echo 22 >> test.txt
[root@jenkins git_data]#: git commit -am 'add 2'
[dev 2562c60] add 2
1 file changed, 1 insertion(+)
[root@jenkins git_data]#: git push -u origin dev
Counting objects: 5, done.
Writing objects: 100% (3/3), 231 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.20/test/git_data/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.20:test/git_data.git
* [new branch] dev -> dev
Branch dev set up to track remote branch dev from origin.
6 合并分支
6.1 第一次合并分支dev用户操作
管理端
6.2 第二次合并
删除dev用户并切换到master
[root@jenkins git_data]#: git checkout master
Switched to branch 'master'
[root@jenkins git_data]#: git branch
dev
* master
[root@jenkins git_data]#: git branch -d dev
warning: deleting branch 'dev' that has been merged to
'refs/remotes/origin/dev', but not yet merged to HEAD.
Deleted branch dev (was 2562c60).
[root@jenkins git_data]#: git branch
* master
拉取新的代码,在创建因分支前一定要保持master代码时最新的
[root@jenkins git_data]#: cat test.txt
11
[root@jenkins git_data]#: git pull
remote: Enumerating objects: 2, done.
remote: Counting objects: 100% (2/2), done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 2 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (2/2), done.
From 10.0.0.20:test/git_data
ac73233..9fa10c8 master -> origin/master
* [new branch] revert-9fa10c88 -> origin/revert-9fa10c88
Updating ac73233..9fa10c8
Fast-forward
test.txt | 1 +
1 file changed, 1 insertion(+)
[root@jenkins git_data]#: cat test.txt
11
22
创建新分支推送代码
[root@jenkins git_data]#: git checkout -b dev
Switched to a new branch 'dev'
[root@jenkins git_data]#: echo 111 > test.txt
[root@jenkins git_data]#: git commit -am 'add test'
[root@jenkins git_data]#: git push -u origin dev
管理端操作