Ubuntu 上 GitLab 项目管理实操指南
一 安装与初始化
sudo apt update && sudo apt install -y curl openssh-server ca-certificates postfixcurl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bashsudo apt install -y gitlab-cesudo vim /etc/gitlab/gitlab.rb,设置 external_url 'http://你的域名或IP'sudo gitlab-ctl reconfigure && sudo gitlab-ctl starthttp://你的域名或IP,设置管理员密码后进入管理界面。二 组织与权限管理
三 代码协作与分支策略
git clone <项目URL>git add . → git commit -m "..." → git push origin main四 CI/CD 与自动化
.gitlab-ci.yml,示例:stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building..."
- make build
test_job:
stage: test
script:
- echo "Testing..."
- make test
deploy_prod:
stage: deploy
script:
- echo "Deploying..."
only:
- main
/etc/gitlab/gitlab.rb 配置 SMTP 并 sudo gitlab-ctl reconfigure 使配置生效。五 备份恢复与运维安全
sudo gitlab-backup create(默认归档至 /var/opt/gitlab/backups)0 2 * * * /opt/gitlab/bin/gitlab-backup create STRATEGY=copy CRON=1/etc/gitlab/gitlab.rb 设置 gitlab_rails['backup_keep_time'] = 7776000(保留 90 天,单位秒)sudo chown git:git /var/opt/gitlab/backups/时间戳_gitlab版本号_gitlab_backup.tarsudo gitlab-ctl stop puma && sudo gitlab-ctl stop sidekiqsudo gitlab-backup restore BACKUP=时间戳_gitlab版本号sudo gitlab-ctl restart,随后可执行 gitlab-rake gitlab:check SANITIZE=true 等校验/etc/gitlab/gitlab.rb 与 /etc/gitlab/gitlab-secrets.json,需单独手动备份;恢复时版本需与备份时保持一致或兼容。sudo gitlab-ctl status/start/stop/restart/tailsudo ufw allow 80,443,22/tcp