1. 安裝與基礎配置GitLab
在Linux服務器(如Ubuntu/CentOS)上通過包管理器安裝GitLab社區版(CE),例如Ubuntu使用sudo apt update && sudo apt install gitlab-ce,CentOS使用sudo yum install gitlab-ce。安裝完成后,編輯配置文件/etc/gitlab/gitlab.rb,設置external_url(如http://your_server_ip)及SMTP郵件服務器(用于通知),運行sudo gitlab-ctl reconfigure應用配置,啟動GitLab服務(sudo systemctl start gitlab)。
2. 創建用戶與權限分配
通過GitLab Web界面(http://your_server_ip)或命令行創建用戶:
dev1、dev1@example.com、SecurePass123)→點擊“Create user”。sudo gitlab-rake gitlab:create_user[username,email,password](如sudo gitlab-rake gitlab:create_user[dev1,dev1@example.com,SecurePass123])。GitLab提供內置角色(從低到高):
通過“Admin Area”→“Users”選中用戶,或在項目/組頁面“Settings”→“Members”中,為用戶分配對應角色。
3. 組織項目與團隊協作
web-app)→選擇“Private”(僅組成員可見)→點擊“Create project”。Developer)→點擊“Invite”。git clone git@your_server_ip:username/web-app.git),初始化本地倉庫(git init)、配置用戶信息(git config user.name "Dev1"、git config user.email "dev1@example.com"),推送初始代碼到遠程master分支。后續開發中,成員通過git checkout -b feature/login創建特性分支,修改代碼后提交(git add .、git commit -m "Add login functionality")并推送(git push origin feature/login)。4. 代碼提交與合并流程
feature/login)和目標分支(如master)→填寫標題(如“Add login page”)、描述(如“Implemented user login with JWT”)→點擊“Create merge request”。master分支。5. 關鍵安全與協作優化
ssh-keygen -t rsa -b 4096 -C "dev1@example.com"),復制公鑰(cat ~/.ssh/id_rsa.pub)到GitLab用戶設置→“SSH Keys”→“Add key”,實現免密克隆/推送代碼。master分支→設置“Allowed to merge”(如Maintainer)、“Allowed to push”(如Maintainer),防止普通成員誤修改主分支。.gitlab-ci.yml文件,定義流水線階段(如build、test、deploy),例如:stages:
- build
- test
- deploy
build_job:
stage: build
script:
- echo "Building the project..."
test_job:
stage: test
script:
- echo "Running tests..."
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
GitLab會自動觸發流水線,執行構建、測試、部署任務。