在Debian上配置GitLab CI/CD流程的完整步驟
確保Debian系統已更新至最新版本,并安裝GitLab所需的依賴組件:
sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix
安裝Postfix時,選擇“Internet Site”類型,填寫系統域名(如example.com)以啟用郵件通知。
添加GitLab官方APT倉庫并安裝GitLab CE:
# 添加GitLab倉庫
curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash
# 安裝GitLab CE
sudo apt install -y gitlab-ce
# 配置GitLab外部訪問地址(替換為你的域名/IP)
sudo vim /etc/gitlab/gitlab.rb
# 修改以下行(取消注釋并替換值)
external_url 'http://your-gitlab-domain-or-ip'
# 重新配置并啟動GitLab
sudo gitlab-ctl reconfigure
sudo gitlab-ctl start
等待幾分鐘,通過瀏覽器訪問http://your-gitlab-domain-or-ip,使用初始管理員賬號(root)登錄。
GitLab Runner是執行.gitlab-ci.yml中任務的守護進程,需單獨安裝:
# 添加GitLab Runner倉庫
curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
# 安裝GitLab Runner
sudo apt install -y gitlab-runner
# 注冊Runner(替換為你的GitLab實例URL和注冊Token)
sudo gitlab-runner register
注冊時需填寫以下信息:
http://your-gitlab-domain-or-ipSettings→CI/CD→Runners中獲取docker(隔離環境)或shell(直接運行命令)debian-runner)ci),用于限制任務匹配在項目根目錄創建.gitlab-ci.yml,定義CI/CD流程的核心邏輯(以Java項目為例):
# 定義流程階段(按順序執行)
stages:
- build
- test
- deploy
# 全局變量(可選)
variables:
MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository"
# 緩存依賴(加速后續構建)
cache:
paths:
- .m2/repository/
# 構建階段
build:
stage: build
image: maven:3.8.7-openjdk-11 # 使用Maven鏡像
script:
- echo "Building project..."
- mvn clean package -DskipTests
artifacts:
paths:
- target/*.jar # 保存構建產物(供后續階段使用)
expire_in: 1 hour # 產物有效期
# 測試階段
test:
stage: test
image: maven:3.8.7-openjdk-11
script:
- echo "Running unit tests..."
- mvn test
artifacts:
when: always # 無論測試是否通過都保存結果
paths:
- target/surefire-reports/*.xml # 保存測試報告
# 部署階段(僅master分支觸發)
deploy:
stage: deploy
image: alpine:latest # 使用輕量級鏡像
script:
- echo "Deploying to production..."
- apk add --no-cache openssh
- ssh -o StrictHostKeyChecking=no user@your-server "mkdir -p /opt/app && scp target/*.jar user@your-server:/opt/app/app.jar && ssh user@your-server 'systemctl restart app.service'"
only:
- master # 限制僅在master分支推送時觸發
說明:
stages: 定義流程順序(build→test→deploy);image: 指定任務運行的Docker鏡像(需提前拉?。?;script: 任務執行的具體命令;artifacts: 傳遞產物(如構建結果、測試報告)給后續階段;only: 限制任務觸發的分支(如僅master分支部署)。將.gitlab-ci.yml文件提交到GitLab倉庫,流水線將自動觸發:
git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin master
觸發后,可在GitLab項目頁面→CI/CD→Pipelines查看流水線狀態(運行中/成功/失?。?,點擊任務可查看詳細日志。
若部署階段需要SSH連接遠程服務器,需將私鑰添加為GitLab CI/CD變量:
# 生成SSH密鑰(若未生成)
ssh-keygen -t rsa -b 4096 -C "gitlab-ci@example.com"
# 復制公鑰到遠程服務器
cat ~/.ssh/id_rsa.pub | ssh user@your-server "mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys"
# 將私鑰添加為GitLab變量
# 在項目頁面→`Settings`→`CI/CD`→`Variables`中添加:
# Key: SSH_PRIVATE_KEY
# Value: (粘貼私鑰內容)
# Type: File(可選,用于直接寫入文件)
更新.gitlab-ci.yml中的before_script,自動加載SSH密鑰:
before_script:
- apk add --no-cache openssh
- echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
- mkdir -p ~/.ssh
- chmod 700 ~/.ssh
- ssh-keyscan your-server >> ~/.ssh/known_hosts
- chmod 644 ~/.ssh/known_hosts
通過image字段指定任務運行的鏡像,避免重復安裝工具(如Maven、Node.js):
build:
stage: build
image: node:18-alpine # 使用Node.js鏡像
script:
- npm install
- npm run build
推薦使用官方鏡像(如maven:3.8.7-openjdk-11、node:18-alpine),減少鏡像大小和依賴問題。
通過only/except或rules限制任務觸發條件(如僅在特定分支、標簽或MR時運行):
deploy_staging:
stage: deploy
script:
- echo "Deploying to staging..."
only:
- staging # 僅在staging分支觸發
deploy_production:
stage: deploy
script:
- echo "Deploying to production..."
only:
- tags # 僅打標簽時觸發(如v1.0.0)
或使用rules實現更復雜的邏輯(如合并請求時運行測試):
test_on_mr:
stage: test
script:
- echo "Running tests for MR..."
rules:
- if: '$CI_PIPELINE_SOURCE == "merge_request_event"' # 僅在MR時運行
when: always