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

如何在GitHub Action中条件设置步骤输入并使用默认值?

在GitHub Action工作流中条件性传递输入参数并保留Action默认值

你可以通过在表达式中返回null来实现需求——当工作流输入为空时,传递null会让GitHub Actions自动忽略该参数,从而让目标Action使用自身定义的默认值,无需在工作流中重复默认值,也不用复制步骤。

问题根源

你之前用inputs.my-input-dispatch != '' && inputs.my-input-dispatch的写法,当输入为空时表达式会返回false,这个'false'会被当作参数值传递给Action,覆盖了它的默认值;而手动复制默认值又会导致工作流和Action之间的冗余依赖,增加维护成本。

解决方案

使用表达式${{ inputs.my-input-dispatch || null }}(或更明确的${{ if(inputs.my-input-dispatch != '', inputs.my-input-dispatch, null) }}),当my-input-dispatch不为空时传递其值,为空时传递null。GitHub Actions会忽略值为null的参数,让目标Action使用自身定义的默认值。

修改后的工作流代码

name: Test Input for GitHub Action

on: 
  workflow_dispatch:
    inputs:
      my-input-dispatch:
        description: 'Test input'
        required: false
        default: ''

jobs:
  my-test:
    runs-on: toolbox-runner
    steps:
    - uses: actions/checkout@v1
    - name: Test gh action 5
      uses: ./.github/actions/test-input
      with:
        my-input-with-default: ${{ inputs.my-input-dispatch || null }}

验证效果

  • 当你在workflow_dispatch中填写了my-input-dispatch的值时,Action会接收到该值并输出;
  • 当my-input-dispatch为空时,该参数会被忽略,Action会使用自身定义的default: 'default value'。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 15:55:00