溫馨提示×

Debian GitLab的CI/CD如何配置

小樊
43
2025-08-30 11:42:13
欄目: 智能運維

Debian系統配置GitLab CI/CD完整流程

1. 準備Debian基礎環境

確保Debian系統為最新版本,并安裝必要依賴包:

sudo apt update && sudo apt upgrade -y
sudo apt install -y curl openssh-server ca-certificates postfix

安裝Postfix時選擇“Internet Site”,按提示配置郵件服務(用于GitLab通知)。

2. 安裝GitLab CE(社區版)

2.1 添加GitLab官方倉庫

curl -sS https://packages.gitlab.com/install/repositories/gitlab/gitlab-ce/script.deb.sh | sudo bash

2.2 安裝GitLab并配置外部URL

替換your-gitlab-domain.com為你的域名或IP地址:

sudo EXTERNAL_URL="http://your-gitlab-domain.com" apt install gitlab-ce

2.3 啟動GitLab服務

sudo gitlab-ctl reconfigure  # 應用配置
sudo gitlab-ctl restart     # 重啟服務

訪問http://your-gitlab-domain.com,完成管理員賬號初始化(默認用戶名root)。

3. 安裝并注冊GitLab Runner

GitLab Runner是執行CI/CD任務的代理,需在Debian上安裝并注冊到GitLab項目。

3.1 安裝GitLab Runner

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt install gitlab-runner

3.2 注冊Runner

獲取GitLab項目的Runner注冊令牌(路徑:項目→Settings→CI/CD→Runners):

sudo gitlab-runner register

按提示輸入:

  • GitLab實例URL(http://your-gitlab-domain.com
  • 注冊令牌
  • Runner描述(如debian-runner
  • 標簽(如linux,可選)
  • 執行器類型(推薦ShellDocker,根據需求選擇)

示例(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!

3.3 啟動Runner服務

sudo systemctl enable gitlab-runner  # 開機自啟
sudo systemctl start gitlab-runner   # 立即啟動

驗證Runner狀態:

sudo gitlab-runner status

應顯示Runner is running。

4. 編寫.gitlab-ci.yml配置文件

在項目根目錄創建.gitlab-ci.yml文件,定義CI/CD流程(以構建→測試→部署為例):

4.1 基礎配置示例

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分支觸發

4.2 Docker優化配置(可選)

若項目需要特定環境(如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

4.3 多分支動態配置(可選)

通過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分支手動部署

5. 觸發并監控CI/CD流程

5.1 提交配置文件

.gitlab-ci.yml推送到GitLab倉庫:

git add .gitlab-ci.yml
git commit -m "Add GitLab CI/CD configuration"
git push origin your-branch-name

5.2 查看Pipeline狀態

  1. 登錄GitLab,進入項目頁面。
  2. 點擊左側菜單CI/CD→Pipelines,查看Pipeline執行狀態(成功/失?。?。
  3. 點擊Pipeline ID,進入Jobs頁面,查看每個作業的日志(如build_job、test_job)。

5.3 手動觸發作業

若部署作業設置為when: manual,可在Jobs頁面點擊作業右側的Play按鈕手動觸發。

6. 常見問題排查

  • Runner未啟動:檢查gitlab-runner status,若未運行,執行sudo systemctl start gitlab-runner。
  • Pipeline未觸發:確認.gitlab-ci.yml文件位于項目根目錄,且語法正確(可通過GitLab的CI Lint工具驗證)。
  • 權限問題:確保Runner有權限訪問項目倉庫(如使用SSH密鑰或個人訪問令牌)。
  • Docker執行器問題:若使用Docker執行器,需確保Runner所在機器安裝了Docker,并配置了正確的權限(如--privileged=true)。

通過以上步驟,即可在Debian系統上完成GitLab CI/CD的配置,實現項目的自動化構建、測試和部署。根據項目需求,可進一步擴展配置(如添加緩存、集成第三方工具、配置多環境變量等)。

0
亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女