并行执行可复用工作流及状态报告:获取作业状态遇阻
解决GitHub Actions多租户部署状态收集问题
需求
- 为每个租户执行部署
- 等待每个租户的部署完成
- 收集每个部署作业的状态,用于向其他系统发送通知
我知道fail-fast: false可以让部署矩阵在某个租户部署出错时继续执行其他租户的部署,但现在没办法获取到可复用工作流中每个作业的成功/失败状态来做日志记录。
用户当前的工作流代码:
# 触发部署的主工作流 on: pull_request: types: [closed] branches: [main] jobs: deploy: strategy: fail-fast: false matrix: tenant: ["tenant1", "tenant2"] uses: ./.github/workflows/reusable-workflow.yml with: aws_env: 'staging' tenant: ${{ matrix.tenant }} secrets: inherit post_deploy: needs: deploy steps: #... 这里需要处理deploy的执行结果
# 可复用工作流.yml # 这个工作流被另一个GitHub Action按租户矩阵调用 on: workflow_call: inputs: tenant: description: '租户名称' type: string default: 'test' aws_env: description: 'AWS环境' type: string default: 'staging' jobs: deploy_1: uses: ./.github/workflows/docker.yml name: 部署API服务 with: aws_env: ${{ inputs.aws_env }} module: "api" tenant: ${{ inputs.tenant }} deploy_2: uses: ./.github/workflows/docker.yml name: 部署其他服务 with: aws_env: ${{ inputs.aws_env }} module: "other_service" tenant: ${{ inputs.tenant }}
解决方案:添加状态输出与结果收集
1. 修改可复用工作流,新增状态汇总输出
在可复用工作流中添加一个作业,汇总当前租户的所有部署任务状态,并输出为工作流变量:
# 可复用工作流.yml(修改后) on: workflow_call: inputs: tenant: description: '租户名称' type: string default: 'test' aws_env: description: 'AWS环境' type: string default: 'staging' outputs: deployment_status: description: '当前租户的整体部署状态' value: ${{ jobs.check_deployment_status.outputs.status }} jobs: deploy_1: uses: ./.github/workflows/docker.yml name: 部署API服务 with: aws_env: ${{ inputs.aws_env }} module: "api" tenant: ${{ inputs.tenant }} deploy_2: uses: ./.github/workflows/docker.yml name: 部署其他服务 with: aws_env: ${{ inputs.aws_env }} module: "other_service" tenant: ${{ inputs.tenant }} check_deployment_status: name: 汇总部署状态 needs: [deploy_1, deploy_2] runs-on: ubuntu-latest outputs: status: ${{ steps.set_status.outputs.status }} steps: - name: 设置部署状态 id: set_status run: | if [[ "${{ needs.deploy_1.result }}" == "success" && "${{ needs.deploy_2.result }}" == "success" ]]; then echo "status=success" >> $GITHUB_OUTPUT else echo "status=failure" >> $GITHUB_OUTPUT fi
2. 更新主工作流,收集并处理每个租户的状态
在post_deploy作业中,通过needs.deploy.results获取所有矩阵实例的部署结果,遍历处理每个租户的状态:
# 触发部署的主工作流(修改后) on: pull_request: types: [closed] branches: [main] jobs: deploy: strategy: fail-fast: false matrix: tenant: ["tenant1", "tenant2"] uses: ./.github/workflows/reusable-workflow.yml with: aws_env: 'staging' tenant: ${{ matrix.tenant }} secrets: inherit post_deploy: needs: deploy runs-on: ubuntu-latest steps: - name: 遍历所有租户的部署状态 run: | echo "=== 所有租户部署结果 ===" # 解析JSON格式的结果并输出 ${{ toJSON(needs.deploy.results) }} | jq -r '.[] | "租户: \(.matrix.tenant) | 状态: \(.outputs.deployment_status)"' - name: 根据状态发送通知 run: | # 遍历每个租户的结果,执行通知逻辑 for result in $(echo '${{ toJSON(needs.deploy.results) }}' | jq -c '.[]'); do tenant=$(echo $result | jq -r '.matrix.tenant') status=$(echo $result | jq -r '.outputs.deployment_status') echo "准备向外部系统发送通知:租户 $tenant 部署状态为 $status" # 这里替换为实际的通知接口调用,示例: # curl -X POST https://your-notification-service.com -d '{"tenant":"'$tenant'","status":"'$status'"}' done
关键说明
- 状态汇总:通过新增的
check_deployment_status作业,判断当前租户的所有部署任务是否全部成功,输出统一的状态标识。 - 结果获取:
needs.deploy.results包含了矩阵中每个租户的部署详情,包括租户名称和可复用工作流输出的状态。 - 灵活处理:使用
jq工具解析JSON结果,可轻松扩展逻辑,比如针对失败状态单独处理、批量发送通知等。
内容的提问来源于stack exchange,提问作者Flavadave
相关产品推荐
相关产品推荐

