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

如何在GitHub Actions中针对指定分支PR手动运行工作流任务?

问题

在GitLab和Bitbucket中可以给流水线添加manual手动步骤,请问GitHub Actions里能不能实现类似操作?我的需求是:

  • 创建针对master、main、develop分支的Pull Request(PR)时,手动运行integration-test任务;
  • 创建针对master、release*、develop、main分支的PR时,自动运行static-code-analysis任务。

我给integration-test任务加了如下if条件:

if: ${{ github.event_name == 'workflow_dispatch' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop') }}

完整工作流配置如下:

#ci.yml
name: Pull Request

on:
  pull_request:
    branches:
      - master
      - release*
      - develop
      - main
  workflow_dispatch:

jobs:
  static-code-analysis:
    runs-on: macos-latest
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
    steps:    
      - name: Checkout code
        id: Checkout-code
        uses: actions/checkout@v3

      - name: Notify Slack
        uses: act10ns/slack@v2
        with:
          channel: 's1-panel'
          status: ${{ job.status }}
          steps: ${{ toJson(steps) }}
        if: always()

  integration-test:
    if: ${{ github.event_name == 'workflow_dispatch' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop') }}
    runs-on: macos-latest
    permissions: write-all
    env:
      SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
    steps:
      - name: Checkout code
        id: Checkout-code
        uses: actions/checkout@v3

      - name: Notify Slack 
        uses: act10ns/slack@v2
        with:
          channel: 's1-panel'
          status: ${{ job.status }}
          steps: ${{ toJson(steps) }}
        if: always()

但创建从testing分支到master分支的PR时,workflow启动后,integration-test任务显示「This job was skipped」,该怎么解决?

解决方案

你当前的问题核心是:PR触发的workflow中github.event_name为pull_request,但你的integration-test任务仅允许workflow_dispatch触发,因此PR场景下会直接跳过任务。要实现PR中手动触发该任务,推荐以下两种方案:

方案一:用环境审批实现PR内手动运行(最贴合需求)

  1. 在仓库「Settings」→「Environments」中创建一个环境(比如命名为integration-test-approval),开启「Wait for approval before deploying」选项(无需评审者的话可留空,但必须启用等待审批)。
  2. 修改integration-test任务的配置,关联环境并调整触发条件:
integration-test:
  # 允许PR符合分支条件时触发,同时保留workflow_dispatch手动触发能力
  if: ${{ (github.event_name == 'pull_request' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop')) || github.event_name == 'workflow_dispatch' }}
  runs-on: macos-latest
  environment: integration-test-approval # 关联创建的审批环境
  permissions: write-all
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
  steps:
    - name: Checkout code
      id: Checkout-code
      uses: actions/checkout@v3

    - name: Notify Slack 
      uses: act10ns/slack@v2
      with:
        channel: 's1-panel'
        status: ${{ job.status }}
        steps: ${{ toJson(steps) }}
      if: always()

配置后,符合分支条件的PR触发workflow时,integration-test任务会显示「Waiting」状态,你只需在PR的workflow界面点击「Approve」即可手动启动任务。

方案二:通过workflow_dispatch手动触发并指定PR

若不想使用环境审批,可通过传入PR编号参数的方式,手动触发任务针对指定PR运行:

  1. 修改workflow触发配置,添加PR编号输入参数:
on:
  pull_request:
    branches:
      - master
      - release*
      - develop
      - main
  workflow_dispatch:
    inputs:
      pr_number:
        description: '需要运行测试的PR编号'
        required: false
  1. 调整integration-test的条件和代码检出逻辑:
integration-test:
  if: ${{ (github.event_name == 'pull_request' && (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop')) || (github.event_name == 'workflow_dispatch' && (github.event.inputs.pr_number != '' || (github.base_ref == 'main' || github.base_ref == 'master' || github.base_ref == 'develop'))) }}
  runs-on: macos-latest
  permissions: write-all
  env:
    SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
  steps:
    - name: 检出指定PR代码
      if: ${{ github.event.inputs.pr_number != '' }}
      uses: actions/checkout@v3
      with:
        ref: refs/pull/${{ github.event.inputs.pr_number }}/head
    - name: 检出默认分支代码
      if: ${{ github.event.inputs.pr_number == '' }}
      uses: actions/checkout@v3

    - name: Notify Slack 
      uses: act10ns/slack@v2
      with:
        channel: 's1-panel'
        status: ${{ job.status }}
        steps: ${{ toJson(steps) }}
      if: always()

这种方式下,PR创建后你需手动触发workflow并输入对应PR编号,即可启动integration-test任务。

关键注意点

PR触发的workflow中,github.event_name固定为pull_request,而非workflow_dispatch,这是你原配置导致任务被跳过的根本原因。方案一更适配你的需求,无需额外输入参数,操作更便捷。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.21 01:12:17