CentOS与GitLab集成开发实践指南
在CentOS上集成GitLab前,需完成系统基础配置,确保后续流程顺利:
sudo yum update -y更新所有软件包至最新版本,修复潜在漏洞。sudo yum install -y curl policycoreutils-python openssh-server postfix安装。sudo systemctl enable sshd && sudo systemctl start sshd。sudo firewall-cmd --permanent --add-service={http,https,ssh} && sudo firewall-cmd --reload。通过官方脚本添加GitLab的Yum仓库,确保能获取最新稳定版本的安装包:
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。
执行sudo yum install -y gitlab-ce安装GitLab CE(免费版),安装过程会自动下载并配置相关组件。
编辑GitLab主配置文件/etc/gitlab/gitlab.rb,设置external_url为服务器IP或域名(如http://192.168.1.100),此配置决定了GitLab的访问入口:
sudo vi /etc/gitlab/gitlab.rb → 修改external_url 'http://your_server_ip'。
运行以下命令使配置生效并启动GitLab:
sudo gitlab-ctl reconfigure(应用配置)→ sudo gitlab-ctl start(启动服务)。
打开浏览器,输入http://your_server_ip,首次访问需设置管理员账户(root)的密码,完成后即可登录。
GitLab Runner是GitLab的CI/CD执行器,负责运行流水线任务(如编译、测试、部署)。
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.rpm.sh | sudo bash添加Runner仓库,再用sudo yum install -y gitlab-runner安装。shell,适合简单环境),完成注册。stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the application..."
- make build
test_job:
stage: test
script:
- echo "Running unit tests..."
- make test
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- make deploy
only:
- master # 仅master分支触发部署
此配置实现了“代码提交→编译→测试→部署”的自动化流程。
unicorn['worker_processes'] = 4),避免进程过多导致内存耗尽。/etc/gitlab/gitlab.rb中设置nginx['enable'] = true、nginx['ssl_protocols'] = "TLSv1.2 TLSv1.3"),提升传输效率。net.ipv4.tcp_tw_reuse = 1、net.core.somaxconn = 65535),增加并发连接能力。git lfs install→git lfs track "*.mp4"。/etc/gitlab/gitlab.rb,添加以下配置(以QQ邮箱为例):gitlab_rails['smtp_enable'] = true
gitlab_rails['smtp_address'] = "smtp.qq.com"
gitlab_rails['smtp_port'] = 465
gitlab_rails['smtp_user_name'] = "your_qq@qq.com"
gitlab_rails['smtp_password'] = "your_qq_password"
gitlab_rails['smtp_authentication'] = "login"
gitlab_rails['smtp_enable_starttls_auto'] = true
gitlab_rails['smtp_tls'] = true
gitlab_rails['gitlab_email_from'] = 'your_qq@qq.com'
运行sudo gitlab-ctl reconfigure使配置生效。
2. 使用SSH密钥认证:开发者需生成SSH密钥对(ssh-keygen -t rsa -C "your_email@example.com"),并将公钥(id_rsa.pub)添加到GitLab账户的SSH Keys设置中,替代密码认证,提升安全性。
3. 定期备份:使用GitLab内置命令创建备份(sudo gitlab-rake gitlab:backup:create),备份文件默认存储在/var/opt/gitlab/backups目录。建议设置cron定时任务(如每天凌晨2点备份):
0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create。
4. 及时更新版本:GitLab官方会定期发布安全补丁,需及时升级到最新稳定版本(sudo yum update gitlab-ce),修复已知漏洞。
通过以上步骤,可在CentOS上完成GitLab的集成部署,并通过CI/CD、性能优化、安全加固等措施,构建高效的协作开发环境。