1. 在Debian上安裝GitLab
首先更新系統包并安裝必要依賴,確保系統處于最新狀態:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix
添加GitLab官方APT倉庫,替換$(lsb_release -cs)
為你的Debian發行版代號(如bookworm
):
curl https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
安裝GitLab Community Edition(CE)并設置外部訪問URL(替換為你的服務器IP或域名):
sudo EXTERNAL_URL="http://your_server_ip_or_domain" apt install -y gitlab-ce
完成安裝后,重新配置GitLab并啟動服務:
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
通過瀏覽器訪問http://your_server_ip_or_domain
,完成管理員賬號初始化設置。
2. 安裝并配置GitLab Runner
GitLab Runner是執行CI/CD任務的代理工具,需單獨安裝。首先添加Runner倉庫并安裝:
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-ci-multi-runner/script.deb.sh | sudo bash
sudo apt install -y gitlab-ci-multi-runner
注冊Runner到GitLab項目:運行以下命令,按提示輸入GitLab實例URL(如http://your_server_ip_or_domain
)和項目注冊令牌(在GitLab項目→Settings→CI/CD→Runners中獲?。?/p>
sudo gitlab-runner register
選擇執行器(推薦shell
或docker
,docker
可實現環境隔離),完成注冊后啟動Runner服務并設置開機自啟:
sudo systemctl daemon-reload
sudo systemctl start gitlab-runner
sudo systemctl enable gitlab-runner
驗證Runner狀態:
sudo gitlab-runner status
確保Runner顯示為running
,表示已就緒。
3. 創建.gitlab-ci.yml文件定義CI/CD流程
在項目根目錄下創建.gitlab-ci.yml
文件,定義流水線的階段(stages)和任務(jobs)。以下是一個基礎示例,包含構建、測試、部署三個階段:
stages:
- build # 構建階段:編譯代碼、生成產物
- test # 測試階段:運行單元測試、集成測試
- deploy # 部署階段:將產物部署到目標環境
variables:
DOCKER_IMAGE: "myapp:latest" # 定義Docker鏡像名稱(可選)
build_job:
stage: build
script:
- echo "Building the project..."
- ./gradlew build # 示例:Gradle構建(根據項目類型調整)
artifacts:
paths:
- build/ # 保存構建產物,供后續階段使用
test_job:
stage: test
script:
- echo "Running tests..."
- ./gradlew test # 示例:Gradle測試(根據項目類型調整)
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp -r build/* user@your_production_server:/var/www/myapp/ # 示例:SCP復制產物到生產服務器
only:
- master # 僅當master分支有推送時觸發
將文件提交到GitLab倉庫,觸發流水線:
git add .gitlab-ci.yml
git commit -m "Add CI/CD configuration with GitLab Runner"
git push origin master
在GitLab項目→CI/CD→Pipelines頁面,可查看流水線運行狀態和日志。
4. 高級配置優化
SSH密鑰配置:若部署階段需要SSH訪問遠程服務器,需將私鑰添加到GitLab CI/CD變量中。
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
。id_rsa.pub
)添加到GitLab項目→Settings→CI/CD→Variables(變量名如SSH_PUBLIC_KEY
)。id_rsa
)添加到GitLab項目→Settings→CI/CD→Variables(變量名如SSH_PRIVATE_KEY
,設置為Masked
和Protected
)。.gitlab-ci.yml
的before_script
中配置SSH客戶端:before_script:
- 'which ssh-agent || (apt-get update -y && apt-get install -y openssh-client)'
- eval $(ssh-agent -s)
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan your_production_server >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
Docker集成:使用Docker鏡像作為Runner執行環境,簡化依賴管理。修改Runner注冊時的執行器為docker
,并在.gitlab-ci.yml
中指定鏡像:
build_job:
stage: build
image: node:18 # 使用Node.js 18鏡像
script:
- npm install
- npm test
緩存優化:通過緩存依賴項(如node_modules
、~/.m2
)加速構建,減少重復下載:
build_job:
stage: build
script:
- npm install
- npm test
cache:
paths:
- node_modules/ # 緩存node_modules目錄
環境變量管理:將敏感信息(如API密鑰、數據庫密碼)存儲為GitLab CI/CD變量(在項目→Settings→CI/CD→Variables中添加),避免硬編碼在.gitlab-ci.yml
中。