gitlab-ci-job-configuration

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

GitLab CI - Job Configuration

GitLab CI - 作业配置

Configure GitLab CI jobs with proper scripts, environments, and execution settings.
为GitLab CI作业配置合适的脚本、环境与执行设置。

Basic Job Structure

基础作业结构

yaml
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"
yaml
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"

Script Configuration

脚本配置

Multi-Line Scripts

多行脚本

yaml
build:
  script:
    - echo "Building application..."
    - npm run build
    - echo "Build complete"
yaml
build:
  script:
    - echo "Building application..."
    - npm run build
    - echo "Build complete"

Script with Exit Codes

含退出码的脚本

yaml
test:
  script:
    - npm test || exit 1
    - npm run lint
  allow_failure: false
yaml
test:
  script:
    - npm test || exit 1
    - npm run lint
  allow_failure: false

Environment Configuration

环境配置

yaml
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
yaml
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

作业规则

Conditional Execution

条件执行

yaml
job:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: on_success
    - when: never
yaml
job:
  rules:
    - if: $CI_COMMIT_BRANCH == "main"
      when: always
    - if: $CI_PIPELINE_SOURCE == "merge_request_event"
      when: on_success
    - when: never

Changes-Based Rules

基于变更的规则

yaml
test:frontend:
  rules:
    - changes:
        - "src/frontend/**/*"
        - "package.json"
yaml
test:frontend:
  rules:
    - changes:
        - "src/frontend/**/*"
        - "package.json"

Exists-Based Rules

基于存在性的规则

yaml
docker:build:
  rules:
    - exists:
        - Dockerfile
yaml
docker:build:
  rules:
    - exists:
        - Dockerfile

Job Dependencies

作业依赖

Using Dependencies

使用Dependencies

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

test:
  stage: test
  dependencies:
    - build
  script: npm test
yaml
build:
  stage: build
  script: npm run build
  artifacts:
    paths:
      - dist/

test:
  stage: test
  dependencies:
    - build
  script: npm test

Using Needs (DAG)

使用Needs(DAG)

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

Parallel Jobs

并行作业

Matrix Jobs

矩阵作业

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

Simple Parallel

简单并行

yaml
test:
  parallel: 5
  script: npm run test:shard
yaml
test:
  parallel: 5
  script: npm run test:shard

Resource Configuration

资源配置

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