feat(requires): support variable references in enum constraints (#2678)

This commit is contained in:
Valentin Maerten
2026-03-21 11:32:02 +01:00
committed by GitHub
parent 19d8fae5f9
commit 8b6aca5722
16 changed files with 268 additions and 16 deletions

View File

@@ -1233,6 +1233,71 @@ This is supported only for string variables.
:::
### Using variable references for enum values
Instead of hardcoding enum values, you can reference a variable containing the
allowed values. This is useful when you want to define allowed values once and
reuse them, or when the values are computed dynamically.
Use the `ref` key to reference a variable:
```yaml
version: '3'
vars:
ALLOWED_ENVS: [dev, staging, prod]
tasks:
deploy:
requires:
vars:
- name: ENV
enum:
ref: .ALLOWED_ENVS
cmds:
- echo "Deploying to {{.ENV}}"
```
You can also use template expressions to transform the value:
```yaml
version: '3'
vars:
CONFIG:
sh: cat config.json
tasks:
deploy:
requires:
vars:
- name: ENV
enum:
ref: ( .CONFIG | fromJson ).allowed_environments
cmds:
- echo "Deploying to {{.ENV}}"
```
Or generate values dynamically from a shell command:
```yaml
version: '3'
vars:
AVAILABLE_SERVICES:
sh: ls services/
tasks:
deploy:
requires:
vars:
- name: SERVICE
enum:
ref: .AVAILABLE_SERVICES | splitLines | compact
cmds:
- echo "Deploying {{.SERVICE}}"
```
### Prompting for missing variables interactively
If you want Task to prompt users for missing required variables instead of

View File

@@ -674,14 +674,12 @@ tasks:
```yaml
tasks:
# Simple requirements
deploy:
requires:
vars: [API_KEY, ENVIRONMENT]
cmds:
- ./deploy.sh
# Requirements with enum validation
advanced-deploy:
requires:
vars:
@@ -693,6 +691,17 @@ tasks:
cmds:
- echo "Deploying to {{.ENVIRONMENT}} with log level {{.LOG_LEVEL}}"
- ./deploy.sh
# Requirements with enum from variable reference
reusable-deploy:
requires:
vars:
- name: ENVIRONMENT
enum:
ref: .ALLOWED_ENVS
cmds:
- ./deploy.sh
```
See [Prompting for missing variables interactively](/docs/guide#prompting-for-missing-variables-interactively)