温馨提示×

GitLab在Linux怎么用

小樊
43
2026-08-24 22:31:24
栏目: 智能运维

GitLab 在 Linux 上的使用,通常分为 「安装部署」「日常使用(代码管理、CI/CD)」 两部分。下面我用 最常见、最实用 的方式给你讲清楚。


一、GitLab 是什么?

GitLab 是一个 基于 Git 的代码托管 + DevOps 平台,功能包括:

  • Git 仓库管理
  • 代码审查(Merge Request)
  • CI/CD(持续集成/部署)
  • Issue、Wiki、权限管理

分为:

  • GitLab CE(社区版,免费)
  • GitLab EE(企业版,收费)

二、在 Linux 上安装 GitLab(最常见方式)

✅ 推荐系统

  • Ubuntu 20.04 / 22.04
  • CentOS 7 / Rocky / AlmaLinux(较新版本)

1️⃣ 安装依赖(Ubuntu 示例)

sudo apt update
sudo apt install -y curl openssh-server ca-certificates tzdata

2️⃣ 添加 GitLab 仓库

curl -fsSL https://packages.gitlab.com/gpg.key | sudo gpg --dearmor -o /usr/share/keyrings/gitlab.gpg

echo "deb [signed-by=/usr/share/keyrings/gitlab.gpg] https://packages.gitlab.com/gitlab/gitlab-ce/ubuntu $(lsb_release -cs) main" | \
sudo tee /etc/apt/sources.list.d/gitlab_gitlab-ce.list

3️⃣ 安装 GitLab

sudo apt update
sudo apt install -y gitlab-ce

4️⃣ 配置 GitLab

编辑配置文件:

sudo nano /etc/gitlab/gitlab.rb

常见配置:

external_url 'http://gitlab.example.com'

如果是本机测试:

external_url 'http://192.168.1.100'

保存后执行:

sudo gitlab-ctl reconfigure

5️⃣ 访问 GitLab

浏览器访问:

http://服务器IP

首次登录:

  • 用户名:root
  • 密码在:
sudo cat /etc/gitlab/initial_root_password

三、GitLab 日常使用(重点)

1️⃣ 创建项目

  1. 登录 GitLab
  2. 点击 New Project
  3. 选择:
    • Blank project
    • 或 Import from GitHub / Git

2️⃣ 本地 Git 使用 GitLab

配置 Git

git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

克隆仓库

git clone http://服务器IP/用户名/项目名.git

提交代码

git add .
git commit -m "first commit"
git push origin main

3️⃣ SSH 方式(推荐)

生成 SSH Key:

ssh-keygen -t rsa -b 4096 -C "your@email.com"

查看公钥:

cat ~/.ssh/id_rsa.pub

复制到 GitLab:

User → Preferences → SSH Keys

四、GitLab CI/CD(核心功能)

1️⃣ 在项目根目录创建

nano .gitlab-ci.yml

示例:

stages:
  - build
  - test

build_job:
  stage: build
  script:
    - echo "Building..."

test_job:
  stage: test
  script:
    - echo "Testing..."

提交后自动触发流水线。


五、常用 GitLab 管理命令

sudo gitlab-ctl status        # 查看状态
sudo gitlab-ctl restart      # 重启
sudo gitlab-ctl stop         # 停止
sudo gitlab-ctl tail         # 查看日志

六、常见问题

❓ 内存占用高?

GitLab 官方建议:

  • 最少 4GB 内存
  • 否则容易 502

❓ 端口冲突?

修改:

external_url 'http://IP:8080'

七、你适合哪种用法?

场景 建议
个人学习 本机 / 虚拟机
公司内网 自建 GitLab
不想维护 GitLab.com(云端)

如果你愿意,我可以:

  • 给你 一步步的 CentOS / Ubuntu 安装脚本
  • 教你 从零搭建 GitLab + CI/CD
  • 或直接 对比 GitLab vs GitHub vs Gitee

你现在的 Linux 版本是什么?打算个人用还是公司用?

0