本文主要阐述如何运用条件表达式(condition)来控制某个作业(Job)的执行与否,并提供若干常见的示例场景。
使用 condition 条件判断语法,支持引用环境变量,根据条件表达式判断是否要执行某个 Job。以下示例中 job2 的 condition 条件引用代码源分支系统环境变量 "${CI_COMMIT_REF_NAME}" == "develop"
,当运行分支为 develop
时执行 job2,否则跳过。示例 YAML 代码及运行效果如下:
sources:
my_repo:
type: gitSample
name: 示例代码源
endpoint: https://atomgit.com/flow-example/spring-boot.git
branch: master
stages:
stage1:
name: 阶段1
jobs:
command_job1:
name: job1
steps:
command_step:
name: 执行命令1
step: Command
with:
run: |
echo This is Job1
stage2:
name: 阶段2
jobs:
command_job2:
name: job2
# 条件判断:运行分支为 develop 时执行该任务
condition: |
"${CI_COMMIT_REF_NAME}" == "develop"
steps:
command_step:
name: 执行命令2
step: Command
with:
run: echo This is Job2
stage3:
name: 阶段3
jobs:
command_job3:
name: job3
steps:
command_step:
name: 执行命令3
step: Command
with:
run: echo This is Job3
command_job4:
name: job4
needs: command_job3
steps:
command_step:
name: 执行命令4
step: Command
with:
run: echo This is Job4
示例场景1:前端应用未更新跳过构建,仅构建后端应用
某些前后端应用依赖场景,先构建前端应用生成静态文件、后端应用构建时引用前端静态文件。但并不是每个需求都涉及前端应用修改,可根据条件判断前端应用是否需要构建。示例中,使用流水线自定义环境变量 "${FRONT_APP_CHANGED}" == "true"
作为任务 condition 条件,变量值为true
时执行前端应用构建,否则跳过。
sources:
my_repo:
type: gitSample
name: 示例代码源
endpoint: https://atomgit.com/flow-example/spring-boot.git
branch: master
stages:
build_stage:
name: 构建
jobs:
front_build_job:
name: 前端应用构建
# 根据自定义环境变量判断是否需要执行前端应用构建
condition: |
"${FRONT_APP_CHANGED}" == "true"
steps:
command_step:
name: 执行命令
step: Command
with:
run: |
echo This is front app build job...
backend_build_job:
name: 后端应用构建
needs: front_build_job
steps:
command_step:
name: 执行命令
step: Command
with:
run: |
echo This is backend app build job...
deploy_stage:
name: 部署
jobs:
deploy_job:
name: 部署任务
steps:
command_step:
name: 执行命令
step: Command
with:
run: echo This is deploy job...
示例场景2:非窗口期发布需要人工审核、窗口期跳过人工审核
生产发布场景,非发布窗口期需要人工审核、窗口期可以跳过人工审核。
sources:
my_repo:
type: gitSample
name: 示例代码源
endpoint: https://atomgit.com/flow-example/spring-boot.git
branch: master
stages:
build_stage:
name: 构建
jobs:
build_job:
name: 构建任务
steps:
command_step:
name: 执行命令
step: Command
with:
run: |
echo This is build job...
approve_stage:
name: 审批
jobs:
approve_job:
name: 人工卡点
# 运行分支为 master 时执行审批任务,请替换为实际的审批判断条件
condition: |
"${CI_COMMIT_REF_NAME}" == "master"
component: ManualValidate
with:
validatorType: users # 验证者类型为企业成员,通过阿里云 ID 确定审核人员
validateMethod: and # 验证方式 and:会签(须所有审批人同意)or:或签(一名审批人同意或拒绝即可)
validators:
- 290591284908846966 #通过阿里云控制台获取阿里云 ID
deploy_stage:
name: 部署
jobs:
deploy_job:
name: 部署任务
steps:
command_step:
name: 执行命令
step: Command
with:
run: echo This is deploy job...
示例场景3:一次构建按需部署到多套环境
研发团队场景有多套测试环境,按需使用。可以根据指定环境名称按需部署到测试环境。
sources:
my_repo:
type: gitSample
name: 示例代码源
endpoint: https://atomgit.com/flow-example/spring-boot.git
branch: master
stages:
build_stage:
name: 构建
jobs:
build_job:
name: 构建任务
steps:
command_step:
name: 执行命令
step: Command
with:
run: |
echo This is build job...
deploy_stage:
name: 部署测试环境
jobs:
deploy_job1:
name: 部署测试环境一套
# 根据指定环境名按需部署
condition: |
"${ENVNAME}" == "EVN1"
steps:
command_step:
name: 执行命令
step: Command
with:
run: echo This is deploy env 1...
deploy_job2:
name: 部署测试环境二套
# 根据指定环境名按需部署
condition: |
"${ENVNAME}" == "EVN2"
steps:
command_step:
name: 执行命令
step: Command
with:
run: echo This is deploy env 2...
文档内容是否对您有帮助?