22.13 搭建git服务器html
22.14/22.15 安装gitlabnode
22.16 使用gitlabpython
22.17 gitlab备份和恢复mysql
svn的钩子 http://coolnull.com/1716.htmllinux
gitlab修改端口 http://blog.csdn.net/arybd/article/details/54635295nginx
修改主机名 http://www.mamicode.com/info-detail-1316828.htmlgit
第三方邮件 http://blog.csdn.net/liuruiqun/article/details/50000213github
server ssh 端口并非22 http://www.cnblogs.com/limx/p/5709101.html http://www.linuxidc.com/Linux/2017-02/141043.htm redis
应该修改 /opt/gitlab/embedded/service/gitlab-rails/config/gitlab.ymlsql
# If you use non-standard ssh port you need to specify it
ssh_port: xxxxx
gitlab的钩子相关配置 http://fighter.blog.51cto.com/1318618/1670667
22.13 搭建git服务器(私有仓库)
1.github毕竟是公开的,而私有仓库又得花钱买。因此咱们能够想办法搭建一个私有的,只本身公司使用的。Gitlab是个不错的选择。
#git虽然是分布式的,能够在本身的电脑上工做,但仍是须要有一个中心的控制服务器。那这台git服务器就是做为咱们中心
#可是没有办法更多的去控制用户的权限,和很快捷很高效的管理用户名和密码或者租之类的东西
#应用场景在开发人员不多的状况下(好比一两个)或者项目代码量不是很大的。平时维护成本很容易,这时候能够选择这种方案去作。
在介绍它以前,先讲述一下命令行的git服务器
2.找一台服务器,首先要安装git,yum install git
3.添加git用户,而且设置shell为/usr/bin/git-shell,目的是为了避免让git用户远程登录
#也就是不须要他登陆咱们的系统
useradd -s /usr/bin/git-shell git
cd /home/git
4.建立authorized_keys文件,并更改属主、属组和权限,用来存客户端机器上的公钥
#也就是让另一台机器能和他通讯。经过ssh秘钥的形式
mkdir .ssh
touch .ssh/authorized_keys
chown -R git.git .ssh
chmod 600 .ssh/authorized_keys
5.定好存储git仓库的目录,好比 /data/gitroot
mkdir /data/gitroot
cd /data/gitroot
6.git init --bare sample.git
// 会建立一个裸仓库,裸仓库没有工做区,由于服务器上的Git仓库纯粹是为了共享,因此不让用户直接登陆到服务器上去改工做区,而且服务器上的Git仓库一般都以.git结尾
#不须要作git add或commit的操做
chown -R git.git sample.git
以上操做是在git服务器上作的,平时git服务器是不须要开发人员登陆修改代码的,它仅仅是充当着一个服务器的角色,就像github同样,平时操做都是在咱们本身的pc上作的
首先要把客户端上的公钥放到git服务器上/home/git/.ssh/authorized_keys文件里
在客户端上(本身pc)克隆远程仓库
git clone git@ip:/data/gitroot/sample.git
此时就能够在当前目录下生成一个sample的目录,这个就是咱们克隆的远程仓库了。进入到这里面,能够开发一些代码,而后push到远程。
实例:
在开一台虚拟机做为咱们的这台服务器
[root@axinlinux-02 ~]# yum install -y git #也要安装git
[root@axinlinux-02 ~]# useradd -s /usr/bin/git-shell git #建立git用户。不需药他远程登陆
[root@axinlinux-02 ~]# cd /home/git/ #进入家目录的git目录
[root@axinlinux-02 git]# ls
[root@axinlinux-02 git]# mkdir .ssh #建立.ssh目录
[root@axinlinux-02 git]# touch .ssh/authorized_keys #在.ssh目录下建立authorized_keys文件
[root@axinlinux-02 git]# chmod 600 .ssh/authorized_keys #赋予这个文件600的权限
[root@axinlinux-02 git]# chown -R git:git .ssh #.ssh目录下全部的属主属组为git
以上的目的是让另一台机器能和他通讯,经过ssh秘钥的形式
[root@axinlinux-01 ~]# cat .ssh/id_rsa.pub #把01机器上的公钥复制下来(此处为上一张在github添加秘钥认证时生成的公钥)
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDXXjoeyXRbpHSwxJa8kvnGev9HV7xqHfQxxVL711ypsMMaKGlpcRVGhRWNkfzS37W0jAfnVZJX3/nSD2BEcrmqmPAxRG48+OZMBhEqh6g6KeCe0JgOA0azm/gpujrrcRNLxbVsT6dTGiRzxfvAG2OPqwOCWN/a3QMPXpdd2IDVTSAw0GMDBYUpr+tHu1DzeVogt5wvdkcBAtb9+caDAAWM0uZLLlG/mZ+Zm2FqX7j8J9LPpy2LIeJF0OBbzeFHMHT1zdUkpLBL5FQSmQ2hrwweT3iqcBXB/A7MNQNan7SFAW4vj7LiUWuxA301RBHuY8e9sS74nLb9lJZds1yle5Hz root@axinlinux-01
[root@axinlinux-02 git]# vim .ssh/authorized_keys #复制的公钥放到这个刚建立的文件里来。其实这一步操做给在github上添加公钥是一个道理
[root@axinlinux-01 ~]# ssh git@192.168.208.130 #在客户端测试一下,看可否登陆服务端
fatal: Interactive git shell is not enabled. #出现这样的提示则没问题,验证成功。只不过不容许你直接登陆
hint: ~/git-shell-commands should exist and have read and execute access.
Connection to 192.168.208.130 closed.
[root@axinlinux-02 git]# cd /data/
[root@axinlinux-02 data]# mkdir gitroot
[root@axinlinux-02 data]# cd gitroot/
[root@axinlinux-02 gitroot]# ls
[root@axinlinux-02 gitroot]# git init --bare sample.git #初始化,产生sample的文件
初始化空的 Git 版本库于 /data/gitroot/sample.git/
[root@axinlinux-02 gitroot]# chown -R git:git sample.git #属主属组
[root@axinlinux-01 ~]# git clone git@192.168.208.130:/data/gitroot/sample.git #把这个项目克隆到本地
正克隆到 'sample'...
warning: 您彷佛克隆了一个空版本库。
[root@axinlinux-01 ~]# cd sample/
[root@axinlinux-01 sample]# cp /etc/init.d/mysqld ./ #作一个文件,推到远程
[root@axinlinux-01 sample]# git add mysqld
[root@axinlinux-01 sample]# git commit -m "add mysql"
[root@axinlinux-01 sample]# git push #推到远程,阿铭作的时候要origin master(指定分支),阿鑫作的时候没有提示。多是版本的问题
Counting objects: 3, done.
Delta compression using up to 2 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 3.84 KiB | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@192.168.208.130:/data/gitroot/sample.git
* [new branch] master -> master
[root@axinlinux-01 sample]# echo "asfsasafsafasf" > 22.txt #能够多推几个
[root@axinlinux-01 sample]# git add 22.txt
[root@axinlinux-01 sample]# git commit -m "add 22.txt"
[root@axinlinux-01 sample]# git push
caec0d6..f3ff75c master -> master
[root@axinlinux-01 sample]# cd /tmp/ #咱们cd到tmp下。将远程的这个项目在把它克隆到新的目录下(伪装另外一台服务器)
[root@axinlinux-01 tmp]# git clone git@192.168.208.130:/data/gitroot/sample.git #直接克隆服务器下的sample文件(项目)
正克隆到 'sample'...
remote: Counting objects: 6, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 6 (delta 0), reused 0 (delta 0)
接收对象中: 100% (6/6), 4.08 KiB | 0 bytes/s, done.
[root@axinlinux-01 tmp]# cd sample/
[root@axinlinux-01 sample]# ls #就有了
22.txt mysqld
[root@axinlinux-01 sample]# pwd #咱们也能够作pull(拉取的动做)
/tmp/sample
[root@axinlinux-01 sample]# vim 22.txt #从新变动一些文件,在提交,在push
[root@axinlinux-01 sample]# git add 22.txt
[root@axinlinux-01 sample]# git commit -m "again 22.txt"
[root@axinlinux-01 sample]# cd /root/sample/ #能够回到刚才的目录下
[root@axinlinux-01 sample]# git pull #直接拉取
[root@axinlinux-01 sample]# ls
22.txt mysqld
[root@axinlinux-01 sample]# cat 22.txt #能够看到咱们刚才对22.txt作的变动
asfsasafsafasf
243141341231231
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22.14/22.15 安装gitlab
1.以上咱们使用了自建服务器。本节咱们将gitlab这种解决方案(算是比较完美的)
咱们可使用在线的托管的管理平台。好比给github,可是github在美国有点慢。
也可使用国内的coding.net,还有一个付费的码市,还有一个码云
咱们能够把咱们的代码放到他们那里,比较省心不用维护
2.除了以上这种方案,还能够自检一个lab界面浏览管理控制的代码管理平台。首先方案就是gitlab软件。咱们能够去下载这个软件,在咱们本身的服务器上搭建这样的平台出来
3.这台跑gitlab的服务器尽可能不要放其余的东西。使用官方的一些备份工具,能备份所有的数据
1.gitlab官网 https://about.gitlab.com/gitlab-com/
2.官方安装文档 https://about.gitlab.com/installation/?version=ce#centos-7 (ce/ee)
#详见实例
3.要求服务器内存很多于2g
4.vim /etc/yum.repos.d/gitlab.repo//加入以下内容 (官方提供的源比较慢,咱们使用另外一个镜像)
!!!这种方式执行起来并不成功(gpgkey验证起来不成功)。只能按照官方文档去安装,有几部不须要体现,详细见实例!!!
[gitlab-ce]
name=gitlab-ce
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
Repo_gpgcheck=0
Enabled=1
Gpgkey=https://packages.gitlab.com/gpg.key
5.yum install -y gitlab-ce(ce为社区版本免费)
6.gitlab-ctl reconfigure
#gitlab的核心命令
7.netstat -lnpt //查看监听端口
8.gitlab-ctl stop/restart/start/status
#gitlab-ctl 这个命令能够中止/重启/开启/状态
9.浏览器访问gitlab,输入ip便可
默认管理员root,无密码,它会让咱们去定义一个密码
实例:
按照官方文档那个来安装的
[root@axinlinux-01 ~]# yum install -y curl policycoreutils-python openssh-server openssh-clients
[root@axinlinux-01 ~]# curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash #生成yum的仓库
[root@axinlinux-01 ~]# EXTERNAL_URL="http://gitlab.example.com" yum install -y gitlab-ce #yum安装ce版本(社区版本)
[root@axinlinux-01 ~]# gitlab-ctl reconfigure
Running handlers:
Running handlers complete
Chef Client finished, 454/648 resources updated in 04 minutes 59 seconds
gitlab Reconfigured! #最后会告诉你讲gitla先关的服务给你启动起来
[root@axinlinux-01 ~]# ps aux |grep gitlab #能够看到有不少gitlab相关的服务。能够说这个系统很复杂
他会内置一个nginx。若是机器上有安装过nginx,在安装gitlab以前先把nginx关掉,以避免冲突
[root@axinlinux-01 ~]# gitlab-ctl stop
[root@axinlinux-01 ~]# killall redis-server #redis若是安装了,也要关掉
redis-server: no process found
[root@axinlinux-01 ~]# gitlab-ctl start
ok: run: alertmanager: (pid 6407) 0s
ok: run: gitaly: (pid 6418) 0s
ok: run: gitlab-monitor: (pid 6428) 0s
ok: run: gitlab-workhorse: (pid 6432) 1s
ok: run: logrotate: (pid 6452) 0s
ok: run: nginx: (pid 6462) 1s
ok: run: node-exporter: (pid 6470) 0s
ok: run: postgres-exporter: (pid 6475) 1s
ok: run: postgresql: (pid 6481) 0s #他用的是这个数据库。
ok: run: prometheus: (pid 6483) 0s
ok: run: redis: (pid 6499) 0s #他也会内置一个redis。因此也要将以前的redis关掉
ok: run: redis-exporter: (pid 6503) 0s
ok: run: sidekiq: (pid 6509) 1s
ok: run: unicorn: (pid 6516) 0s
而后登录便可,默认为root用户
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22.16 使用gitlab
nginx相关:
由于在内网里,直接输入内网ip就能够了。
若是想用公网去访问他,那么在公网服务器上用nginx作个代理就能够了,也能够设置域名去访问他
1./var/opt/gitlab/nginx/conf/nginx.conf #主配置文件
2./var/opt/gitlab/nginx/conf/gitlab-http.conf #gitlab相关的配置文件。修改端口或绑定域名能够在这里更改
server {
listen *:80; #更改端口
server_name gitlab.example.com; #更改域名
若是这台服务器不须要跑别的服务,仅仅一个gitlab。那就不用动他
进入gitlab:
gitlab经常使用命令 https://www.cnyunwei.cc/archives/1204
实例:
建一个新的项目
咱们在建立新的项目以前先在建立一个新的组
接下来,咱们在组里面建立用户>project
接下来添加ssh key
接下来咱们在建立一个用户
新的密码为 wx123456789
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22.17 gitlab备份和恢复
1.gitlab备份 gitlab-rake gitlab:backup:create
#若是数据量很大的话,确定会耗费不短的时间。在线备份,服务要是开启的状态
#会备份你建立的project(项目)、group、user、固然还有project里的代码
2.备份目录在/var/opt/gitlab/backups
#1543236441_2018_11_26_11.5.0_gitlab_backup.tar,#11.5.0为版本号。
#假如你用的是9.几的gitlab作的备份,想把9.几的tar文件恢复到10.几版本的gitlab里边去,那么就会遇到问题:
要么装一个9.几的版本。
要么就把老的版本升级,而后再作本分。完成以后,在把它搞到10.几版本的gitlab上来。也就是只要是版本一致就能恢复过来
3.gitlab 恢复 先停服务 gitlab-ctl stop unicorn ; gitlab-ctl stop sidekiq
#停掉这两个数据的目的是暂时不要变动数据了
gitlab-rake gitlab:backup:restore BACKUP=xxxxx (这里是一个编号,即备份文件的前缀)
#1543236441_2018_11_26_11.5.0_gitlab_backup.tar。红色部分就是BACKUP=后面的编号
再启动服务 gitlab-ctl start