SuperAPI AI 工具集

gitlab-ci-job-configuration

部署与运维

查看项目

更新于 10-16 04:15

技能介绍

用途

该技能用于配置 GitLab CI 作业,帮助在 .gitlab-ci.yml 中定义作业结构、脚本、运行环境、执行规则、作业依赖与资源参数。适用于需要编写或调整 GitLab CI 流水线作业定义的场景。

主要能力

作业基本结构

作业可包含 stage、image、before_script、script、after_script、rules 等字段。示例:

job_name:
  stage: test
  image: node:20-alpine
  before_script:
    - npm ci
  script:
    - npm test
  after_script:
    - echo "Cleanup tasks"
  rules:
    - if: $CI_COMMIT_BRANCH == "main"

脚本配置

支持多行脚本,以及通过退出码控制失败行为。示例:

build:
  script:
    - echo "Building application..."
    - npm run build
    - echo "Build complete"
test:
  script:
    - npm test || exit 1
    - npm run lint
  allow_failure: false

环境配置

可定义部署环境,包括名称、URL、停止作业(on_stop)以及停止动作。示例:

deploy:production:
  stage: deploy
  script:
    - ./deploy.sh
  environment:
    name: production
    url: https://example.com
    on_stop: stop:production
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: manual

stop:production:
  stage: deploy
  script:
    - ./teardown.sh
  environment:
    name: production
    action: stop
  when: manual

作业规则

支持基于条件、文件变更(changes)和文件存在(exists)的规则。示例:

job:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: on_success
    - when: never
test:frontend:
  rules:
    - changes:
        - "src/frontend/**/*"
        - "package.json"
docker:build:
  rules:
    - exists:
        - Dockerfile

作业依赖

可通过 dependencies 或 needs(DAG)声明依赖关系。示例:

build:
  stage: build
  script: npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  dependencies:
    - build
  script: npm test
test:unit:
  needs:
    - job: build
      artifacts: true
  script: npm run test:unit

并行作业

支持矩阵并行与简单并行。示例:

test:
  parallel:
    matrix:
      - NODE_VERSION: ["18", "20", "22"]
        OS: ["alpine", "bullseye"]
  image: node:${NODE_VERSION}-${OS}
  script: npm test
test:
  parallel: 5
  script: npm run test:shard

资源配置

可设置 tags、resource_group、timeout、retry 等。示例:

heavy_job:
  tags:
    - high-memory
  resource_group: deploy
  timeout: 2h
  retry:
    max: 2
    when:
      - runner_system_failure
      - stuck_or_timeout_failure

使用方式

在 GitLab CI 配置文件(.gitlab-ci.yml)中按上述字段组织作业定义。具体安装命令、运行命令或调用方式暂未提供。

适用场景

  • 定义 GitLab CI 作业及其执行阶段。
  • 配置作业脚本、前后置脚本与失败处理。
  • 设置部署环境及环境停止作业。
  • 通过规则控制作业的条件执行、文件变更触发与文件存在触发。
  • 管理作业间的依赖关系与产物传递。
  • 配置并行作业与资源限制。

限制

资料未提供该技能的安装方式、运行环境要求、版本兼容性、收费或价格信息,也未提供官方文档链接或额外使用步骤。