gitlab-ci-job-configuration
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseGitLab 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: falseyaml
test:
script:
- npm test || exit 1
- npm run lint
allow_failure: falseEnvironment 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: manualyaml
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: manualJob Rules
作业规则
Conditional Execution
条件执行
yaml
job:
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: always
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: on_success
- when: neveryaml
job:
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: always
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: on_success
- when: neverChanges-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:
- Dockerfileyaml
docker:build:
rules:
- exists:
- DockerfileJob Dependencies
作业依赖
Using Dependencies
使用Dependencies
yaml
build:
stage: build
script: npm run build
artifacts:
paths:
- dist/
test:
stage: test
dependencies:
- build
script: npm testyaml
build:
stage: build
script: npm run build
artifacts:
paths:
- dist/
test:
stage: test
dependencies:
- build
script: npm testUsing Needs (DAG)
使用Needs(DAG)
yaml
test:unit:
needs:
- job: build
artifacts: true
script: npm run test:unityaml
test:unit:
needs:
- job: build
artifacts: true
script: npm run test:unitParallel Jobs
并行作业
Matrix Jobs
矩阵作业
yaml
test:
parallel:
matrix:
- NODE_VERSION: ["18", "20", "22"]
OS: ["alpine", "bullseye"]
image: node:${NODE_VERSION}-${OS}
script: npm testyaml
test:
parallel:
matrix:
- NODE_VERSION: ["18", "20", "22"]
OS: ["alpine", "bullseye"]
image: node:${NODE_VERSION}-${OS}
script: npm testSimple Parallel
简单并行
yaml
test:
parallel: 5
script: npm run test:shardyaml
test:
parallel: 5
script: npm run test:shardResource Configuration
资源配置
yaml
heavy_job:
tags:
- high-memory
resource_group: deploy
timeout: 2h
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failureyaml
heavy_job:
tags:
- high-memory
resource_group: deploy
timeout: 2h
retry:
max: 2
when:
- runner_system_failure
- stuck_or_timeout_failure