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

能否在GitHub Action的post-if条件中引用workflow_dispatch输入事件?

问题解答

首先明确:post-if无法直接访问github上下文或事件输入,这就是你报错的原因——GitHub Actions的post-if上下文和常规步骤的if不同,它仅支持有限的上下文变量(比如job、steps、runner、secrets),github上下文不在允许的列表里,所以会提示无法识别github这个命名变量。

要实现你想要的效果,得通过将事件输入传递为作业输出的方式间接实现,具体步骤如下:

  1. 在主作业中添加步骤,把github.event.inputs.cache的值保存为作业级输出:

    jobs:
      your-main-job:
        runs-on: ubuntu-latest
        steps:
          # 其他步骤...
          - name: 传递cache参数到post步骤
            id: cache-flag
            run: echo "cache=${{ github.event.inputs.cache }}" >> $GITHUB_OUTPUT
    
  2. 在post步骤的post-if中引用这个作业输出:

    jobs:
      your-main-job:
        # 主作业配置...
        post:
          if: ${{ steps.cache-flag.outputs.cache == 'true' }}
          runs-on: ubuntu-latest
          steps:
            - name: 仅当cache为true时执行的post步骤
              run: echo "Cache flag is true, running post step"
    

注意:workflow_dispatch的布尔输入在GitHub Actions中会被解析为字符串'true'或'false',所以判断时要和字符串值对比,而非直接使用布尔值。

内容的提问来源于stack exchange,提问作者Paul Razvan Berg

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 13:55:21