GitLab Linux集成开发环境搭建指南
小樊
45
2025-12-06 15:53:53
GitLab Linux集成开发环境搭建指南
一 环境准备与规划
- 操作系统与权限:建议使用 Ubuntu/Debian 或 CentOS/RHEL,以具有 sudo 权限的账号操作。
- 硬件与网络:至少 2 核 CPU、4GB 内存、20GB 磁盘;开放 80/443(HTTP/HTTPS)与 22(SSH)端口;域名可解析到服务器;如需邮件通知,准备 SMTP 信息。
- 访问方式:准备通过 域名或服务器 IP 访问 GitLab Web 界面。
二 安装与部署
- 原生安装(Ubuntu/Debian)
- 更新系统并安装依赖:sudo apt update && sudo apt install -y curl openssh-server ca-certificates tzdata perl
- 添加官方仓库:curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
- 安装 GitLab CE:sudo apt install gitlab-ce
- 设置访问地址:编辑 /etc/gitlab/gitlab.rb,设置 external_url(如:external_url ‘http://your-domain-or-ip’)
- 使配置生效:sudo gitlab-ctl reconfigure
- 启动服务:sudo gitlab-ctl start
- 访问:浏览器打开 http://your-domain-or-ip,首次访问设置管理员密码
- 防火墙(UFW):sudo ufw allow 80,443;如需 SSH:sudo ufw allow OpenSSH
- 原生安装(CentOS/RHEL)
- 安装依赖:sudo yum install -y curl policycoreutils-python openssh-server postfix;sudo systemctl enable --now postfix
- 添加仓库:curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash
- 安装 GitLab CE:sudo yum install gitlab-ce
- 配置 external_url 并 sudo gitlab-ctl reconfigure;sudo gitlab-ctl start
- 防火墙(firewalld):sudo firewall-cmd --permanent --add-service={http,https}; sudo firewall-cmd --reload
- Docker Compose 快速部署(推荐用于测试/快速上线)
- 创建 docker-compose.yml(示例):
version: ‘3.6’
services:
gitlab:
image: gitlab/gitlab-ee:17.4.5-ee.0
container_name: gitlab
restart: always
ports:
- ‘5480:80’
- ‘5443:443’
- ‘5022:22’
volumes:
- ‘./config:/etc/gitlab’
- ‘./logs:/var/log/gitlab’
- ‘./data:/var/opt/gitlab’
shm_size: ‘256m’
- 启动:docker-compose up -d
- 获取初始 root 密码:docker exec -it gitlab /bin/bash -lc ‘cat /etc/gitlab/initial_root_password’
- 访问:http://your-domain-or-ip:5480(HTTP),或配置反向代理到 80/443
三 安全与运维配置
- 启用 HTTPS(推荐):使用 Let’s Encrypt 与 Certbot
- 安装:sudo apt-get install -y certbot python3-certbot-nginx
- 申请并自动配置:sudo certbot --nginx -d yourdomain.com
- 自动续期:可加入系统定时任务(certbot 默认会创建 systemd timer)
- 防火墙与端口:仅开放 80/443/22;云服务器安全组同步放行对应端口
- 备份与恢复:
- 手动备份:sudo gitlab-backup create(备份至 /var/opt/gitlab/backups)
- 恢复:先停止相关服务(sudo gitlab-ctl stop puma/unicorn),再执行 sudo gitlab-backup restore BACKUP=timestamp,最后 sudo gitlab-ctl reconfigure && gitlab-ctl start
- 监控与日志:
- 服务状态:sudo gitlab-ctl status
- 实时日志:sudo gitlab-ctl tail;关键日志路径:/var/log/gitlab/
- 性能与资源:内存不足时可增加 Swap(示例:fallocate -l 2G /swapfile; chmod 600 /swapfile; mkswap /swapfile; swapon /swapfile; echo ‘/swapfile none swap sw 0 0’ | tee -a /etc/fstab)
四 开发者接入与 CI/CD
- 账号与 SSH:
- 生成密钥:ssh-keygen -t ed25519 -C “you@example.com”
- 将公钥(~/.ssh/id_ed25519.pub)添加到 GitLab 用户 SSH Keys
- 克隆仓库:git clone git@your-gitlab:namespace/project.git(或 HTTPS 方式)
- IDE 集成:
- VS Code:安装 GitLens 等扩展
- IntelliJ IDEA:内置 Git 集成
- Eclipse:安装 EGit 插件
- GitLab Runner 与 CI/CD:
- 安装 Runner(Debian/Ubuntu 示例):curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash && sudo apt-get install gitlab-runner
- 注册 Runner:sudo gitlab-runner register(填写 GitLab 实例 URL 与 Registration Token)
- 示例 .gitlab-ci.yml:
stages:
- build
- test
- deploy
build:
stage: build
script:
- echo “Building…”
test:
stage: test
script:
- echo “Running tests…”
deploy:
stage: deploy
script:
- echo “Deploying…”
- 提交后可在项目的 Pipelines 页面查看执行状态