Skip to content

Commit 8b9574f

Browse files
committed
build: add variable validation in bake
Signed-off-by: David Karlsson <[email protected]>
1 parent adab458 commit 8b9574f

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,50 @@ $ 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, the `PORT` variable must be set to 1024 or a higher
88+
value.
89+
90+
```hcl
91+
variable "PORT" {
92+
default = 3000
93+
validation {
94+
condition = PORT >= 1024
95+
error_message = "PORT must be 1024 or higher"
96+
}
97+
}
98+
```
99+
100+
If the `condition` expression evaluates to `false`, the variable value is
101+
considered invalid, whereby the build invocation fails and `error_message` is
102+
emitted.
103+
104+
Values are coerced into the expected type before the validation is set. This
105+
ensures that any overrides set with environment variables work as expected.
106+
107+
### Validate multiple conditions
108+
109+
To evaluate more than one condition, define multiple `validation` blocks for
110+
the variable. All conditions must be `true`.
111+
112+
```hcl
113+
variable "VAR" {
114+
validation {
115+
condition = VAR != ""
116+
error_message = "VAR must not be empty"
117+
}
118+
validation {
119+
condition = sanitize(VAR) == VAR
120+
error_message = "VAR can only contain [a-zA-Z0-9-_]"
121+
}
122+
}
123+
```
124+
81125
## Escape variable interpolation
82126

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

0 commit comments

Comments
 (0)