1.服务器准备
服务器 虚拟机 纯干净的系统
10.0.0.100 Gitlab 1核2G 20G硬盘
10.0.0.101 Jenkins 1核1G 20G硬盘
10.0.0.·02 Nexus 1核2G 20G硬盘
10.0.0.103 Sonar 1核2G 20G硬盘
10.0.0.20 Web 1核1G 20G硬盘
2.Git安装
①. 系统环境准备
[root@Gitlab ~] cat /etc/redhat-release #查看系统版本
CentOS Linux release 7.5.1804 (Core)
[root@Gitlab ~]uname -r #查看内核版本
3.10.0-862.el7.x86_64
[root@Gitlab ~] getenforce #确认selinux关闭状态
Disabled
[root@Gitlab ~]systemctl stop firewalld #关闭防火墙
②Git安装部署
系统默认自带git
[root@Gitlab ~]# git --version
git version 1.8.3.1
若无git,可使用命令安装
[root@Gitlab ~]yum -y install git
[root@Gitlab ~]# git config
#--global 使用全局配置文件
#--system 使用系统级配置文件
#--local 使用版本库级配置文件
[root@Gitlab ~]git config --global user.name "deman"
#配置git使用用户
[root@Gitlab ~]git config --global user.email "deman_zhao@163.com"
#配置git使用邮箱
[root@Gitlab ~]git config --global color.ui true
#语法高亮
[root@Gitlab ~]git config --list
user.name=deman
user.email=deman_zhao@163.com
color.ui=true
[root@Gitlab ~]cat .gitconfig
[user]
name = deman
email = deman_zhao@163.com
[color]
ui = true
③git初始化
[root@Gitlab ~]ll
总用量 4
-rw-------. 1 root root 1578 11月 15 12:41 anaconda-ks.cfg
[root@Gitlab ~]rm -rf *
[root@Gitlab ~]ll
总用量 0
[root@Gitlab ~]mkdir git_data
[root@Gitlab ~]ll
总用量 0
drwxr-xr-x 2 root root 6 11月 27 21:27 git_data
[root@Gitlab ~]cd git_data/
[root@Gitlab git_data]ll
总用量 0
[root@Gitlab git_data]git init
初始化空的 Git 版本库于 /root/git_data/.git/
[root@Gitlab git_data]ll
总用量 0
[root@Gitlab git_data]ll -a
总用量 0
drwxr-xr-x 3 root root 18 11月 27 21:28 .
dr-xr-x---. 4 root root 174 11月 27 21:27 ..
drwxr-xr-x 7 root root 119 11月 27 21:28 .git
3.gitlab安装及使用
安装
常用的网站:
官网: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
①这里已有下载后的rpm安装包,传输到服务器中用rpm安装
[root@Gitlab git_data]rz -y
[root@Gitlab git_data]ll
总用量 380624
-rw-r--r-- 1 root root 389758391 11月 17 20:29 gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
[root@Gitlab git_data]rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
警告:gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
错误:依赖检测失败:
policycoreutils-python 被 gitlab-ce-10.2.2-ce.0.el7.x86_64 需要
[root@Gitlab git_data] yum -y install policycoreutils-python #rmp安装gitlab前需要先安装policycoreutils-python
如下为安装成功
[root@Gitlab git_data]rpm -ivh gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm
警告:gitlab-ce-10.2.2-ce.0.el7.x86_64.rpm: 头V4 RSA/SHA1 Signature, 密钥 ID f27eab47: NOKEY
准备中... ################################# [100%]
正在升级/安装...
1:gitlab-ce-10.2.2-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`
configuration in /etc/gitlab/gitlab.rb file.
Then, you can start your GitLab instance by running the following command:
sudo gitlab-ctl reconfigure
For a comprehensive list of configuration options please see the Omnibus GitLab readme
https://gitlab.com/gitlab-org/omnibus-gitlab/blob/master/README.md
②安装成功之后在/etc/gitlab/gitlab.rb file文件中配置IP地址,若不更改也可将“http://gitlab.example.com”复制在本地host文件中做一下解析
图片.png更改完成之后需用命令重新配置一下(过程有点长,耐心等待。。。)
[root@Gitlab ~]gitlab-ctl reconfigure
如下提示为成功状态
Running handlers:
Running handlers complete
Chef Client finished, 382/541 resources updated in 03 minutes 45 seconds
gitlab Reconfigured!
[root@Gitlab ~]gitlab-ctl status #查看目前gitlab所有服务运维状态
run: gitaly: (pid 3849) 174s; run: log: (pid 3512) 277s
run: gitlab-monitor: (pid 3864) 173s; run: log: (pid 3574) 265s
run: gitlab-workhorse: (pid 3836) 174s; run: log: (pid 3470) 291s
run: logrotate: (pid 3497) 283s; run: log: (pid 3496) 283s
run: nginx: (pid 3482) 289s; run: log: (pid 3481) 289s
run: node-exporter: (pid 3560) 271s; run: log: (pid 3559) 271s
run: postgres-exporter: (pid 3884) 172s; run: log: (pid 3659) 247s
run: postgresql: (pid 3249) 370s; run: log: (pid 3248) 370s
run: prometheus: (pid 3873) 172s; run: log: (pid 3605) 253s
run: redis: (pid 3189) 376s; run: log: (pid 3188) 376s
run: redis-exporter: (pid 3588) 259s; run: log: (pid 3587) 259s
run: sidekiq: (pid 3453) 298s; run: log: (pid 3452) 298s
run: unicorn: (pid 3415) 304s; run: log: (pid 3414) 304s
以上服务均正常
③在浏览器中输入10.0.0.100即可访问gitlab页面,在如下页面配置密码12345678,然后登陆 图片.png 图片.png 图片.png ④修改外观 图片.pngPS:在gitlab中先创建组,在组中创建项目,将创建的用户加入组中,则在此组中的用户有权限管理相关组中的项目。
使用
①创建组 图片.png 图片.png
②创建项目 图片.png 图片.png
③添加公钥获得免秘钥上传和下载代码权限
图片.png在服务器中生成公钥
[root@Gitlab ~]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:tA6+1CZk4eM9qZ6MW/cYQ+GwkXqYfItfJnn9rZtmm5U root@Gitlab
The key's randomart image is:
+---[RSA 2048]----+
| |
| . |
| = o |
| . = B o |
| = X S |
| O X o . |
| . X # . E |
| * % * .o= |
| ooB . .oB+. |
+----[SHA256]-----+
[root@Gitlab ~]cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDyWud5CS7j5rRw7ONntM1cGaHjOe5qDm9ThL3z6e2w1olZFaH04+ZsNi9M5xg46kfsQv9h/RdAej3Id+tlJ7TeFbyEE7zjIOiXbQaMdQzMVRuuOJOVgHcdMgyZBowE8YLDudP3R7gSgeUpZ2e1NCld3trGDnVcQWzNj+hKDSJ9LY4xxnagT7zjgbm2p1gnu8zQ8xMydJNAviJtKEAGL0v/YSvuulDgr/cfQAYFTDDgvP/8GaPcLdjaA1rWfNlGz2Lki6/w9G7AYCVI3zyyJRnZIL3lxD7aqngf8Rs/0yLwTIrMTHJjcMZma75LENkMVGSWq2wf6ONEIxTDXFzKV4+b root@Gitlab
将公钥添加到gitlab中
图片.png
④在之前创建好的git_data本地仓库中连接gitlab远程仓库 图片.png
[root@Gitlab git_data]git remote add origin git@10.0.0.100:test/git_data.git #连接远程仓库
[root@Gitlab git_data]touch test.txt
[root@Gitlab git_data]ll
总用量 0
-rw-r--r-- 1 root root 0 11月 28 15:03 test.txt
[root@Gitlab git_data]git add . test.txt
[root@Gitlab git_data]git commit -m "newfile"
[master a6c0d6f] newfile
1 file changed, 1 insertion(+)
create mode 100644 test.txt
[root@Gitlab git_data]git push -u origin master #将本地仓库代码推送到远程仓库
⑤创建普通用户
如下为普通用户访问地址方式 图片.png在gitlab中创建一个普通用户 图片.png 图片.png
设置密码 图片.png 将dev用户加入到test组中 图片.png
图片.png 使用普通用户dev账号登录gitlab 图片.png 图片.png 点击项目 git_data,如下图,若使用客户端访问dev账号gitlab,需要配置客户端公钥 图片.png
公钥配置完成后,即可克隆代码到客户端(这里暂时用Jenkins主机充当客户端)
[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:xvGy9uWR9GlG6Qn8IZWV3S1ymJ9bjk/2S0RmjclJteU root@Jenkins
The key's randomart image is:
+---[RSA 2048]----+
| o..O|
| +ooOB|
| . +BBE|
| . o . .*..|
| S . = +* |
| . o . O+++|
| o + O=.|
| . . o +. o|
| . . ..|
+----[SHA256]-----+
[root@Jenkins ~]cat .ssh/id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDHmcdT91wCEWyIO9guG8SnuywZwvOD1LjLDRktb6f/bNKokBSP4MWElzQzw0YKNUymJqna1bcCnHsf17ka5RZQWNkJNP6jOERhMOEW3/5sWLEBJVXPj0o1xf6bEesSfl8bXYZOmN/Thq9/b+Hgc9y5Pv1JeQkJ3xsCzuVzb/BSkXjZi0k7JkvEBCfnY0Nu9XuWLupO8p+ZB1bYKhLrkAoLTywJAS1euNijn2tEtJaNSY/b3hU3ojkoN+KBdT+BgSc+qCFJGI/a/yOu2Y/JCcmc2Utt6UBLpPeAWGzCFqKjpbu9ON1GNlk0g9ZNW2Q2SeUZVJ24R4TInwTiDSdk3LJ7 root@Jenkins
[root@Jenkins ~]git clone git@10.0.0.100:test/git_data.git
正克隆到 'git_data'...
The authenticity of host '10.0.0.100 (10.0.0.100)' can't be established.
ECDSA key fingerprint is SHA256:oGGXZ/dEdBdi4eLCvCGHzO9uYmjhjMfRxYiyxb8cFJY.
ECDSA key fingerprint is MD5:54:9c:ff:65:69:5f:4d:a2:b7:b8:d7:de:79:c5:62:da.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '10.0.0.100' (ECDSA) to the list of known hosts.
remote: Counting objects: 8, done.
remote: Compressing objects: 100% (3/3), done.
remote: Total 8 (delta 0), reused 0 (delta 0)
接收对象中: 100% (8/8), done.
[root@Jenkins ~]ll
总用量 4
-rw-------. 1 root root 1579 11月 15 15:22 anaconda-ks.cfg
drwxr-xr-x 3 root root 34 11月 28 17:37 git_data
配置客户端本地仓库信息,并测试修改信息及上传到远程仓库gitlab
[root@Jenkins git_data]git config --global user.email "123@example.com"
[root@Jenkins git_data]git config --global user.name "job"
[root@Jenkins git_data]touch dev.txt
[root@Jenkins git_data]git add .
[root@Jenkins git_data]git commit -m "newfile dev.txt"
[master ac6de49] newfile dev.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 dev.txt
[root@Jenkins git_data]git push -u origin master
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 264 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@10.0.0.100:test/git_data.git
a6c0d6f..ac6de49 master -> master
分支 master 设置为跟踪来自 origin 的远程分支 master。
如下dev.txt文件提交成功
图片.png
PS:正常情况下,开发人员使用客户端是不允许直接修改master,不允许往主分支上提交代码(除root用户),应先创建一个分支,在创建的分支上修改代码或创建代码,然后发起合并。故需要做master保护,除root用户,不允许其他人在master分支上做修改。
在root用户下登录gitlab,如下设置master保护 图片.png 图片.png 图片.png此时普通用户已无权限在master上提交数据到远程仓库
remote: GitLab: You are not allowed to push code to protected branches on this project.
[root@Jenkins git_data]touch a.txt
[root@Jenkins git_data]git add .
[root@Jenkins git_data]git commit -m "newfile a.txt"
[master efc91aa] newfile a.txt
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a.txt
[root@Jenkins git_data]git push -u origin master
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 256 bytes | 0 bytes/s, done.
Total 2 (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.100:test/git_data.git
! [remote rejected] master -> master (pre-receive hook declined)
error: 无法推送一些引用到 'git@10.0.0.100:test/git_data.git'
创建并切换到dev分支,在dev分支上提交数据
[root@Jenkins git_data]git checkout -b dev
切换到一个新分支 'dev'
[root@Jenkins git_data]ll
总用量 4
-rw-r--r-- 1 root root 0 11月 28 18:19 a.txt
-rw-r--r-- 1 root root 0 11月 28 17:44 dev.txt
-rw-r--r-- 1 root root 6 11月 28 17:37 test.txt
[root@Jenkins git_data]git branch
* dev
master
[root@Jenkins git_data]git push -u origin dev
Counting objects: 3, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (2/2), 256 bytes | 0 bytes/s, done.
Total 2 (delta 0), reused 0 (delta 0)
remote:
remote: To create a merge request for dev, visit:
remote: http://10.0.0.100/test/git_data/merge_requests/new?merge_request%5Bsource_branch%5D=dev
remote:
To git@10.0.0.100:test/git_data.git
* [new branch] dev -> dev
分支 dev 设置为跟踪来自 origin 的远程分支 dev。
此时dev用户下的gitlab上就多出了dev分支数据
图片.png
网友评论