GitLab是基于Debian的发行版(如Debian 11/12)上运行的常用版本控制平台,需先完成GitLab的安装与基础配置。
sudo apt update && sudo apt upgrade -y
curl(下载工具)、openssh-server(SSH连接)、ca-certificates(SSL证书)等依赖。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
sudo apt install -y gitlab-ce
/etc/gitlab/gitlab.rb文件,设置external_url为服务器IP或域名(如http://192.168.1.100),这是GitLab的访问入口。sudo nano /etc/gitlab/gitlab.rb
# 找到并修改以下行(取消注释并替换为你的地址)
external_url 'http://192.168.1.100'
sudo gitlab-ctl reconfigure # 应用配置
sudo gitlab-ctl start # 启动服务
sudo systemctl enable gitlab # 开机自启
http://<服务器IP>,首次登录需设置管理员密码(默认管理员账号为root)。若需将本地代码推送到GitLab远程仓库,需在本地Debian机器上安装Git并配置用户信息。
sudo apt update && sudo apt install -y git
git config --global user.name "Your Name"
git config --global user.email "your-email@example.com"
ssh-keygen -t rsa -b 4096 -C "your-email@example.com"
# 按提示保存密钥(默认路径~/.ssh/id_rsa)
cat ~/.ssh/id_rsa.pub),登录GitLab → 点击头像 → Settings → SSH Keys → 粘贴公钥并保存。my-app)、描述(可选),选择可见性(Private/Internal/Public),点击Create project。git@192.168.1.100:your-username/my-app.git),在本地终端执行:git clone git@192.168.1.100:your-username/my-app.git
cd my-app
git add . # 添加所有更改文件
git commit -m "Initial commit" # 提交更改(描述需清晰)
git push origin master # 推送到远程master分支(若默认分支为main,替换为main)
git pull origin master
feature/login),避免直接修改主分支。git branch new-feature # 创建新分支
git checkout new-feature # 切换到新分支
# 或合并命令:git checkout -b new-feature(创建并切换)
master(或main)。git checkout master # 切换回主分支
git merge new-feature # 合并新分支
git push origin master # 推送合并后的主分支
<<<<<<<、=======、>>>>>>>标记),然后重新提交:git add conflict-file.txt # 标记冲突已解决
git commit -m "Resolve merge conflict"
git push origin master
GitLab内置CI/CD功能,可通过.gitlab-ci.yml文件定义自动化流程(如代码测试、构建、部署)。
.gitlab-ci.yml文件:在项目根目录下创建该文件,定义流水线任务。示例(Python项目):stages:
- test
- deploy
test_job:
stage: test
script:
- pip install -r requirements.txt
- pytest # 运行测试
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp -r . user@server:/var/www/my-app # 示例:部署到服务器
only:
- main # 仅在main分支推送时触发
.gitlab-ci.yml文件推送到远程仓库,GitLab会自动启动流水线。git add .gitlab-ci.yml
git commit -m "Add CI/CD configuration"
git push origin master
Owner/Maintainer/Developer/Reporter/Guest),控制其对项目的访问权限。/etc/gitlab/gitlab.rb:gitlab_rails['gitlab_email_enabled'] = true
gitlab_rails['gitlab_email_from'] = 'your-email@163.com'
gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.163.com"
gitlab_rails['smtp_port'] = 25
gitlab_rails['smtp_user_name'] = "your-email@163.com"
gitlab_rails['smtp_password'] = "your-email-password"
gitlab_rails['smtp_domain'] = "163.com"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = false
保存后重新配置GitLab:sudo gitlab-ctl reconfigure
sudo gitlab-ctl restart
gitlab-backup命令:sudo gitlab-backup create STRATEGY=copy # 创建备份(STRATEGY=copy避免锁表)
备份文件默认保存在/var/opt/gitlab/backups目录,恢复时需将备份文件复制到该目录并执行:sudo gitlab-backup restore BACKUP=<timestamp> # 替换为备份文件名中的时间戳
通过以上步骤,你可在Debian系统上完成GitLab的安装、配置,并实现代码的版本控制、协作开发及自动化流程。根据团队需求,还可进一步优化分支策略(如GitFlow)、集成容器 registry(如Harbor)或配置HTTPS(提升安全性)。