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

GitHub Actions:PR中工作流依赖运行及CI徽章正常显示配置

解决GitHub Actions工作流依赖与徽章显示问题

你可以通过以下两种方案实现需求:让Workflow B在PR中跟随A运行,同时保证CI徽章正常显示合格状态。


方案一:让Workflow B独立运行(API触发)

该方案让B作为独立工作流在Actions标签页显示,同时在PR中由A成功后自动触发。

配置Workflow A(HelmLint)

在A的最后添加触发B的步骤,确保仅当A成功后才触发B:

name: HelmLint
on:
  pull_request:
    branches: [ main ]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: 执行Helm Lint检查
        run: helm lint charts/**

  trigger-install-workflow:
    needs: lint
    runs-on: ubuntu-latest
    steps:
      - name: 触发安装Helm Charts工作流
        uses: actions/github-script@v7
        with:
          script: |
            await github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'install-helm-charts.yml', // 替换为你的Workflow B文件名
              ref: context.payload.pull_request.head.ref,
              inputs: {
                pr_number: '${{ github.event.pull_request.number }}'
              }
            })

配置Workflow B(Install Helm Charts)

通过workflow_dispatch允许API触发,并接收PR参数关联对应分支:

name: Install Helm Charts
on:
  workflow_dispatch:
    inputs:
      pr_number:
        description: 关联的PR编号
        required: true
        type: number

jobs:
  install:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        chart: [ chart1, chart2 ] // 替换为你的实际Chart列表
    steps:
      - name: 检出PR分支
        uses: actions/checkout@v4
        with:
          ref: 'refs/pull/${{ github.event.inputs.pr_number }}/head'
      
      - name: 安装${{ matrix.chart }}
        run: helm install ${{ matrix.chart }} ./charts/${{ matrix.chart }} --dry-run

配置徽章

使用Workflow B的工作流状态生成徽章:

![Install Helm Charts 状态](https://github.com/[你的用户名]/[你的仓库]/actions/workflows/install-helm-charts.yml/badge.svg)

分支保护设置

在仓库设置的「分支保护规则」中,将Install Helm Charts添加到「必需状态检查」列表,确保PR仅在B运行成功后可合并。


方案二:保留子工作流调用,改用A的工作流徽章

该方案无需API触发,直接通过工作流调用关联A和B,徽章指向A的工作流(A成功则B必然成功)。

配置Workflow A(HelmLint)

通过workflow_call直接调用B:

name: HelmLint
on:
  pull_request:
    branches: [ main ]

jobs:
  lint:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: 执行Helm Lint检查
        run: helm lint charts/**

  install-charts:
    needs: lint
    uses: ./.github/workflows/install-helm-charts.yml
    with:
      pr_number: ${{ github.event.pull_request.number }}

配置Workflow B(Install Helm Charts)

支持被A调用,并接收PR参数:

name: Install Helm Charts
on:
  workflow_call:
    inputs:
      pr_number:
        required: true
        type: number

jobs:
  install:
    runs-on: ubuntu-latest
    strategy:
      matrix:
        chart: [ chart1, chart2 ] // 替换为你的实际Chart列表
    steps:
      - name: 检出PR分支
        uses: actions/checkout@v4
        with:
          ref: 'refs/pull/${{ inputs.pr_number }}/head'
      
      - name: 安装${{ matrix.chart }}
        run: helm install ${{ matrix.chart }} ./charts/${{ matrix.chart }} --dry-run

配置徽章

使用Workflow A的工作流状态生成徽章:

![HelmLint 状态](https://github.com/[你的用户名]/[你的仓库]/actions/workflows/helm-lint.yml/badge.svg)

分支保护设置

在「分支保护规则」中,将HelmLint和Install Helm Charts的矩阵任务都添加到「必需状态检查」列表。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.07 23:45:34