V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
wsgzao
V2EX  ›  程序员

RHEL7/CentOS7 在线和离线安装 GitLab 配置使用实践

  •  
  •   wsgzao ·
    wsgzao · 2018-04-10 19:21:31 +08:00 · 3595 次点击
    这是一个创建于 2179 天前的主题,其中的信息可能已经有所发展或是发生改变。

    前言

    GitLab Community Edition 新版本已经集成了 CI/CD 的支持,从某种程度上来说可以告别对于 Jenkins 的依赖,我本来计划是写基于 Docker 部署 GitLab 但感觉还是有点重,对于内网离线环境来说基于容器部署也未必是合理的方案,这里沿用传统的部署方式介绍简单且长期有效的 GitLab 部署方案供大家参考,或许未来会增加基于容器的部署方案和 CI/CD 的分享,因需而变。

    RHEL7/CentOS7 在线和离线安装 GitLab 配置实践,GitLab 汉化配置使用小结

    更新记录

    2018 年 04 月 10 日 - 初稿

    阅读原文 - https://wsgzao.github.io/post/gitlab/

    扩展阅读

    GitLab Installation - https://about.gitlab.com/installation

    GitLab 简介

    GitLab 是利用 Ruby On Rails 开发的一个开源版本管理系统,实现了一个自托管的 Git 项目仓库,是集代码托管,测试,部署于一体的开源 git 仓库管理软件,可通过 web 界面来进行访问公开的或私人项目。与 Github 类似,GitLab 能够浏览代码,管理缺陷和注释。可以管理团队对仓库的访问,它非常易于浏览提交过的版本,并提供一个文件历史库。它还提供一个代码片段收集功能可以轻松实现代码复用,便于日后需要的时候查找。

    Git 的家族成员

    • Git:是一种版本控制系统,是一个命令,是一种工具。
    • Gitlib:是用于实现 Git 功能的开发库。
    • Github:是一个基于 Git 实现的在线代码托管仓库,公开项目是免费的,也可以付费创建私人项目。
    • GitLab:是一个基于 Git 实现的在线代码仓库托管软件,可以用 GitLab 搭建一套类似 Github 的系统。

    GitLab 对硬件还是有一定要求的,1 核心的 CPU 基本上可以满足需求,大概支撑 100 个左右的用户,不过在运行 GitLab 网站的同时还需要运行多个后台 job,就会显得有点捉襟见肘了。需要至少 4GB 的可寻址内存( RAM 交换)来安装和使用 GitLab,操作系统和任何其他正在运行的应用程序也将使用内存,因此请记住,在运行 GitLab 之前,您至少需要 4GB 的可用空间。如果使用更少的内存,GitLab 将在重新配置运行期间给出奇怪的错误,我用虚拟机来分别新建 1G,2G 内存的 CentOS 系统来装 GitLab,确实非常捉襟见肘啊,伤不起。

    Gitlab 的服务构成

    • Nginx:静态 web 服务器
    • gitlab-shell:用于处理 Git 命令和修改 authorized keys 列表
    • gitlab-workhorse: 轻量级的反向代理服务器
    • logrotate:日志文件管理工具
    • postgresql:数据库
    • redis:缓存数据库
    • sidekiq:用于在后台执行队列任务(异步执行)
    • unicorn:An HTTP server for Rack applications,GitLab Rails 应用是托管在这个服务器上面的

    GitLab 工作流程

    GitLab 安装

    在线安装 gitlab-ce

    # 安装必要的依赖包,如果不需要可以考虑跳过
    yum install pygpgme yum-utils
    # On CentOS 7 (and RedHat/Oracle/Scientific Linux 7), the commands below will also open HTTP and SSH access in the system firewall.
    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 systemctl reload firewalld
    # Next, install Postfix to send notification emails. If you want to use another solution to send emails please skip this step and configure an external SMTP server after GitLab has been installed.
    sudo yum install postfix
    sudo systemctl enable postfix
    sudo systemctl start postfix
    
    # 使用阿里云作加速
    cd /etc/yum.repos.d/ && rm -f *.repo
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
    # 添加 GitLab 镜像源并安装,建议切换国内资源加速访问
    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
    # 创建 gitlab-ce 的 repo,使用清华大学加速
    vim /etc/yum.repos.d/gitlab_gitlab-ce.repo
    
    [gitlab-ce]
    name=gitlab-ce
    baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
    repo_gpgcheck=0
    gpgcheck=0
    enabled=1
    gpgkey=https://packages.gitlab.com/gpg.key
    
    #配置并启动 GitLab
    gitlab-ctl reconfigure
    
    # 第一次访问 GitLab,系统会重定向页面到重定向到重置密码页面,你需要输入初始化管理员账号的密码,管理员的用户名为 root,重置密码后新密码即为刚输入的密码。
    0.0.0.0:80
    
    

    离线安装 gitlab-ce

    # 使用阿里云作加速
    cd /etc/yum.repos.d/ && rm -f *.repo
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
    wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    
    # 添加 GitLab 镜像源并安装,建议切换国内资源加速访问
    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
    
    # 创建 gitlab-ce 的 repo,使用清华大学加速
    vim /etc/yum.repos.d/gitlab_gitlab-ce.repo
    
    [gitlab-ce]
    name=gitlab-ce
    baseurl=http://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el7
    repo_gpgcheck=0
    gpgcheck=0
    enabled=1
    gpgkey=https://packages.gitlab.com/gpg.key
    
    #安装 yum-plugin-downloadonly 插件
    yum install -y yum-plugin-downloadonly
    
    # 下载 gitlab-ce 相关 rpm 到指定目录
    mkdir -p /tmp/repo/gitlab-ce/
    yum install --downloadonly --downloaddir=/tmp/repo/gitlab-ce/ gitlab-ce
    
    # 拷贝文件至内网离线安装
    rpm -ivh /tmp/repo/gitlab-ce/*
    
    #配置并启动 GitLab
    gitlab-ctl reconfigure
    
    # 第一次访问 GitLab,系统会重定向页面到重定向到重置密码页面,你需要输入初始化管理员账号的密码,管理员的用户名为 root,重置密码后新密码即为刚输入的密码。
    0.0.0.0:80
    
    

    GitLab 汉化

    如果团队里英文水平都不错的话,是没必要汉化的,我个人的建议是坚持使用原版不做汉化

    GitLab 中文社区的项目,v7-v8.8 是由 Larry Li 发起的“ GitLab 中文社区版项目” https://gitlab.com/larryli/gitlab

    从 v8.9 之后由 @xhang 开始继续汉化项目 https://gitlab.com/xhang/gitlab

    # 如没安装 git,需提前安装
    yum install -y git
    
    # 创建 gitlba 汉化包下载目录
    mkdir -p /tmp/gitlab
    cd /tmp/gitlab
    
    # 下载最新的汉化包
    git clone https://gitlab.com/xhang/gitlab.git
    
    # 如果是要下载老版本的汉化包,需要加上老版本的分支,如果想下载 10.0.2,可以运行如下语句
    git clone https://gitlab.com/xhang/gitlab.git -b v10.0.2-zh
    
    # 停止 GitLab 并执行如下语句
    gitlab-ctl stop
    cp /tmp/gitlab/gitlab/* /opt/gitlab/embedded/service/gitlab-rails/ -rf
    
    # 复制时可能不断提示是否要覆盖,这时可能是系统每次执行 cp 命令时,其实是执行了 cp -i 命令的别名。出现这种情况可以修改~/.bashrc,在“ alias cp=’ cp-i ’”前加#注释即可。使用命令或者注销登录即可
    source ~/.bashrc
    
    # 接下来可以重新配置和启动
    gitlab-ctl reconfigure
    gitlab-ctl restart
    

    GitLab 命令

    # 语法
    gitlab-ctl command (subcommand)
    
    Service Management Commands
    
    start # 启动所有服务
    stop # 关闭所有服务
    restart # 重启所有服务
    status # 查看所有服务状态
    tail # 查看日志信息
    service-list # 列举所有启动服务
    graceful-kill # 平稳停止一个服务
    
    # 启动所有服务
    gitlab-ctl start 
    # 启动单独一个服务
    gitlab-ctl start nginx
    #查看日志,查看所有日志
    gitlab-ctl tail
    #查看具体一个日志,类似 tail -f
    gitlab-ctl tail nginx
    
    General Commands
    
    help # 帮助
    reconfigure # 修改配置文件之后,需要重新加载下
    show-config # 查看所有服务配置文件信息
    uninstall # 卸载这个软件
    cleanse # 删除 gitlab 数据,重新白手起家
    
    # 显示所有服务配置文件
    gitlab-ctl show-config
    
    # 卸载 gitlab
    gitlab-ctl uninstall
    

    QQ 邮箱配置

    默认情况下,GitLab 用 qq 邮箱注册是发不出确认邮件的。查看了网上很多邮箱配置的教程,大部分都是误导的。像这类软件,归根到底总结为一句话:一切以官网文档为准。QQ 邮箱最好用企业邮箱,本人用个人邮箱进行测试是有些小问题的,正确配置如下:

    # 编辑 gitlab.rb
    vim /etc/gitlab/gitlab.rb
    
    gitlab_rails['smtp_enable'] = true
    gitlab_rails['smtp_address'] = "smtp.exmail.qq.com"
    gitlab_rails['smtp_port'] = 465
    gitlab_rails['smtp_user_name'] = "[email protected]"
    gitlab_rails['smtp_password'] = "password"
    gitlab_rails['smtp_authentication'] = "login"
    gitlab_rails['smtp_enable_starttls_auto'] = true
    gitlab_rails['smtp_tls'] = true
    gitlab_rails['gitlab_email_from'] = '[email protected]'
    

    GitLab 使用

    登录 GitLab

    1. 在浏览器的地址栏中输入 IP 即可登录 GitLab 的界面,老版本第一次登录使用的用户名和密码为 root5iveL!fe
    2. 首次登录会强制用户修改密码。密码修改成功后,输入新密码进行登录

    创建 Project

    1.安装 Git 工具 linux:安装 Git,使用自带的源安装

    yum install git
    

    2.生成密钥文件:使用 ssh-keygen 生成密钥文件.ssh/id_rsa.pub 。

    ssh-keygen -t rsa
    

    3.在 GitLab 的主页中新建一个 Project

    4.添加 ssh key 导入步骤 2 中生成的密钥文件内容:

    Profile Settings => SSH Keys => Add SSH key
    

    简单配置

    # 配置使用 Git 仓库的人员姓名
    git config --global user.name "wangao" 
    # 配置使用 Git 仓库的人员 email,填写自己的公司邮箱
    git config --global user.email "[email protected]" 
    # 克隆项目,在本地生成同名目录,并且目录中会有所有的项目文件
    git clone [email protected]:root/test.git
    # 进入到项目目录
    cd test/ 
    # 创建需要上传到 GitLab 中的目标文件
    echo "test" > test.sh
    # 将 test.sh 文件加入到索引中
    git add test.sh
    # 将 test.sh 提交到本地仓库
    git commit -m "test.sh"
    # 将文件同步到 GitLab 服务器上
    git push -u origin master
    # 在网页中查看上传的 test.sh 文件已经同步到 GitLab 中
    
    
    3 条回复    2018-04-11 10:10:40 +08:00
    ywgx
        1
    ywgx  
       2018-04-10 19:44:33 +08:00 via Android
    wwek
        2
    wwek  
       2018-04-10 20:37:42 +08:00
    docker gitlab 了解一下
    liwl
        3
    liwl  
       2018-04-11 10:10:40 +08:00
    gitea 了解一下
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3400 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 10:39 · PVG 18:39 · LAX 03:39 · JFK 06:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.