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

GitHub Actions自动触发工作流中能否添加中途用户输入步骤?

在PR触发的GitHub Actions工作流中途获取用户输入的方案

GitHub Actions本身没有原生支持让运行中的工作流暂停并等待用户输入的功能,但可以通过以下几种变通方法实现你要的场景:

方法一:拆分工作流,手动触发后续步骤

  1. 第一部分工作流由PR事件自动触发,完成前置自动化操作后,通过PR评论通知用户需要提供输入。
  2. 用户准备好输入内容后,手动通过workflow_dispatch事件触发第二部分工作流,传入输入参数和对应的PR编号。
  3. 第二部分工作流根据参数和指定PR,执行后续自动化操作。

示例代码片段:

# PR触发的前置工作流
name: PR Pre-check
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  pre-check:
    runs-on: ubuntu-latest
    steps:
      - name: 执行前置自动化操作
        run: echo "完成PR触发的自动检查任务"
      - name: 通知用户提供输入
        uses: actions/github-script@v6
        with:
          script: |
            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: "请确认需要执行的操作,然后手动触发「PR后续处理」工作流并传入参数"
            })

# 手动触发的后续工作流
name: PR Post-Input Action
on:
  workflow_dispatch:
    inputs:
      user_choice:
        description: "用户选择的操作类型"
        required: true
        type: choice
        options:
          - 执行测试
          - 合并预发布
      pr_number:
        description: "对应的PR编号"
        required: true

jobs:
  post-input-action:
    runs-on: ubuntu-latest
    steps:
      - name: 根据用户选择执行操作
        run: echo "针对PR #${{ github.event.inputs.pr_number }},执行用户选择的操作:${{ github.event.inputs.user_choice }}"

方法二:使用自定义Action等待用户评论输入

借助社区维护的自定义Action(比如action-wait-for-comment),让工作流暂停直到用户在PR下发布特定格式的评论作为输入:

  1. 在PR触发的工作流中添加调用该Action的步骤,配置等待的评论规则(比如特定关键词)。
  2. 工作流会进入等待状态,直到用户在PR下发表符合规则的评论,Action会提取评论内容作为输入参数。
  3. 后续步骤使用该参数执行自动化操作。

示例代码片段:

name: PR with User Input
on:
  pull_request:
    types: [opened, synchronize]

jobs:
  wait-for-user-input:
    runs-on: ubuntu-latest
    steps:
      - name: 等待用户输入评论
        id: get-input
        uses: lewagon/wait-for-comment@v1
        with:
          issue-number: ${{ github.event.pull_request.number }}
          keyword: "确认操作:"
          secret: ${{ secrets.GITHUB_TOKEN }}
      
      - name: 处理用户输入
        run: echo "用户确认的操作内容:${{ steps.get-input.outputs.comment }}"

注意事项

  • 使用等待类Action时,工作流会持续占用Runner资源,建议通过timeout-minutes配置合理的超时时间,避免资源浪费。
  • 如果输入涉及敏感信息,不要用明文评论传递,建议结合GitHub Secrets或加密方式处理。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 22:30:49