CentOS环境下GitLab的高效使用技巧
sudo yum install -y curl policycoreutils-python openssh-server postfix(postfix用于邮件通知,若无需邮件可替换为false)。curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.rpm.sh | sudo bash。/etc/gitlab/gitlab.rb核心配置,关键项包括:设置external_url(如http://your_server_ip)、开启SMTP邮件(需配置smtp_enable=true及相关参数)、调整SSH端口(gitlab_rails['gitlab_shell_ssh_port']=2222,避免默认端口被扫描);修改后执行sudo gitlab-ctl reconfigure使配置生效。/etc/sysctl.conf添加:net.core.somaxconn=65535(最大连接队列长度)、net.ipv4.tcp_tw_reuse=1(复用TIME-WAIT连接)、vm.swappiness=10(减少内存交换);执行sysctl -p使配置生效。unicorn['worker_processes']=4(根据CPU核心数调整,通常为CPU核心数的1-2倍)、unicorn['timeout']=300(请求超时时间,避免长时间阻塞)。sidekiq['concurrency']=25(并发任务数,根据CPU核心数调整)、sidekiq['queues']=["default", "gitlab"](指定任务队列)。gitlab_rails['cache_store']=:memory_store, { size: 64.megabytes }。80、443、22/自定义SSH端口);使用CDN加速静态资源(如项目文档、头像)。sudo firewall-cmd --permanent --add-service=http --add-service=https --add-port=2222/tcp;sudo firewall-cmd --reload)。sudo yum install certbot python2-certbot-nginx;sudo certbot --nginx -d yourdomain.com),配置external_url 'https://yourdomain.com'及SSL证书路径。ssh-keygen -t rsa -b 4096 -C "your_email@example.com";cat ~/.ssh/id_rsa.pub | ssh git@your_server_ip "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys")。/etc/gitlab/gitlab.rb设置gitlab_rails['otp_enabled']=true,强制用户启用2FA。gitlab_rails['ldap_enabled']=true配置LDAP认证(如Active Directory),实现统一用户管理。gitlab_rails['gitlab_password_complexity']={ 'min_length'=>12, 'require_uppercase'=>true, 'require_lowercase'=>true, 'require_numbers'=>true, 'require_symbols'=>true })。sudo yum update gitlab-ce);每周备份数据(sudo gitlab-rake gitlab:backup:create);监控日志(sudo gitlab-ctl tail)及时发现异常。git命令行替代图形界面,提升效率(如git clone git@your_server_ip:group/project.git克隆项目、git push origin feature-branch推送分支)。Git Flow(develop分支用于开发,feature/*分支用于功能开发,release/*分支用于版本发布)或GitLab Flow(main分支为生产环境,production/*分支用于线上变更)。Issues创建任务(分配负责人、设置截止日期)、Merge Requests(MR)进行代码审查(关联CI/CD流水线,通过后才允许合并)。Kanban看板(在项目设置中开启),通过列(如“To Do”、“In Progress”、“Done”)管理任务状态。.gitlab-ci.yml文件,定义流水线步骤(如build、test、deploy);示例:stages: - build - test - deploy build_job: stage: build script: - echo "Building the project..." - mvn clean package test_job: stage: test script: - echo "Running tests..." - mvn test deploy_job: stage: deploy script: - echo "Deploying to production..." - scp target/*.jar user@server:/opt/app only: - main 提交代码后,GitLab Runner会自动触发流水线执行。sudo gitlab-rake gitlab:backup:create),备份文件默认存储在/var/opt/gitlab/backups目录;可通过cron设置每日自动备份(如0 2 * * * /usr/bin/gitlab-rake gitlab:backup:create)。sudo gitlab-ctl stop unicorn;sudo gitlab-ctl stop sidekiq),执行恢复命令(sudo gitlab-rake gitlab:backup:restore BACKUP=2025-10-17_12-00-00,BACKUP为备份文件名),恢复后重启服务(sudo gitlab-ctl start)。