ubuntu

GitLab与Ubuntu集成解决方案

小樊
42
2025-09-09 02:02:40
栏目: 智能运维

GitLab与Ubuntu集成可通过以下方案实现,以下步骤基于官方推荐,适用于Ubuntu 20.04/22.04等版本:

一、基础安装配置

  1. 系统准备
    更新系统并安装依赖包:

    sudo apt update && sudo apt install -y curl openssh-server ca-certificates postfix
    

    (安装Postfix时选择“Internet Site”并配置主机名)

  2. 添加GitLab仓库
    导入GPG密钥并添加APT源:

    curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
    
  3. 安装GitLab
    指定外部访问URL(如http://your-server-ip)并安装:

    sudo EXTERNAL_URL="http://your-server-ip" apt install gitlab-ce
    

    安装完成后,服务会自动启动。

  4. 访问与初始化
    浏览器输入http://your-server-ip,使用默认用户名root及安装时生成的密码登录。

二、进阶配置

  1. 启用HTTPS
    使用Let’s Encrypt配置SSL证书:

    sudo apt install certbot python3-certbot-nginx
    sudo certbot --nginx -d your-gitlab-domain.com
    

    修改/etc/gitlab/gitlab.rb设置external_url 'https://your-gitlab-domain.com',然后执行:

    sudo gitlab-ctl reconfigure
    
  2. 防火墙设置
    若启用UFW,开放HTTP/HTTPS端口:

    sudo ufw allow http
    sudo ufw allow https
    sudo ufw enable
    
  3. 集成CI/CD

    • 在项目根目录创建.gitlab-ci.yml文件,定义构建、测试、部署流程,例如:
      stages:
        - build
        - test
      build:
        stage: build
        script:
          - echo "Building..."
      
    • 注册GitLab Runner执行CI/CD任务。

三、可选方案:Docker部署(推荐)

  1. 安装Docker

    sudo apt install -y docker.io
    
  2. 运行GitLab容器

    sudo docker run --detach \
      --hostname gitlab.example.com \
      --publish 443:443 --publish 80:80 --publish 22:22 \
      --name gitlab \
      --restart always \
      --volume /etc/gitlab:/etc/gitlab \
      --volume /var/run/docker.sock:/var/run/docker.sock \
      gitlab/gitlab-ce:latest
    
  3. 配置与访问
    通过docker exec进入容器修改配置,或直接访问http://your-server-ip完成初始化。

四、验证与维护

注意事项

以上步骤综合自官方指南,具体配置可根据实际需求调整。

0
看了该问题的人还看了