You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

能否在GitHub Action的action.yml及工作流文件中使用枚举类型输入?如何在action.yml中强制枚举输入并校验调用该Action的工作流YAML文件有效性?

GitHub Actions: Using Enum-like Input Constraints in action.yml and Workflows

Great question! Let's break this down clearly for your scenario:

First, the direct answers to your two questions:

  1. GitHub Actions doesn't natively support an explicit enum type for inputs in either action.yml or workflow files. However, you can achieve enum-like constraints using built-in configuration options to restrict valid values and validate inputs.
  2. Yes, you can add validation directly in your action.yml to enforce allowed values for my-enum-input, even before your script runs. Here's how:

1. Add Enum Constraints to Your Custom Action's action.yml

You have two complementary ways to implement this:

Option 1: Use options for UI Guidance

Add an options array to your input definition. This turns the parameter into a dropdown in GitHub's workflow visual editor, guiding users to select only valid values:

inputs:
  my-enum-input:
    description: "This is an input that's an enum"
    default: "all"
    required: true
    options:
      - "all"
      - "a"
      - "b"

⚠️ Note: This is only a UI guardrail. If someone manually edits the workflow YAML to pass an invalid value, GitHub won't block it by itself.

Option 2: Add pattern + errorMessage for Enforced Validation

To force validation of input values (and fail early if an invalid value is passed), use a regex pattern that matches your allowed enum values, plus an errorMessage to clarify valid options:

inputs:
  my-enum-input:
    description: "This is an input that's an enum"
    default: "all"
    required: true
    options:
      - "all"
      - "a"
      - "b"
    pattern: ^(all|a|b)$
    errorMessage: "Invalid value for my-enum-input. Must be one of: all, a, b"

With this setup, GitHub will throw an error and stop the workflow before your script runs if an invalid value is provided—no need to rely solely on script-level checks.

2. Enum Constraints in Workflow Files

If you're defining inputs directly in a workflow (e.g., for workflow_dispatch triggers), you can use the same options + pattern combo to enforce enum-like behavior:

name: My Example Workflow
on:
  workflow_dispatch:
    inputs:
      workflow-enum-input:
        description: "Workflow-level enum input"
        default: "all"
        required: true
        options:
          - "all"
          - "a"
          - "b"
        pattern: ^(all|a|b)$
        errorMessage: "Invalid value. Must be one of: all, a, b"

Bonus: Combine with Script Validation

You can still keep your existing script-level validation as a fallback (or to add logic tied to specific enum values) — this creates a double layer of protection against invalid inputs.

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

火山引擎 最新活动