流水线类型 #
一、Pipeline类型概览 #
GitLab CI支持多种Pipeline类型,满足不同的使用场景。
text
┌─────────────────────────────────────────────────────────────┐
│ Pipeline 类型 │
├─────────────────────────────────────────────────────────────┤
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ 分支流水线 │ │ MR流水线 │ │
│ │ (Branch) │ │ (Merge Request) │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ 定时流水线 │ │ 手动流水线 │ │
│ │ (Scheduled) │ │ (Manual) │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
│ ┌─────────────────┐ ┌─────────────────┐ │
│ │ 父子流水线 │ │ 多项目流水线 │ │
│ │ (Parent-Child) │ │ (Multi-project) │ │
│ └─────────────────┘ └─────────────────┘ │
│ │
└─────────────────────────────────────────────────────────────┘
二、分支流水线(Branch Pipeline) #
基本概念 #
分支流水线是最常见的类型,当推送代码到分支时自动触发。
yaml
stages:
- build
- test
build:
stage: build
script:
- npm run build
test:
stage: test
script:
- npm test
触发条件 #
yaml
job_name:
script: echo "Hello"
only:
- branches
排除特定分支 #
yaml
job_name:
script: echo "Hello"
only:
- branches
except:
- main
- develop
使用rules控制 #
yaml
job_name:
script: echo "Hello"
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: always
- if: $CI_COMMIT_BRANCH == "develop"
when: manual
- when: never
三、合并请求流水线(Merge Request Pipeline) #
基本概念 #
MR流水线在创建或更新Merge Request时触发,用于代码审查前的自动化验证。
yaml
stages:
- test
test:
stage: test
script:
- npm test
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
MR流水线配置 #
yaml
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
test:
stage: test
script:
- npm test
lint:
stage: test
script:
- npm run lint
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
MR流水线优势 #
| 优势 | 说明 |
|---|---|
| 早期发现问题 | 合并前发现代码问题 |
| 提高代码质量 | 强制通过测试才能合并 |
| 自动化审查 | 自动运行lint、安全扫描等 |
| 合并条件 | 可设置为合并的必要条件 |
设置MR流水线为合并条件 #
- 进入项目Settings → Merge requests
- 启用"Merge checks"
- 勾选"Pipelines must succeed"
四、定时流水线(Scheduled Pipeline) #
创建定时流水线 #
- 进入项目CI/CD → Schedules
- 点击"New schedule"
- 设置Cron表达式和时间
- 选择目标分支
Cron表达式 #
text
┌───────────── 分钟 (0 - 59)
│ ┌───────────── 小时 (0 - 23)
│ │ ┌───────────── 日 (1 - 31)
│ │ │ ┌───────────── 月 (1 - 12)
│ │ │ │ ┌───────────── 星期 (0 - 6, 0=周日)
│ │ │ │ │
* * * * *
常用Cron示例 #
| 表达式 | 说明 |
|---|---|
0 * * * * |
每小时执行 |
0 0 * * * |
每天午夜执行 |
0 0 * * 1 |
每周一执行 |
0 0 1 * * |
每月1日执行 |
0 9 * * 1-5 |
工作日早上9点执行 |
*/15 * * * * |
每15分钟执行 |
定时流水线配置 #
yaml
nightly_build:
stage: build
script:
- npm run build
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
daily_test:
stage: test
script:
- npm test
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
定时流水线变量 #
yaml
variables:
SCHEDULE_TYPE: "nightly"
nightly_job:
script:
- echo "Running nightly build..."
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
variables:
RUN_FULL_TESTS: "true"
五、手动流水线(Manual Pipeline) #
通过API触发 #
bash
curl --request POST \
--form token=TOKEN \
--form ref=main \
--form "variables[DEPLOY_ENV]=staging" \
https://gitlab.example.com/api/v4/projects/ID/trigger/pipeline
通过界面触发 #
- 进入CI/CD → Pipelines
- 点击"Run pipeline"
- 选择分支
- 添加变量(可选)
- 点击"Run pipeline"
手动触发配置 #
yaml
deploy_production:
stage: deploy
script:
- echo "Deploying to production..."
when: manual
environment:
name: production
url: https://example.com
手动Job配置 #
yaml
deploy:
stage: deploy
script:
- ./deploy.sh
when: manual
allow_failure: false
environment:
name: production
六、Webhook触发流水线 #
配置Webhook #
- 进入项目Settings → Webhooks
- 添加Webhook URL
- 选择触发事件
- 保存
Webhook触发示例 #
yaml
trigger_pipeline:
stage: deploy
script:
- echo "Triggered by webhook"
rules:
- if: $CI_PIPELINE_SOURCE == "external"
七、父子流水线(Parent-Child Pipeline) #
基本概念 #
父流水线可以触发子流水线,实现流水线的模块化和复用。
yaml
stages:
- build
- test
build:
stage: build
script:
- npm run build
child_pipeline:
stage: test
trigger:
include: child-pipeline.yml
strategy: depend
子流水线配置 #
yaml
stages:
- test
unit_test:
stage: test
script:
- npm run test:unit
integration_test:
stage: test
script:
- npm run test:integration
动态子流水线 #
yaml
generate-config:
stage: build
script: ./generate-config.sh
artifacts:
paths:
- generated-config.yml
child-pipeline:
stage: test
trigger:
include:
- artifact: generated-config.yml
job: generate-config
八、多项目流水线(Multi-project Pipeline) #
基本概念 #
触发其他项目的流水线,实现跨项目协作。
yaml
stages:
- build
- trigger
build:
stage: build
script:
- npm run build
trigger_downstream:
stage: trigger
trigger:
project: my-group/my-project
branch: main
strategy: depend
传递变量 #
yaml
trigger_downstream:
stage: trigger
trigger:
project: my-group/my-project
branch: main
variables:
UPSTREAM_COMMIT: $CI_COMMIT_SHA
DEPLOY_ENV: staging
九、Pipeline来源变量 #
CI_PIPELINE_SOURCE值 #
| 值 | 说明 |
|---|---|
push |
Git push触发 |
web |
Web界面触发 |
trigger |
API触发 |
schedule |
定时触发 |
api |
API调用触发 |
external |
外部CI服务触发 |
pipeline |
多项目流水线触发 |
parent_pipeline |
父流水线触发 |
merge_request_event |
MR事件触发 |
external_pull_request_event |
外部PR事件触发 |
使用示例 #
yaml
job_name:
script: echo "Hello"
rules:
- if: $CI_PIPELINE_SOURCE == "push"
when: always
- if: $CI_PIPELINE_SOURCE == "schedule"
when: always
- when: never
十、workflow控制 #
控制Pipeline创建 #
yaml
workflow:
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: always
- if: $CI_COMMIT_BRANCH == "develop"
when: always
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: always
- when: never
合并MR和分支流水线 #
yaml
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_COMMIT_BRANCH == "develop"
避免重复流水线 #
yaml
workflow:
rules:
- if: $CI_COMMIT_BRANCH && $CI_OPEN_MERGE_REQUESTS && $CI_PIPELINE_SOURCE == "push"
when: never
- when: always
十一、完整示例 #
多触发源配置 #
yaml
stages:
- lint
- test
- build
- deploy
workflow:
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_COMMIT_BRANCH == "develop"
- if: $CI_PIPELINE_SOURCE == "schedule"
- if: $CI_PIPELINE_SOURCE == "web"
lint:
stage: lint
script:
- npm run lint
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
test:
stage: test
script:
- npm test
rules:
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
- if: $CI_COMMIT_BRANCH == "main"
- if: $CI_COMMIT_BRANCH == "develop"
build:
stage: build
script:
- npm run build
artifacts:
paths:
- dist/
nightly_test:
stage: test
script:
- npm run test:full
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
deploy_staging:
stage: deploy
script:
- echo "Deploying to staging..."
environment:
name: staging
rules:
- if: $CI_COMMIT_BRANCH == "develop"
when: manual
deploy_production:
stage: deploy
script:
- echo "Deploying to production..."
environment:
name: production
rules:
- if: $CI_COMMIT_BRANCH == "main"
when: manual
下一步 #
现在你已经了解了各种Pipeline类型,接下来让我们学习 变量与环境!
最后更新:2026-03-28