GitLabCI作业配置技能Skill gitlab-ci-job-configuration

此技能专注于GitLab CI作业的配置和管理,用于定义作业结构、脚本编写、环境设置、规则应用、依赖处理和并行执行,是DevOps中持续集成/持续部署(CI/CD)的核心组成部分。关键词:GitLab CI、作业配置、DevOps、CI/CD、自动化、脚本、环境管理、依赖管理、并行作业。

DevOps 0 次安装 0 次浏览 更新于 3/25/2026

名称: gitlab-ci-job-configuration 用户可调用: false 描述: 用于定义GitLab CI作业、配置脚本、设置环境或管理作业依赖。涵盖作业结构和执行选项。 允许工具:

  • 读取
  • 写入
  • 编辑
  • Bash
  • Grep
  • Glob

GitLab CI - 作业配置

配置GitLab CI作业,包括脚本、环境和执行设置。

基本作业结构

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

环境配置

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

作业规则

条件执行

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

作业依赖

使用依赖

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

资源配置

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