Debian系統配置GitLab CI/CD完整流程
確保Debian系統為最新版本,并安裝必要依賴包:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix
安裝Postfix時選擇“Internet Site”,按提示配置郵件服務(用于GitLab通知)。
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
替換your-gitlab-domain.com
為你的域名或IP地址:
sudo EXTERNAL_URL="http://your-gitlab-domain.com" apt install gitlab-ce
sudo gitlab-ctl reconfigure # 應用配置
sudo gitlab-ctl restart # 重啟服務
訪問http://your-gitlab-domain.com
,完成管理員賬號初始化(默認用戶名root
)。
GitLab Runner是執行CI/CD任務的代理,需在Debian上安裝并注冊到GitLab項目。
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner
獲取GitLab項目的Runner注冊令牌(路徑:項目→Settings→CI/CD→Runners
):
sudo gitlab-runner register
按提示輸入:
http://your-gitlab-domain.com
)debian-runner
)linux
,可選)Shell
或Docker
,根據需求選擇)示例(Shell執行器):
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
> http://your-gitlab-domain.com
Please enter the gitlab-ci token for this runner:
> <粘貼注冊令牌>
Please enter the gitlab-ci description for this runner:
> [runner-name]
Please enter the gitlab-ci tags for this runner (comma separated):
> linux
Registering runner... succeeded runner=<runner-ID>
Please enter the executor: shell, docker+machine, docker-ssh+machine, kubernetes, docker, parallels, virtualbox, docker-ssh, shell:
> shell
Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded!
sudo systemctl enable gitlab-runner # 開機自啟
sudo systemctl start gitlab-runner # 立即啟動
驗證Runner狀態:
sudo gitlab-runner status
應顯示Runner is running
。
在項目根目錄創建.gitlab-ci.yml
文件,定義CI/CD流程(以構建→測試→部署為例):
stages:
- build # 構建階段
- test # 測試階段
- deploy # 部署階段
# 構建作業
build_job:
stage: build
script:
- echo "Building project..."
- mkdir -p build
- cd build && touch status.txt # 模擬構建產物
artifacts: # 將構建產物傳遞給后續作業
paths:
- build/
# 測試作業(依賴build_job的產物)
test_job:
stage: test
script:
- echo "Running tests..."
- test -f build/status.txt && echo "Tests passed!" || echo "Tests failed!"
dependencies: # 依賴build_job的輸出
- build_job
# 部署作業(手動觸發,避免誤操作)
deploy_job:
stage: deploy
script:
- echo "Deploying to production..."
- scp -r build/* user@your-server:/path/to/deploy # 示例:SCP傳輸文件
when: manual # 手動觸發
only:
- main # 僅main分支觸發
若項目需要特定環境(如Node.js、Python),可使用Docker鏡像:
image: node:18 # 使用Node.js 18鏡像
stages:
- install
- test
- deploy
install_job:
stage: install
script:
- npm install # 安裝依賴
test_job:
stage: test
script:
- npm test # 運行測試
deploy_job:
stage: deploy
script:
- echo "Deploying with Docker..."
only:
- master
若需啟動Docker服務(如測試數據庫),添加services
:
services:
- postgres:15 # 啟動PostgreSQL服務
variables:
POSTGRES_DB: test_db
POSTGRES_USER: test_user
POSTGRES_PASSWORD: test_pass
通過rules
控制不同分支的CI行為:
stages:
- build
- deploy
build_job:
stage: build
script:
- echo "Building..."
rules:
- if: $CI_COMMIT_BRANCH == "main" # main分支觸發
- if: $CI_COMMIT_BRANCH =~ /^feature\// # feature/*分支觸發
deploy_staging_job:
stage: deploy
script:
- echo "Deploying to staging..."
rules:
- if: $CI_COMMIT_BRANCH == "develop" # develop分支部署到staging
deploy_prod_job:
stage: deploy
script:
- echo "Deploying to production..."
when: manual # 生產環境手動觸發
rules:
- if: $CI_COMMIT_BRANCH == "main" # main分支手動部署
將.gitlab-ci.yml
推送到GitLab倉庫:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin your-branch-name
build_job
、test_job
)。若部署作業設置為when: manual
,可在Jobs頁面點擊作業右側的Play按鈕手動觸發。
gitlab-runner status
,若未運行,執行sudo systemctl start gitlab-runner
。.gitlab-ci.yml
文件位于項目根目錄,且語法正確(可通過GitLab的CI Lint工具驗證)。--privileged=true
)。通過以上步驟,即可在Debian系統上完成GitLab CI/CD的配置,實現項目的自動化構建、測試和部署。根據項目需求,可進一步擴展配置(如添加緩存、集成第三方工具、配置多環境變量等)。