Git服务选型
- 目前市面上比较有名的Git服务提供商,国外有GitHub、BitBucket、GitLab,国内有Coding。
- 现有的服务商,对于免费的套餐都有一定的限制。比如:GitHub只允许建立免费的开源repository,建立私有的仓库需要收费。BitBucket允许建立无限制的私有项目,不过对于项目中参与的开发人员是有人数限制的,当团队中开发者规模达到一定数量后,需要付费购买相应的套餐。
GitLab社区版是免费的,不但能建立免费的私有仓库而且没有数量上限,参与人员也没有数量限制,还能设置成员的权限,甚至细致到具体某条分支的权限,以及强大的工作流等等。
GitLab很适合中小型非开源项目公司。
一、GitLab 简介
GitLab 是一个利用Ruby on Rails 开发的开源版本控制系统,实现一个自托管的Git项目仓库,可通过Web界面进行访问公开的或者私人项目。
它拥有与GitHub类似的功能,能够浏览源代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本并提供一个文件历史库。团队成员可以利用内置的简单聊天程序(Wall)进行交流。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后有需要的时候进行查找。
开源中国代码托管平台 码云 就是基于GitLab项目搭建。
GitLab官网访问需要科学上网
GitLab 分为 GitLab Community Edition(CE) 社区版 和 GitLab Enterprise Edition(EE) 专业版。社区版免费,专业版收费,两个版本在功能上的差异对比,可以参考官方对比说明
二、GitLab 安装和配置
安装社区版,GitLab CE 版本:12.7.3
1、GitLab安装
通过GitLab官方提供的Omnibus安装包来安装(omnibus方式即是通过gitlab提供的一个打包好的文件),相对方便。Omnibus安装包套件整合了大部分的套件(Nginx、ruby on rails、git、redis、postgresql等),再不用额外安装这些软件,减轻了绝大部分安装量。
GitLab官方安装文档 :CentOS7.x系统
GitLab配置文档首页: Omnibus模式
安装配置依赖环境
sudo yum install -y curl policycoreutils-python openssh-server
sudo systemctl enable sshd
sudo systemctl start sshd
sudo firewall-cmd --permanent --add-service=http
sudo firewall-cmd --permanent --add-service=https
sudo systemctl reload firewalld
如果使用的是 iptables 则使用以下方法 打开HTTP和SSH端口:
# iptables -I INPUT -m tcp -p tcp --dport 22 -j ACCEPT
# iptables -I INPUT -m tcp -p tcp --dport 80 -j ACCEPT
配置postfix服务为GitLab邮件服务
sudo yum install postfix
sudo systemctl enable postfix
sudo systemctl start postfix
两种安装源
- 从官方镜像源安装
添加GitLab仓库并安装到服务器上
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ee/script.rpm.sh | sudo bash
# Next, install the GitLab package. Change https://gitlab.example.com to the URL at which you want to access your GitLab instance. Installation will automatically configure and start GitLab at that URL.
sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ee
# 指定访问域名,作为nginx的配置文件,或者修改gitlab配置文件
# yum install gitlab-ce-10.5.8-ce.0.el6 # 安装指定版本
# 或者rpm安装 rpm -ivh gitlab-ce-10.5.8-ce.0.el7.x86_64.rpm
- 从第三方镜像源安装
官方镜像源在国外,国内安装会很慢,甚至有时因网络问题会无法安装。
国内推荐使用 清华大学开源软件镜像源。
gitlab centos7 资源地址
新建 /etc/yum.repos.d/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 makecache # 更新本地YUM缓存
sudo EXTERNAL_URL="https://gitlab.example.com" yum install -y gitlab-ee # 指定域名安装
sudo yum install gitlab-ce # 安装最新版本,需手动指定域名
配置文件 /etc/gitlab/gitlab.rb,绑定域名
external_url 'http://gitlab.xxx.com'
启动GitLab,使得配置生效
gitlab-ctl reconfigure # gitlab的客户端工具
添加解析记录
使用阿里云解析 使用dnspod解析
使用浏览器访问GitLab
首次访问GitLab,系统会让你重新设置管理员的密码,设置成功后会返回登录界面. 默认的管理员账号是root,如果你想更改默认管理员账号,请输入上面设置的新密码登录系统后修改帐号名.
GitLab安装细节
主配置文件: /etc/gitlab/gitlab.rb
GitLab 文档根目录: /opt/gitlab
默认存储库位置: /var/opt/gitlab/git-data/repositories
GitLab Nginx 配置文件路径: /var/opt/gitlab/nginx/conf/gitlab-http.conf
Postgresql 数据目录: /var/opt/gitlab/postgresql/data
GitLab由以下服务构成
omnibus模式默认安装服务
- NGINX: 静态web服务器,Routes requests to appropriate components, terminates SSL
- Unicorn (GitLab Rails): Handles requests for the web interface and API
- Sidekiq: 用于在后台执行队列任务(异步执行),Background jobs processor
- Gitaly: Git RPC service for handling all Git calls made by GitLab
- Praefect: A transparent proxy between any Git client and Gitaly storage nodes.
- GitLab Workhorse: 轻量级的反向代理服务器, Smart reverse proxy, handles large HTTP requests
- GitLab Shell: 用于处理Git命令和修改authorized keys列表, Handles git over SSH sessions
- Redis: 缓存数据库, Caching service
- PostgreSQL: 核心数据库, Database
- GitLab self-monitoring: Prometheus: 自带时序数据库监控, Time-series database, metrics collection, and query service
- GitLab self-monitoring: 自带监控图表, Grafana: Metrics dashboard
- Redis Exporter: Prometheus endpoint with Redis metrics
- Postgres Exporter: Prometheus endpoint with PostgreSQL metrics
- GitLab Exporter: Generates a variety of GitLab metrics
- Node Exporter: Prometheus endpoint with system metrics
- Database Migrations: Database migrations
- Certificate Management: TLS Settings, Let’s Encrypt
另外一个重要的核心服务(管理)
- Logrotate: 日志文件管理工具。
2、配置SMTP服务
GitLab邮件服务配置(gitlab.rb文件),使用腾讯企业邮箱的SMTP服务器,填写账号和密码
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.server"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "smtp user"
gitlab_rails['smtp_password'] = "smtp password"
gitlab_rails['smtp_domain'] = "example.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
使配置生效
# gitlab-ctl reconfigure
# gitlab-rake cache:clear RAILS_ENV=production # 清除缓存
3、GitLab配置HTTPS
GitLab默认是使用HTTP的,可以手动配置为HTTPS
服务器修改过ssh端口的坑(需要修改配置ssh端口)
#修改过ssh端口,gitlab中项目的的ssh地址,会在前面加上协议头和端口号“ssh://git@gitlab.domain.com:55000/username/test.git”
gitlab_rails['gitlab_shell_ssh_port'] = 55000
上传SSL证书
创建ssl目录,用于存放SSL证书
# mkdir -p /etc/gitlab/ssl
# chmod 0700 /etc/gitlab/ssl
上传证书并修改证书权限
# chmod 600 /etc/gitlab/ssl/*
修改GitLab的配置文件
修改配置文件/etc/gitlab/gitlab.rb
external_url "https://gitlab.xxx.com"
nginx['redirect_http_to_https'] = true
nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.xxx.com.crt"
nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.xxx.com.key"
重建配置,使其生效
# gitlab-ctl reconfigure
以上操作后,GitLab自带的Nginx服务的配置文件 /var/opt/gitlab/nginx/conf/gitlab-http.conf 会被重新修改:
server {
listen *:80;
server_name gitlab.xxx.com;
server_tokens off; ## Don't show the nginx version number, a security best practice
return 301 https://gitlab.xxx.com:443$request_uri;
access_log /var/log/gitlab/nginx/gitlab_access.log gitlab_access;
error_log /var/log/gitlab/nginx/gitlab_error.log;
}
不用额外再配置,HTTP 会自动跳转到 HTTPS 。
开放443端口
在防火墙上开放443端口,用于HTTPS
# centos7 已经开启https不需要重复操作
# iptables -I INPUT -m tcp -p tcp --dport 443 -j ACCEPT
4、LDAP配置
启用Ldap配置
gitlab_rails['ldap_enabled'] = true
gitlab_rails['prevent_ldap_sign_in'] = false
#! **remember to close this block with 'EOS' below**
gitlab_rails['ldap_servers'] = YAML.load <<-'EOS'
main: # 'main' is the GitLab 'provider ID' of this LDAP server
label: 'LDAP'
host: 'adfs.company.cn'
port: 389
uid: 'mail'
bind_dn: 'cn=ldapgitlab,cn=Users,dc=company,dc=cn'
password: 'password'
encryption: 'plain' # "start_tls" or "simple_tls" or "plain"
verify_certificates: true
smartcard_auth: false
active_directory: true
allow_username_or_email_login: false
lowercase_usernames: false
block_auto_created_users: false
base: 'ou=department,ou=ouname,DC=company,DC=cn'
user_filter: ''
## EE only
group_base: ''
admin_group: ''
sync_ssh_keys: false
EOS
使用uid为邮箱登录方式(不使用用户名),过滤方式默认,指定base
5、修改root用户密码
对于普通用户而言,可通过系统重置密码,接收邮件即可。可是GitLab管理员账号,缺省邮箱 admin@example.com 是个不存在的邮箱地址,无法通过邮箱修改密码。
官方修改密码文档,根据文档,修改root密码的方法如下:
打开与Rails程序交互的控制台
在root权限下,执行:
gitlab-rails console -e production
等待一会,直到控制台加载成功。
获取用户信息并修改root用户密码
gitlab-12交互方式(使用ruby语言),001表示行号,类似python的 IPython 方式
[root@gitlab-test ~]# gitlab-rails console -e production
--------------------------------------------------------------------------------
GitLab: 12.6.2 (3041661dec5)
GitLab Shell: 10.3.0
PostgreSQL: 10.9
--------------------------------------------------------------------------------
Loading production environment (Rails 5.2.3)
irb(main):001:0> user = User.where(id: 1).first
=> #<User id:1 @root>
irb(main):002:0> user.password = '12345678'
=> "12345678"
irb(main):003:0> user.password_confirmation = '12345678'
=> "12345678"
irb(main):004:0> user.save!
Enqueued ActionMailer::DeliveryJob (Job ID: 24d59aa0-18c0-4322-9d56-45e8d6d0740a) to Sidekiq(mailers) with arguments: "DeviseMailer", "password_change", "deliver_now", #<GlobalID:0x00007efd42cdef88 @uri=#<URI::GID gid://gitlab/User/1>>
=> true
irb(main):005:0> exit
这里过滤user可以使用id,或者使用邮箱方式:
user = User.find_by(email: 'admin@example.com')
三、GitLab 常用命令
部分命令是老版本,但依然兼容12版本
运维管理排查
# 查看版本
cat /opt/gitlab/embedded/service/gitlab-rails/VERSION
# 检查gitlab
gitlab-rake gitlab:check SANITIZE=true --trace
# 实时查看日志
gitlab-ctl tail
# 数据库关系升级
gitlab-rake db:migrate
# 清理redis缓存
gitlab-rake cache:clear
# 升级GitLab-ce 版本
yum update gitlab-ce
# 升级PostgreSQL最新版本
gitlab-ctl pg-upgrade
服务管理
# 启动所有 gitlab 组件:
gitlab-ctl start
# 停止所有 gitlab 组件:
gitlab-ctl stop
# 停止所有 gitlab postgresql 组件:
gitlab-ctl stop postgresql
# 停止相关数据连接服务
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
# 重启所有 gitlab 组件:
gitlab-ctl restart
# 重启所有 gitlab gitlab-workhorse 组件:
gitlab-ctl restart gitlab-workhorse
# 查看服务状态
gitlab-ctl status
# 生成配置并启动服务
gitlab-ctl reconfigure
日志
# 实时查看所有日志
gitlab-ctl tail
# 实时检查redis的日志
gitlab-ctl tail redis
# 实时检查postgresql的日志
gitlab-ctl tail postgresql
# 检查gitlab-workhorse的日志
gitlab-ctl tail gitlab-workhorse
# 检查logrotate的日志
gitlab-ctl tail logrotate
# 检查nginx的日志
gitlab-ctl tail nginx
# 检查sidekiq的日志
gitlab-ctl tail sidekiq
# 检查unicorn的日志
gitlab-ctl tail unicorn
四、GitLab备份和恢复
备份
GitLab作为公司项目代码的版本管理系统,数据非常重要,必须做好备份。
修改备份目录
GitLab备份的默认目录是 /var/opt/gitlab/backups ,如果想改备份目录,可修改/etc/gitlab/gitlab.rb:
gitlab_rails['manage_backup_path'] = true
gitlab_rails['backup_path'] = "/var/opt/gitlab/backups" # 修改备份path
gitlab_rails['backup_archive_permissions'] = 0644 # 配置备份权限
# 甚至可以被封到亚马逊,或者文件存储
修改配置后,记得:
gitlab-ctl reconfigure
备份命令
sudo gitlab-backup create
# gitlab-rake gitlab:backup:create # For GitLab 12.1 and earlier, use gitlab-rake gitlab:backup:create.
该命令会在备份目录(默认:/var/opt/gitlab/backups/)下创建一个tar压缩包xxxxxxxx_gitlab_backup.tar,其中开头的xxxxxx是备份创建的时间戳,这个压缩包包括GitLab整个的完整部分。
自动备份
通过任务计划crontab 实现全量自动备份
# Create a full backup of the GitLab repositories and SQL database every day at 4am
0 4 * * * cd /home/git/gitlab && PATH=/usr/local/bin:/usr/bin:/bin bundle exec rake gitlab:backup:create RAILS_ENV=production CRON=1
我开始认为有好的自动备份方式,crontab 和 配置文件中找了半天。。。
结果 上一任 将自动备份放在/etc/crontab,哎。
gitlab备份时间需要一个小时(跟代码量也有关吧,一个备份大小约5G),定时是2点执行,一般到3点才完成。
备份保留7天
可设置只保留最近7天的备份,编辑配置文件 /etc/gitlab/gitlab.rb
# 数值单位:秒
gitlab_rails['backup_keep_time'] = 604800
重新加载gitlab配置文件
gitlab-ctl reconfigure
恢复
备份文件:
/var/opt/gitlab/backups/1499244722_2017_07_05_9.2.6_gitlab_backup.tar
停止 unicorn 和 sidekiq ,保证数据库没有新的连接,不会有写数据情况。
# 停止相关数据连接服务
sudo gitlab-ctl stop unicorn
sudo gitlab-ctl stop puma
sudo gitlab-ctl stop sidekiq
# Verify 检查
sudo gitlab-ctl status
# 指定恢复文件,会自动去备份目录找。确保备份目录中有这个文件。
# 指定文件名的格式类似:1499242399_2017_07_05_9.2.6,程序会自动在文件名后补上:“_gitlab_backup.tar”
# 一定按这样的格式指定,否则会出现 The backup file does not exist! 的错误
sudo gitlab-backup restore BACKUP=1493107454_2018_04_25_10.6.4-ce
# gitlab-rake gitlab:backup:restore BACKUP=1499242399_2017_07_05_9.2.6 12.1版本及之前版本操作
# 启动Gitlab
gitlab-ctl start
五、版本升级
gitlab升级可以直接使用yum升级,但不能跨大版本
根据官方推荐升级到指定的版本
| Latest stable version | Your version | Recommended upgrade path | Note |
|---|---|---|---|
| 9.4.5 | 8.13.4 | 8.13.4 -> 8.17.7 -> 9.4.5 |
8.17.7 is the last version in version 8 |
| 10.1.4 | 8.13.4 | 8.13.4 -> 8.17.7 -> 9.5.10 -> 10.1.4 |
8.17.7 is the last version in version 8, 9.5.10 is the last version in version 9 |
| 11.3.4 | 8.13.4 | 8.13.4 -> 8.17.7 -> 9.5.10 -> 10.8.7 -> 11.3.4 |
8.17.7 is the last version in version 8, 9.5.10 is the last version in version 9, 10.8.7 is the last version in version 10 |
| 12.5.8 | 11.3.4 | 11.3.4 -> 11.11.8 -> 12.0.9 -> 12.5.8 |
11.11.8 is the last version in version 11 |
版本10.5.8升级: 先升级到10版本最后一个版本,然后在升级到11版本
wget https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7/gitlab-ce-11.3.4-ce.0.el7.x86_64.rpm
gitlab-ctl status
gitlab-ctl stop unicorn
gitlab-ctl stop sidekiq
gitlab-ctl stop nginx
gitlab-ctl status
rpm -Uvh gitlab-ce-11.3.4-ce.0.el7.x86_64.rpm
gitlab-ctl restart