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
)。