前言

GitHub 的访问总是间歇性抽风,再加上自己也喜欢将所有数据都进行本地化的维护,故而决定部署一个私有的 gitlab 服务器,并迁移代码。安装的服务器环境为Ubuntu 22.04.2 LTS

一、GitLab 简介

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

当前 GitLab 在中国成立的合资公司极狐,并提供中文版官网,本次安装也是基于极狐版 gitlab,域名注册及邮件服务的提供商为阿里云.

二、安装

安装过程整体参考文档使用 Omnibus 安装包安装极狐 GitLab,但存在以下几点不同:

  1. 未采用默认的 443 端口,使用自定义访问端口12345
  2. 未采用默认的Let’s Encrypt,使用acme.sh配合alidns
  3. 未采用推荐的Postfix邮件通知方案,使用阿里云邮件服务。

2.1 默认安装流程

  1. 安装和配置必须的依赖项

    1
    2
    3
    4
    5
    6
    # 安装依赖
    sudo apt-get update
    sudo apt-get install -y curl openssh-server ca-certificates tzdata perl

    # 放行防火墙,未开启则不必执行
    sudo ufw allow 12345/tcp
  2. 下载/安装极狐 GitLab

    1
    2
    3
    4
    5
    # 配置极狐GitLab 软件源镜像。
    curl -fsSL https://packages.gitlab.cn/repository/raw/scripts/setup.sh | /bin/bash

    # 安装,更改 https://gitlab.example.com 为您要访问极狐GitLab 实例的 URL。
    sudo EXTERNAL_URL="https://gitlab.example.com:12345" apt-get install gitlab-jh
  3. 查看当前初始用户密码

    1
    sudo cat /etc/gitlab/initial_root_password

2.2 配置证书

  1. 重定向 http,禁用原生 letsencrypt

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 编辑配置文件
    sudo vi /etc/gitlab/gitlab.rb
    # 通过 `/gitlab_rails` 找到相关配置项并修改
    ## 重定向
    nginx['redirect_http_to_https'] = true
    ## 禁用原生letsencrypt
    letsencrypt['enable'] = false
    ## 确认访问地址,https://gitlab.example.com 为您要访问极狐GitLab 实例的 URL。
    external_url 'https://gitlab.example.com:12345'
  2. 安装acme.sh,默认将会安装到当前用户目录~/.acme.sh/中,同时会将acme.sh命令注册到环境变量中.

    1
    2
    curl  https://get.acme.sh | sh
    source ~/.bashrc
  3. 注册用户及环境变量

    1
    2
    3
    4
    5
    6
    # 更改 example@mail.com 为你的个人邮箱,注册/登录acme.sh账户
    acme.sh --register-account -m example@mail.com

    # 修改配置文件 ~/.acme.sh/account.conf ,添加alidns所需密钥
    SAVED_Ali_Key='your ali key'
    SAVED_Ali_Secret='your ali secret'
  4. 签发并部署证书

    注意: 这里必须通过--fullchain-file命令而不是--cert-file命令部署

    1
    2
    3
    4
    5
    6
    7
    # 签发证书,更改 gitlab.example.com 为您要访问极狐GitLab 实例的 URL。
    acme.sh --issue --dns dns_ali -d gitlab.example.com

    # 部署证书,此处可能会遇到权限不足问题,可以通过该命令解决`sudo chmod 777 /etc/gitlab/ssl`
    sudo acme.sh --installcert -d gitlab.example.com \
    --key-file /etc/gitlab/ssl/gitlab.example.com.key \
    --fullchain-file /etc/gitlab/ssl/gitlab.example.com.cer
  5. 部署自动更新

    1
    2
    # 自动更新
    acme.sh --upgrade --auto-upgrade
  6. 重启 gitlab,加载证书

    1
    2
    3
    4
    5
    6
    # 初次安装配置时,直接重启gitlab完成更新
    sudo gitlab-ctl reconfigure

    # 后续证书续签时,可通过以下命令更新
    sudo gitlab-ctl hup nginx
    sudo gitlab-ctl hup registry

2.3 配置邮件服务

  1. 通过修改 gitlab 配置文件/etc/gitlab/gitlab.rb设置邮件服务,服务提供商为阿里邮件推送,每日免费的 200 封已经够用了.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    # 编辑配置文件
    sudo vi /etc/gitlab/gitlab.rb
    # 通过 `/gitlab_rails` 找到相关配置项并修改
    ## 允许smtp
    gitlab_rails['smtp_enable'] = true
    ## 配置smtp地址
    gitlab_rails['smtp_address'] = "smtpdm.aliyun.com"
    ## 配置smtp端口
    gitlab_rails['smtp_port'] = 80
    ## 配置smtp用户名,用户自定义,修改mailserver.example.com为您的邮件服务地址
    gitlab_rails['smtp_user_name'] = "noreply@mailserver.example.com"
    ## 配置smtp密码,在设置邮件服务时获取
    gitlab_rails['smtp_password'] = "your smtp password"
    ## 配置smtp域名,修改mailserver.example.com为您的邮件服务地址
    gitlab_rails['smtp_domain'] = "mailserver.example.com"
    ## 配置登录方式
    gitlab_rails['smtp_authentication'] = "login"
    ## 修改邮件发送者姓名,修改mailserver.example.com为您的邮件服务地址
    gitlab_rails['gitlab_email_from'] = 'noreply@mailserver.example.com'
  2. 重启 gitlab

    1
    sudo gitlab-ctl reconfigure

2.4 配置 runner

gitlab runner 为 CI/CD 工具链的一环,通过 runner,可以达到本地化自动执行,类似github action.安装方法可以在私有化实例的网页中看到https://gitlab.example.com:12345/admin/runners.

  1. 安装 runner

    • 以下命令为直接在 Linux 系统中安装 runner
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    # Download the binary for your system
    sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

    # Give it permission to execute
    sudo chmod +x /usr/local/bin/gitlab-runner

    # Create a GitLab Runner user
    sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

    # Install and run as a service
    sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
    sudo gitlab-runner start
    • 以下命令为在 docker 中安装 runner
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    # 官方默认config路径为`/srv/gitlab-runner/config`,存在读写权限问题,故而换到用户目录中,`/home/user/gitlab-runner/config`,请将`user`换为自己的用户名
    # 由于采用了HTTPS,需要在容器中更新部署证书
    sudo docker run -d --name gitlab-runner --restart always \
    -v /home/user/gitlab-runner/config:/etc/gitlab-runner \
    -v /var/run/docker.sock:/var/run/docker.sock \
    -v /etc/gitlab/ssl:/certs \
    gitlab/gitlab-runner:latest

    # 更新证书
    sudo docker exec gitlab-runner update-ca-certificates
  2. 注册 runner

    • 以下命令为注册 Linux 的 runner,并配置执行器为 docker,在后续配置中,建议公共 runner 不配置标签. registration-tokenhttps://gitlab.example.com:12345/admin/runners中获取.
    1
    2
    3
    4
    5
    6
    7
    # 更改 gitlab.example.com 为您要访问极狐GitLab 实例的 URL。
    sudo gitlab-runner register \
    --url https://gitlab.example.com:12345/ \
    --registration-token your-registration-token \
    --tls-ca-file /etc/gitlab/ssl/gitlab.example.com.crt \
    --tls-key-file /etc/gitlab/ssl/gitlab.example.com.key \
    --executor docker
    • 以下命令为注册 docker 的 runner,并配置执行器为 docker,个人是将该类 runner 作为项目的专属 runner,并配置指定标签,your-project-registration-token项目-设置-CI/CD-Runner中获取.
    1
    2
    3
    4
    5
    6
    7
    # 请将 -v 后的值替换为创建runner时所指定的目录.
    sudo docker run --rm -it \
    -v /home/user/gitlab-runner/config:/etc/gitlab-runner \
    gitlab/gitlab-runner register \
    --url https://gitlab.example.com:12345/ \
    --registration-token your-project-registration-token \
    --executor docker

2.5 配置 Container register

使用极狐 GitLab 的容器镜像库,可以让每个项目都拥有自己的空间来存储 Docker 镜像。部署参考该文档.

  1. 参考步骤 2.2,配置 register 对应 url 的证书,示例 url 为https://registry.gitlab.example.com

  2. 修改配置文件,激活Container register

    1
    2
    3
    4
    5
    6
    7
    # 编辑配置文件
    sudo vi /etc/gitlab/gitlab.rb
    # 通过 `/registry_external_url` 找到相关配置项并修改
    registry_external_url 'https://registry.gitlab.example.com'

    # 重启gitlab,激活仓库,默认将会在5000端口启动容器镜像库服务
    sudo gitlab-ctl reconfigure

参考文档