温馨提示×

Debian GitLab的CI/CD如何配置

小樊
56
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_jobtest_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