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

GitHub Actions:如何配置通知作业根据依赖测试结果执行对应步骤?

解决GitHub Actions中通知作业判断依赖结果的问题

你遇到的问题核心是needs.*.result的写法无法正确处理矩阵作业的结果集合,导致判断逻辑失效。以下是具体原因和修正方案:

问题根源

needs是一个包含依赖作业结果的对象:

  • 单个作业(如run-api-tests)的result是字符串值(success/failure/cancelled)
  • 矩阵作业(如run-e2e-tests)的result是数组,包含每个矩阵实例的结果
    直接使用contains(needs.*.result, 'failure')无法同时处理这两种结构,导致判断逻辑出错。

修正方案1:直接针对作业类型判断(简洁版)

针对单个作业和矩阵作业分别处理结果:

run-api-tests:
 ...
run-e2e-tests:
 ...
notification:
  needs: [run-api-tests, run-e2e-tests]
  runs-on: ubuntu-latest
  steps:
    - name: Slack Notify Failure
      uses: slackapi/slack-github-action@v1.25.0
      if: always() && (
        needs.run-api-tests.result == 'failure' || 
        needs.run-api-tests.result == 'cancelled' ||
        some(needs.run-e2e-tests.*.result, 'failure') ||
        some(needs.run-e2e-tests.*.result, 'cancelled')
      )
      # 此处填写失败通知的配置参数
    - name: Slack Notify Success
      uses: slackapi/slack-github-action@v1.25.0
      if: always() && (
        needs.run-api-tests.result == 'success' &&
        every(needs.run-e2e-tests.*.result, 'success')
      )
      # 此处填写成功通知的配置参数
  • 用some函数检查矩阵作业中是否存在任意失败/取消的实例
  • 用every函数确保矩阵作业所有实例都成功,同时单个作业也成功

修正方案2:通用结果收集(灵活版)

适合依赖作业数量多或有多个矩阵的场景,先收集所有结果再判断:

run-api-tests:
 ...
run-e2e-tests:
 ...
notification:
  needs: [run-api-tests, run-e2e-tests]
  runs-on: ubuntu-latest
  steps:
    - name: 收集测试结果并判断状态
      id: test-status
      run: |
        # 初始化结果数组
        results=()
        # 添加单个作业结果
        results+=("${{ needs.run-api-tests.result }}")
        # 添加矩阵作业的所有实例结果
        for res in $(echo '${{ toJSON(needs.run-e2e-tests.*.result) }}' | jq -r '.[]'); do
          results+=("$res")
        done
        # 判断是否存在失败或取消状态
        if [[ "${results[*]}" =~ "failure" || "${results[*]}" =~ "cancelled" ]]; then
          echo "status=failure" >> $GITHUB_OUTPUT
        else
          echo "status=success" >> $GITHUB_OUTPUT
        fi
    - name: Slack Notify Failure
      uses: slackapi/slack-github-action@v1.25.0
      if: always() && steps.test-status.outputs.status == 'failure'
      # 失败通知配置
    - name: Slack Notify Success
      uses: slackapi/slack-github-action@v1.25.0
      if: always() && steps.test-status.outputs.status == 'success'
      # 成功通知配置

内容的提问来源于stack exchange,提问作者Steve

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.28 09:05:16