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

如何根据PR依赖升级标志条件触发对应GitHub Actions工作流

解决方案:基于PR标签/标题关键词区分触发工作流

核心思路是通过PR的标签或标题关键词作为判断依据,让两个工作流分别在符合条件时触发,以下是两种常用实现方案:


方案一:基于PR标签判断

给需要执行依赖升级的PR打上特定标签(比如dependency-upgrade),然后修改两个工作流的触发条件:

修改后的workflow1(仅带指定标签时运行)

name: workflow1
on:
  pull_request:
    branches:
      - staging
    types: [opened, synchronize, reopened]
    labels:
      - dependency-upgrade

jobs:
  plan:
    name: "Terragrunt Plan (Dependency Upgrade)"
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: ${{ env.TERRAFORM_WORKING_DIR }}
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v2

      - name: Terragrunt Init
        id: init
        run: terragrunt init -upgrade  # 注意:原代码的-ugrade是拼写错误,已修正

修改后的workflow2(不带指定标签时运行)

name: workflow2
on:
  pull_request:
    branches:
      - staging
    types: [opened, synchronize, reopened]
    labels:
      - '!dependency-upgrade'

jobs:
  plan:
    name: "Terragrunt Plan (Regular)"
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: ${{ env.TERRAFORM_WORKING_DIR }}
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v2

      - name: Terragrunt Init
        id: init
        run: terragrunt init -lockfile=readonly

方案二:基于PR标题关键词判断

如果不想手动打标签,可通过PR标题中的特定关键词(比如[DEP-UPGRADE])判断,无需修改on触发规则,只需在job级别添加if条件:

修改后的workflow1(标题含关键词时运行)

name: workflow1
on:
  pull_request:
    branches:
      - staging

jobs:
  plan:
    if: contains(github.event.pull_request.title, '[DEP-UPGRADE]')
    name: "Terragrunt Plan (Dependency Upgrade)"
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: ${{ env.TERRAFORM_WORKING_DIR }}
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v2

      - name: Terragrunt Init
        id: init
        run: terragrunt init -upgrade  # 修正拼写错误

修改后的workflow2(标题不含关键词时运行)

name: workflow2
on:
  pull_request:
    branches:
      - staging

jobs:
  plan:
    if: !contains(github.event.pull_request.title, '[DEP-UPGRADE]')
    name: "Terragrunt Plan (Regular)"
    runs-on: ubuntu-20.04
    defaults:
      run:
        working-directory: ${{ env.TERRAFORM_WORKING_DIR }}
    steps:
      - name: 'Checkout'
        uses: actions/checkout@v2

      - name: Terragrunt Init
        id: init
        run: terragrunt init -lockfile=readonly

注意事项

  1. 方案一中的types: [opened, synchronize, reopened]确保PR更新、重新打开时也能触发工作流,可根据需求调整。
  2. 两种方案二选一即可:标签方案更直观易管理,标题关键词方案无需手动操作,适合自动化场景。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.15 07:15:42