Skip to content

Commit 300e985

Browse files
authored
Merge pull request #21508 from dvdksn/bake-variable-validation
build: add variable validation in bake
2 parents bee0c7b + 1769e0d commit 300e985

File tree

2 files changed

+125
-17
lines changed

2 files changed

+125
-17
lines changed

content/manuals/build/bake/overrides.md

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -288,43 +288,63 @@ which is the short commit hash generated by `git rev-parse --short HEAD`.
288288
Overriding non-string variables with environment variables is supported. Values
289289
passed as environment variables are coerced into suitable types first.
290290

291-
The following example defines a `PORT` variable with a default value of `8080`.
292-
The `default` target uses a [ternary operator](expressions.md#ternary-operators)
293-
to set the `PORT` variable to the value of the environment variable `PORT`
294-
if it is greater than `1024`, otherwise it uses the default value.
295-
296-
In this case, the `PORT` variable is coerced to an integer before the ternary
297-
operator is evaluated.
291+
The following example defines a `PORT` variable. The `backend` target uses the
292+
`PORT` variable as-is, and the `frontend` target uses the value of `PORT`
293+
incremented by one.
298294

299295
```hcl
300-
default_port = 8080
301-
302296
variable "PORT" {
303-
default = default_port
297+
default = 3000
304298
}
305299
306-
target "default" {
300+
group "default" {
301+
targets = ["backend", "frontend"]
302+
}
303+
304+
target "backend" {
307305
args = {
308-
PORT = PORT > 1024 ? PORT : default_port
306+
PORT = PORT
307+
}
308+
}
309+
310+
target "frontend" {
311+
args = {
312+
PORT = add(PORT, 1)
309313
}
310314
}
311315
```
312316

313-
Attempting to set the `PORT` variable with a value less than `1024` will result
314-
in the default value being used.
317+
Overriding `PORT` using an environment variable will first coerce the value
318+
into the expected type, an integer, before the expression in the `frontend`
319+
target runs.
315320

316321
```console
317-
$ PORT=80 docker buildx bake --print
322+
$ PORT=7070 docker buildx bake --print
318323
```
319324

320325
```json
321326
{
322-
"target": {
327+
"group": {
323328
"default": {
329+
"targets": [
330+
"backend",
331+
"frontend"
332+
]
333+
}
334+
},
335+
"target": {
336+
"backend": {
337+
"context": ".",
338+
"dockerfile": "Dockerfile",
339+
"args": {
340+
"PORT": "7070"
341+
}
342+
},
343+
"frontend": {
324344
"context": ".",
325345
"dockerfile": "Dockerfile",
326346
"args": {
327-
"PORT": "8080"
347+
"PORT": "7071"
328348
}
329349
}
330350
}

content/manuals/build/bake/variables.md

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,94 @@ $ docker buildx bake --print
7878
}
7979
```
8080

81+
## Validating variables
82+
83+
To verify that the value of a variable conforms to an expected type, value
84+
range, or other condition, you can define custom validation rules using the
85+
`validation` block.
86+
87+
In the following example, validation is used to enforce a numeric constraint on
88+
a variable value; the `PORT` variable must be 1024 or higher.
89+
90+
```hcl
91+
# Define a variable `PORT` with a default value and a validation rule
92+
variable "PORT" {
93+
default = 3000 # Default value assigned to `PORT`
94+
95+
# Validation block to ensure `PORT` is a valid number within the acceptable range
96+
validation {
97+
condition = PORT >= 1024 # Ensure `PORT` is at least 1024
98+
error_message = "The variable 'PORT' must be 1024 or higher." # Error message for invalid values
99+
}
100+
}
101+
```
102+
103+
If the `condition` expression evaluates to `false`, the variable value is
104+
considered invalid, whereby the build invocation fails and `error_message` is
105+
emitted. For example, if `PORT=443`, the condition evaluates to `false`, and
106+
the error is raised.
107+
108+
Values are coerced into the expected type before the validation is set. This
109+
ensures that any overrides set with environment variables work as expected.
110+
111+
### Validate multiple conditions
112+
113+
To evaluate more than one condition, define multiple `validation` blocks for
114+
the variable. All conditions must be `true`.
115+
116+
Here’s an example:
117+
118+
```hcl
119+
# Define a variable `VAR` with multiple validation rules
120+
variable "VAR" {
121+
# First validation block: Ensure the variable is not empty
122+
validation {
123+
condition = VAR != ""
124+
error_message = "The variable 'VAR' must not be empty."
125+
}
126+
127+
# Second validation block: Ensure the value contains only alphanumeric characters
128+
validation {
129+
# VAR and the regex match must be identical:
130+
condition = VAR == regex("[a-zA-Z0-9]+", VAR)
131+
error_message = "The variable 'VAR' can only contain letters and numbers."
132+
}
133+
}
134+
```
135+
136+
This example enforces:
137+
138+
- The variable must not be empty.
139+
- The variable must match a specific character set.
140+
141+
For invalid inputs like `VAR="hello@world"`, the validation would fail.
142+
143+
### Validating variable dependencies
144+
145+
You can reference other Bake variables in your condition expression, enabling
146+
validations that enforce dependencies between variables. This ensures that
147+
dependent variables are set correctly before proceeding.
148+
149+
Here’s an example:
150+
151+
```hcl
152+
# Define a variable `FOO`
153+
variable "FOO" {}
154+
155+
# Define a variable `BAR` with a validation rule that references `FOO`
156+
variable "BAR" {
157+
# Validation block to ensure `FOO` is set if `BAR` is used
158+
validation {
159+
condition = FOO != "" # Check if `FOO` is not an empty string
160+
error_message = "The variable 'BAR' requires 'FOO' to be set."
161+
}
162+
}
163+
```
164+
165+
This configuration ensures that the `BAR` variable can only be used if `FOO`
166+
has been assigned a non-empty value. Attempting to build without setting `FOO`
167+
will trigger the validation error.
168+
81169
## Escape variable interpolation
82170

83171
If you want to bypass variable interpolation when parsing the Bake definition,

0 commit comments

Comments
 (0)