GitLab 在 Linux 上的使用,通常分为 「安装部署」 和 「日常使用(代码管理、CI/CD)」 两部分。下面我用 最常见、最实用 的方式给你讲清楚。
GitLab 是一个 基于 Git 的代码托管 + DevOps 平台,功能包括:
分为:
sudo apt update
sudo apt install -y curl openssh-server ca-certificates tzdata
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
sudo apt update
sudo apt install -y gitlab-ce
编辑配置文件:
sudo nano /etc/gitlab/gitlab.rb
常见配置:
external_url 'http://gitlab.example.com'
如果是本机测试:
external_url 'http://192.168.1.100'
保存后执行:
sudo gitlab-ctl reconfigure
浏览器访问:
http://服务器IP
首次登录:
rootsudo cat /etc/gitlab/initial_root_password
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
生成 SSH Key:
ssh-keygen -t rsa -b 4096 -C "your@email.com"
查看公钥:
cat ~/.ssh/id_rsa.pub
复制到 GitLab:
User → Preferences → SSH Keys
nano .gitlab-ci.yml
示例:
stages:
- build
- test
build_job:
stage: build
script:
- echo "Building..."
test_job:
stage: test
script:
- echo "Testing..."
提交后自动触发流水线。
sudo gitlab-ctl status # 查看状态
sudo gitlab-ctl restart # 重启
sudo gitlab-ctl stop # 停止
sudo gitlab-ctl tail # 查看日志
GitLab 官方建议:
修改:
external_url 'http://IP:8080'
| 场景 | 建议 |
|---|---|
| 个人学习 | 本机 / 虚拟机 |
| 公司内网 | 自建 GitLab |
| 不想维护 | GitLab.com(云端) |
如果你愿意,我可以:
你现在的 Linux 版本是什么?打算个人用还是公司用?