阿里云 centOS 7 下搭建gitlab
和同事讨论以及看过网上的对比后,决定选用gitlab作为我们的版本管理器
git与svn 对比文件详见https://www.cnblogs.com/dazhidacheng/p/7478438.html
gitlab 官方文档https://docs.gitlab.com/ce/install/README.html
gitlab 汉化指南https://gitlab.com/xhang/gitlab/wikis/home
部署以及汉化参考教程 https://www.cnblogs.com/supery007/p/9053774.html
官方gitlab cent0S7 安装教程https://about.gitlab.com/install/#centos-7
阿里官方 cent0S7 安装gitlab教程https://blog.****.net/zhaoyanjun6/article/details/79144175
centos出现“FirewallD is not running”怎么办 https://www.cnblogs.com/kccdzz/p/8110143.html
gitlab搭建提示inet_interfacesJob for postfix.service failed because the control process exited with erro
https://blog.****.net/u013697959/article/details/78041465
gitlab自动备份参考 https://blog.****.net/utopiaprince/article/details/50039989
1.安装gitlab
查看服务器系统版本
$ lsb_release -a
在官网找到对应版本的安装教程
https://about.gitlab.com/install/#centos-7
添加清华大学镜像源(ps:内容不能有空格)
新建 /etc/yum.repos.d/gitlab_gitlab-ce.repo,内容为:
[gitlab-ce]
name=Gitlab CE Repository
baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/
gpgcheck=0
enabled=1
安装依赖
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
警报:防火墙没有打开
参考centos出现“FirewallD is not running”怎么办 https://www.cnblogs.com/kccdzz/p/8110143.html
启动防火墙
systemctl start firewalld
查看防火墙状态 启动成功
systemctl status firewalld
再次执行 成功
sudo firewall-cmd --permanent --add-service=http
sudo systemctl reload firewalld
安装Postfix以发送通知电子邮件
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
参考gitlab搭建提示inet_interfacesJob for postfix.service failed because the control process exited with error
https://blog.****.net/u013697959/article/details/78041465
(PS:注意ipv4 是小写)
安装依赖包。
sudo yum install -y curl policycoreutils-python openssh-server
设置SSH开机自启动并启动SSH服务。
sudo systemctl enable sshd
sudo systemctl start sshd
安装Postfix来发送通知邮件。
sudo yum install postfix
设置Postfix开机自启动。
sudo systemctl enable postfix
启动Postfix服务。
-
输入命令vim /etc/postfix/main.cf打开main.cf文件并找到下图内容:
-
将这行代码改为inet_interfaces = all,然后按Esc键,然后输入:wq并回车以保存并关闭main.cf文件。
-
输入命令sudo systemctl start postfix启动Postfix服务。
添加GitLab软件包仓库。
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
安装GitLab。
sudo EXTERNAL_URL="GitLab服务器的公网IP地址" yum install -y gitlab-ce
sudo EXTERNAL_URL="120.78.xxx.xxx" yum install -y gitlab-ce
去阿里云安全管制添加8089的端口 //这个超级总要,很可能搞半天就是因为这个端口管制问题导致了外网访问不了
。。。。。
打开阿里云服务器防火墙的 8089 端口
systemctl start firewalld
firewall-cmd --zone=public --add-port=8089/tcp --permanent
systemctl restart firewalld
修改 Gitlab 默认端口配置
打开 /etc/gitlab/gitlab.rb 文件,找到 external_url 字段,如下图所示:
把 http://47.94.230.26 改为 http://47.94.230.26:8089
然后执行 gitlab-ctl reconfigure 让配置立即生效。
在浏览器中访问 http://47.94.230.26:8089 , 就可以看到 Gitlab 的页面了。
GitLab备份和恢复
备份
# 可以将此命令写入crontab,以实现定时备份
/usr/bin/gitlab-rake gitlab:backup:create
备份的数据会存储在/var/opt/gitlab/backups,用户通过自定义参数 gitlab_rails['backup_path'],改变默认值。
gitlab自动备份参考 https://blog.****.net/utopiaprince/article/details/50039989
gitlab自动备份
crontab
在ubuntu版本下,crontab工具是默认安装的,配置文件在/etc/crontab里。
编写自动备份脚本
touch auto_backup.sh
sudo chmod +x auto_backup.sh
sudo vim auto_backup.sh
#!/bin/bash
BACKUP_PATH1="/var/opt/gitlab/backups"
DAY=2
gitlab-rake gitlab:backup:create;
find $BACKUP_PATH1 -name "*.tar" -ctime +$DAY -type f -exec rm -rf {} \;
定时执行脚本
vi /etc/crontab
0 20 * * * root /var/opt/gitlab/backups/auto_backup.sh -D 1 //每天晚上八点执行脚本一次
恢复
# 停止unicorn和sidekiq,保证数据库没有新的连接,不会有写数据情况
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop sidekiq
# 进入备份目录进行恢复,1476900742为备份文件的时间戳
cd /var/opt/gitlab/backups
gitlab-rake gitlab:backup:restore BACKUP=1476900742
cd -
# 启动unicorn和sidekiq
sudo gitlab-ctl start unicorn
sudo gitlab-ctl start sidekiq
新建项目
使用root用户登录进gitlab会后,点击“new project“创建一个项目,比如项目命名为“kuaijiFirstProject”。
然后会发现,硬盘上已经生成了一个git文件:
/var/opt/gitlab/git-data/repositories/root/kuaijiFirstProject.git
汉化
https://gitlab.com/xhang/gitlab.git
三、安装汉化包
3.1 准备环境
wget https://gitlab.com/xhang/gitlab/-/archive/11-11-stable-zh/gitlab-11-11-stable-zh.tar.gz
tar zxvf gitlab-11-11-stable-zh.tar.gz
3.2 停止gitlab
[[email protected] ~]# gitlab-ctl stop
3.3 汉化操作
# 查看改汉化补丁版本
[[email protected] src]# cat gitlab-11-11-stable-zh/VERSION
11.11.0
# 查看系统的版本
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
11.11.0
# 备份版本
cd /opt/gitlab/embedded/service/gitlab-rails/
tar zcvf gitlab-rails.bak.tar.gz gitlab-rails/
3.5 替换到gitlab中
cd gitlab-11-11-stable-zh
/bin/cp -rf * /opt/gitlab/embedded/service/gitlab-rails/
3.6 重新配置gitlab
[[email protected] tmp]# gitlab-ctl reconfigure
[[email protected] tmp]# gitlab-ctl start
gitlab的使用
ssh -T -p 55725 [email protected]
注意:<fornt style="color:red">以上这条 ssh 命令测试通过,未必代表就能 git clone 代码,git clone 代码需要执行命令的账户有写权限,如果是普通用户用 sudo git clone 那么 git 就会使用的 root 账号的 Private Key。 </fornt>
1.登录
管理员会为使用者开通账号并设置权限。
2.使用者在客户端生成ssh key
参考文章: http://www.jianshu.com/p/142b3dc8ae15
ssh-****** -t rsa -C "[email protected]"
3.将公钥的内容copy到gitlab用户设置里面的“SSH Keys”
Windows: clip < ~/.ssh/id_rsa.pub
Mac: pbcopy < ~/.ssh/id_rsa.pub
GNU/Linux (requires xclip): xclip -sel clip < ~/.ssh/id_rsa.pub
4.测试ssh连接
#标准测试代码:ssh -T [email protected]
ssh -T -p 55725 [email protected] #修改过端口号的测试代码
如果连接成功的话,会出现以下信息:
Welcome to GitLab, huangdc!
说明:实际上执行这条ssh命令,所使用的远程服务器的用户是git,这个用户是在安装gitlab的时候生成的,所有使用gitlab服务器的ssh客户端,都是使用git这个用户。在这里的用户“huangdc”是通过gitlab创建的,是用于gitlab的权限管理,也用作标识提交代码的开发者信息,不要跟ssh的用户混淆了。
如何使用多个SSH公钥(自己电脑在使用多个代码仓库)
原理其实是:因为每个仓库都需要 ssh 连接,而 ssh 命令默认是使用 .ssh 目录下面的私钥去连接代码仓库,所以我们可以在 .ssh/config 目录里面针对不同的仓库域名重定义它的私钥。
例子如下:
编辑文件: vim /Users/david/.ssh/config
Host gitlab.domain.com
IdentityFile /Users/david/.ssh/id_rsa
命令行环境下初始化项目
-
首先在 gitlab 上面创建一个空的代码仓库,得到仓库地址如下:
ssh://[email protected]:55725/huangdc/test.git
-
在本地初始化仓库、提交代码、推送到远程 master 分支。
git clone ssh://[email protected]:55725/huangdc/test.git
cd test
touch README.md
git add README.md
git commit -m "add README"
git push -u origin master
命令行环境下迁移旧的项目
-
首先在 gitlab 上面创建一个空的代码仓库,得到仓库地址如下:
#注意:已存在代码的旧项目只能推送到空的远程代码仓库
ssh://[email protected]:55725/dev/memberApi.git
-
本地初始化项目、关联远程仓库、推送到远程仓库
cd /Users/david/work_www/memberApi
git init
git remote add origin ssh://[email protected]:55725/dev/memberApi.git
git push -u origin master
SourceTree的安装和打开
-
打开SourceTree之后,需要登录Atlassian账号来**SourceTree。可以使用Google账号直接关联登录。
-
登录后还需要一些设置,以最简单的方式跳过就行。