Linux GitLab的使用教程及技巧
导读:Linux 上 GitLab 使用教程与实用技巧 一 安装与初始化 准备与要求 推荐系统:Ubuntu/Debian 或 CentOS/RHEL。 资源建议:至少 2GB RAM(推荐 4GB+)、20GB+ 磁盘,并开放 HTTP/H...
Linux 上 GitLab 使用教程与实用技巧
一 安装与初始化
- 准备与要求
- 推荐系统:Ubuntu/Debian 或 CentOS/RHEL。
- 资源建议:至少 2GB RAM(推荐 4GB+)、20GB+ 磁盘,并开放 HTTP/HTTPS/SSH 端口。
- Debian/Ubuntu 安装
- 安装依赖:
sudo apt-get update & & sudo apt-get install -y curl openssh-server ca-certificates tzdata perl - 添加仓库并安装 CE 版:
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash sudo EXTERNAL_URL="http://your_server_ip_or_domain" apt-get install -y gitlab-ce
- 安装依赖:
- CentOS/RHEL 安装(使用国内镜像示例)
- 配置清华镜像源
/etc/yum.repos.d/gitlab-ce.repo:[gitlab-ce] name=Gitlab CE Repository baseurl=https://mirrors.tuna.tsinghua.edu.cn/gitlab-ce/yum/el$releasever/ gpgcheck=0 enabled=1 - 安装与初始化:
sudo yum makecache sudo yum install -y gitlab-ce sudo gitlab-ctl reconfigure
- 配置清华镜像源
- 首次访问与登录
- 浏览器访问
http://your_server_ip_or_domain,首次使用 root 登录并设置强密码(首次登录强制修改)。
- 浏览器访问
二 日常运维命令与目录
- 常用服务管理
sudo gitlab-ctl start|stop|restart|status sudo gitlab-ctl reconfigure # 变更配置后必执行 sudo gitlab-ctl tail # 实时查看全部日志 sudo gitlab-ctl tail nginx # 指定组件日志 gitlab-rake gitlab:check SANITIZE=true --trace # 配置与依赖检查 - 主要目录与组件
- 主配置:/etc/gitlab/gitlab.rb
- 默认仓库:/var/opt/gitlab/git-data/repositories
- 组件:nginx、gitlab-shell、gitlab-workhorse、postgresql、redis、sidekiq、unicorn 等。
三 用户与仓库高频操作
- 配置 Git 全局身份
git config --global user.name "Your Name" git config --global user.email "you@example.com" - SSH 免密与克隆
- 生成密钥:
ssh-keygen -t rsa -b 4096 -C "you@example.com" - 复制公钥:
cat ~/.ssh/id_rsa.pub,粘贴到 GitLab 用户 SSH Keys - 克隆与推送:
git clone git@your_gitlab_host:namespace/project.git git add . git commit -m "init" git push -u origin main
- 生成密钥:
- 分支与合并请求
- 本地分支:
git checkout -b feature/x - 推送分支:
git push -u origin feature/x - 在 Web 界面创建 Merge Request 进行代码审查与合并。
- 本地分支:
四 备份恢复与升级迁移
- 备份与恢复
- 备份(默认目录 /var/opt/gitlab/backups):
sudo gitlab-rake gitlab:backup:create - 自定义备份目录(在
/etc/gitlab/gitlab.rb中设置):gitlab_rails['backup_path'] = "/data/backups" sudo gitlab-ctl reconfigure - 恢复(示例):
sudo gitlab-rake gitlab:backup:restore BACKUP=timestamp
- 备份(默认目录 /var/opt/gitlab/backups):
- 升级与数据库
- 升级软件包:
sudo yum update gitlab-ce(或apt-get upgrade gitlab-ce) - 数据库升级:
sudo gitlab-ctl pg-upgrade
- 升级软件包:
- 迁移要点
- 迁移前先完成全量备份;在新环境安装同版本后执行恢复;如修改仓库存储路径,先停服务、迁移数据、修改
git_data_dir、再reconfigure启动。
- 迁移前先完成全量备份;在新环境安装同版本后执行恢复;如修改仓库存储路径,先停服务、迁移数据、修改
五 安全与性能实用技巧
- 加固与网络
- 仅开放 80/443/22,使用 ufw/firewalld 放行;为公网访问启用 HTTPS(见下节)。
- HTTPS 与证书
sudo mkdir -p /etc/gitlab/ssl sudo chmod 0700 /etc/gitlab/ssl # 放置证书后修改 /etc/gitlab/gitlab.rb external_url "https://gitlab.example.com" nginx['redirect_http_to_https'] = true nginx['ssl_certificate'] = "/etc/gitlab/ssl/gitlab.example.com.crt" nginx['ssl_certificate_key'] = "/etc/gitlab/ssl/gitlab.example.com.key" sudo gitlab-ctl reconfigure - SMTP 邮件通知
gitlab_rails['smtp_address'] = "smtp.example.com" gitlab_rails['smtp_port'] = 587 gitlab_rails['smtp_user_name'] = "gitlab" gitlab_rails['smtp_password'] = "password" gitlab_rails['smtp_domain'] = "example.com" gitlab_rails['smtp_authentication'] = "plain" gitlab_rails['smtp_enable_starttls_auto'] = true sudo gitlab-ctl reconfigure - 资源与文件句柄
- 提升系统限制(示例):
ulimit -Sn 4096; ulimit -u 4096 # /etc/security/limits.conf root soft nofile 4096 root hard nofile 4096 root soft nproc 4096 root hard nproc 4096
- 提升系统限制(示例):
- 仓库存储迁移
# /etc/gitlab/gitlab.rb git_data_dir "/mnt/nfs-01/git-data" # 变更后 sudo gitlab-ctl stop sudo gitlab-ctl reconfigure sudo gitlab-ctl start - CI/CD 与部署密钥
- 在
.gitlab-ci.yml中定义 stages/build/test/deploy 实现自动化;服务器部署建议使用 Deploy Key 或 CI 变量管理密钥,避免明文存放。
- 在
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Linux GitLab的使用教程及技巧
本文地址: https://pptw.com/jishu/789100.html
