22.13 搭建git服务器
22.14 22.15 安装gitlab
22.16 使用gitlab
22.17 gitlab备份与恢复
22.13 搭建git服务器
github毕竟是公开的,而私有仓库又得花钱买。所以我们可以想办法搭建一个私有的,只自己公司使用的。Gitlab是个不错的选择。在介绍它之前,先讲述一下命令行的git服务器
搭建准备工作
1 找一台服务器,首先要安装git,
yum install -y git
2 添加git用户,并且设置shell为/usr/bin/git-shell,目的是为了不让git用户远程登陆
useradd -s /usr/bin/git-shell git
cd /home/git
3 创建authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥
mkdir .ssh
touch .ssh/authorized_keys
chown -R git.git .ssh
chmod 600 .ssh/authorized_keys
4 把客户端的公钥,放到服务器的/root/.ssh/authorized_keys上,失效免密登录
[root@9F-VM1 ~]# cat .ssh/id_rsa.pub
Nlet2N+TXAQJDOnrRtN42qFoRLAYXO+D8xsYDmwPMLdJymcYBbQJL5jU65/+loL7kuHcTtRSwk46xQXdLFPjN/c2c0I6OofxZAVKgPHGtjd8Y8dkOzYep6S6n7C/jug5PwbQedFoNy16+0JU1lBQa6IxqrWsXdLuM3Gc8Oko4sAvKQgZQRfJh root@9F-VM1
[root@DD-Server-9F ~]# vim !$
vim .ssh/authorized_keys
#9F-VM1
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC7TRMjzg1SiLIZg3X3HBVaVzvfCMouDV/d0owu/SLddyHAitEvii3Nq1ElOhuB2hDbVq9fhEH61ZuBVqI+PkcI0KE04p8/KCSzF4dEvUdIYrQAeafxnLd7hl4QofmJbEnP07FQOurFFXItnniGz12mN95cbvijoLPNlet2N+TXA
5 登录验证测试
[root@9F-VM1 ~]# ssh [email protected]
Last login: Tue Sep 4 16:16:31 2018 from 192.88.29.250
fatal: Interactive git shell is not enabled.
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.88.24.200 closed.
不能直接远程通过登录的,但是能验证到客户端29.250的公钥,稍后会再做说明。
在服务器创建git仓库
6定好存储git仓库的目录,比如 /data/git
mkdir /data/git
cd /data/git
7 初始化仓库
[root@DD-Server-9F git]# git init --bare sample.git
初始化空的 Git 版本库于 /data/git/sample.git/
# 会创建一个裸仓库,裸仓库没有工作区,因为服务器上的Git仓库纯粹是为了共享,所以不让用户直接登录到服务器上去改工作区,并且服务器上的Git仓库通常都以.git结尾
chown -R git.git sample.git
以上操作是在git服务器上做的,平时git服务器是不需要开发人员登录修改代码的,它仅仅是充当着一个服务器的角色,就像github一样,平时操作都是在我们自己的pc上做的
8 客户端使用仓库
首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里
在客户端上(自己pc)克隆远程仓库
执行命令: git clone git@ip:/data/git/sample.git
[root@9F-VM1 git]# cd /data/git
[root@9F-VM1 git]# git clone [email protected]:/data/git/sample.git
正克隆到 'sample'...
warning: 您似乎克隆了一个空版本库。
[root@9F-VM1 git]# cd sample/
[root@9F-VM1 sample]# ls -la
总用量 0
drwxr-xr-x 3 root root 18 9月 4 16:41 .
drwxr-xr-x 9 root root 131 9月 4 16:41 ..
drwxr-xr-x 7 root root 119 9月 4 16:41 .git
此时就可以在当前目录下生成一个sample的目录,这个就是我们克隆的远程仓库了。进入到这里面,可以开发一些代码,然后push到远程。
9 测试
添加文件实现远程仓库功能
[root@9F-VM1 sample]# touch 1.txt && echo -e "111\n222\n333" >> 1.txt
[root@9F-VM1 sample]# cat 1.txt
111
222
333
[root@9F-VM1 sample]# git add 1.txt
[root@9F-VM1 sample]# git commit -m "add the first file"
[master(根提交) e46c02a] add the first file
1 file changed, 3 insertions(+)
create mode 100644 1.txt
[root@9F-VM1 sample]# git push origin master
Counting objects: 3, done.
Writing objects: 100% (3/3), 219 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/data/git/sample.git
* [new branch] master -> master
9.1 在别的目录克隆远程仓库,查看协同效果是否实现
[root@9F-VM1 git]# cd /data/git
[root@9F-VM1 git]# mkdir sample-t
[root@9F-VM1 git]# cd sample-t/
[root@9F-VM1 sample-t]# git clone [email protected]:/data/git/sample.git
正克隆到 'sample'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 0 (delta 0)
接收对象中: 100% (3/3), done.
[root@9F-VM1 sample-t]# ls
sample
[root@9F-VM1 sample-t]# cd sample/
[root@9F-VM1 sample]# ls
1.txt
[root@9F-VM1 sample]# vim 2.txt
[root@9F-VM1 sample]# echo "222" > 2.txt
[root@9F-VM1 sample]# ls
1.txt 2.txt
[root@9F-VM1 sample]# git add 2.txt
[root@9F-VM1 sample]# git commit -m "add 2.txt"
[master 0f7b313] add 2.txt
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@9F-VM1 sample]# git push
Counting objects: 4, done.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 261 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To [email protected]:/data/git/sample.git
e46c02a..0f7b313 master -> master
切换原来开始的仓库目录
[root@9F-VM1 sample]# cd -
/data/git/sample
[root@9F-VM1 sample]# ls
1.txt
[root@9F-VM1 sample]# git pull
remote: Counting objects: 4, done.
remote: Compressing objects: 100% (2/2), done.
remote: Total 3 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (3/3), done.
来自 192.88.24.200:/data/git/sample
e46c02a..0f7b313 master -> origin/master
更新 e46c02a..0f7b313
Fast-forward
2.txt | 1 +
1 file changed, 1 insertion(+)
create mode 100644 2.txt
[root@9F-VM1 sample]# ls
1.txt 2.txt
协同实现,远程仓库项目搭建完成
22.14 安装gitlab(上)
安装方法一:
gitlab官网 https://about.gitlab.com/gitlab-com/
官方安装文档 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)
要求服务器内存不少于2g
方法二:本地yum源安装
vim /etc/yum.repos.d/gitlab.repo //加入如下内容
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
yum install -y gitlab-ce
gitlab-ctl reconfigure
22.15 安装gitlab(下)
执行命令自动加载配置
gitlab-ctl reconfigure
如果系统自带nginx,先把它停掉,因为gitlab也自带了一个nginx,以免发生冲突
/etc/init.d/nginx stop
chkconfig nginx off
netstat -lnpt //查看监听端口
gitlab-ctl 后面带的执行命令(停止,开始,重启,状态)
gitlab-ctl stop/restart/start/status
访问gitlab服务器ip:192.88.24.200
第一次登录需要创建新的密码
改完密码后需要登录
默认账户:root
密码:新的密码
截图
22.16 使用gitlab
gitlab常用命令 https://www.cnyunwei.cc/archives/1204
1 使用gitlab自带的nginx配置代理
路径:
/var/opt/gitlab/nginx/conf/nginx.conf
#主配置文件
/var/opt/gitlab/nginx/conf/gitlab-http.conf
#gitlab相关的web配置文件,修改相关gitlab web可以在这里修改
2 创建新的组group
截图
3 创建新的项目project
截图
4 在gitlab添加SSH Key
User Settings-->SSH key-->把公钥复制进去
截图
5 添加组
管理员区域 添加用户,组,项目
截图
6 添加用户
密码获取方法一:新的用户密码会使用链接方式将密码发送通知给邮件地址来获取
截图
密码获取方法二:选中用户-->Edit下直接编辑定义密码
截图
7 新用户登录
创建项目project
22.17 gitlab备份与恢复
gitlab备份
gitlab-rake gitlab:backup:create
[root@DD-Server-9F ~]# gitlab-rake gitlab:backup:create
Dumping database ...
Dumping PostgreSQL database gitlabhq_production ... [DONE]
done
Dumping repositories ...
* g1/p1 ... [SKIPPED]
[SKIPPED] Wiki
* kevin/kp01 ... [SKIPPED]
[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: 1536129054_2018_09_05_11.2.3_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 ... skipping
备份目录在/var/opt/gitlab/backups
[root@DD-Server-9F ~]# ls /var/opt/gitlab/backups/
1536129054_2018_09_05_11.2.3_gitlab_backup.tar
gitlab 恢复
gitlab 恢复 先停服务 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
[root@DD-Server-9F ~]# gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
ok: down: unicorn: 1s, normally up
ok: down: sidekiq: 0s, normally up
停掉服务后,继续恢复
gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀,时间戳_日期_版本号)