GitHub Actions:workflow_dispatch复选框控制作业执行失败问题
问题原因分析
- 触发源不匹配导致条件失效:当通过
push到release分支触发工作流时,github.event.inputs对象不存在,github.event.inputs.graphics为undefined,你的条件== true || == false无法匹配,作业直接跳过。 - 布尔输入的类型问题:GitHub Actions中
workflow_dispatch的布尔类型输入会被解析为字符串("true"或"false"),直接用== true做比较会不匹配,导致条件判断错误。
解决方案
根据你的需求,分两种场景调整作业的if条件:
场景1:仅手动触发时根据复选框控制作业执行
只在手动触发工作流且勾选了graphics复选框时,执行graphics作业:
workflow_dispatch: inputs: graphics: type: boolean required: false default: false tags: description: 'trigger manually' push: branches: - "release" jobs: graphics: if: ${{ github.event_name == 'workflow_dispatch' && github.event.inputs.graphics == 'true' }} name: call graphicstransform deploy uses: ./.github/workflows/deploygraphics.yml secrets: inherit with: external: 'true'
场景2:push到release分支时强制执行,手动触发时根据复选框控制
如果需要push到release分支时自动执行该作业,同时手动触发时由复选框决定:
workflow_dispatch: inputs: graphics: type: boolean required: false default: false tags: description: 'trigger manually' push: branches: - "release" jobs: graphics: if: ${{ github.event_name == 'push' || (github.event_name == 'workflow_dispatch' && github.event.inputs.graphics == 'true') }} name: call graphicstransform deploy uses: ./.github/workflows/deploygraphics.yml secrets: inherit with: external: 'true'
关键说明
- 必须通过
github.event_name区分触发源,避免非手动触发时inputs变量不存在导致的条件失效。 - 布尔类型的输入值要以字符串形式比较(
'true'/'false'),而非直接用布尔值true/false。
内容的提问来源于stack exchange,提问作者Serve Laurijssen
相关产品推荐
相关产品推荐

