You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置GitHub Actions依赖作业在条件作业跳过或运行时正确执行

解决GitHub Actions依赖作业在条件作业跳过时的执行问题

要实现条件作业运行时等待其完成后执行,条件作业被跳过时立即执行的需求,需要调整依赖作业的if条件,覆盖默认的“依赖作业随条件作业跳过”的行为。

修改后的完整配置

name: Test conditionals

on:
  workflow_dispatch:

env:
  DEPLOY_NONPROD_ENV: 'false'

jobs:
  conditionalJob: 
    if: ${{ env.DEPLOY_NONPROD_ENV == 'true' }}
    runs-on: 'ubuntu-latest'
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Echo message
        run: 'echo "Condition evaluated to true"'
  dependantJob: 
    needs: [conditionalJob]
    # 允许依赖作业在条件作业被跳过,或运行完成(成功/失败)时执行
    if: ${{ needs.conditionalJob.result == 'skipped' || always() }}
    runs-on: 'ubuntu-latest'
    steps:
      - name: Checkout repository
        uses: actions/checkout@v3
      - name: Echo message
        run: 'echo "Running dependantJob"'

关键配置说明

  • needs: [conditionalJob]:保持依赖关系,确保当条件作业实际运行时,依赖作业会等待它完成后再执行。
  • if条件逻辑:
    • needs.conditionalJob.result == 'skipped':明确允许条件作业被跳过时,依赖作业直接执行。
    • always():确保条件作业运行完成后(无论成功还是失败),依赖作业都会执行。如果只希望条件作业成功时才执行依赖作业,可以将always()替换为success()。

可选的精确条件

如果需要排除“条件作业被取消”的场景(即仅在条件作业跳过/成功/失败时执行依赖作业),可以使用更精确的条件:

if: ${{ !cancelled() && (needs.conditionalJob.result == 'skipped' || needs.conditionalJob.result == 'success' || needs.conditionalJob.result == 'failure') }}

内容的提问来源于stack exchange,提问作者Michał Turczyn

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:53:11