Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bake: basic variable validation #2794

Merged
merged 1 commit into from
Nov 19, 2024
Merged

Conversation

crazy-max
Copy link
Member

@crazy-max crazy-max commented Nov 18, 2024

fixes #1346

This adds basic validation for variables similar to what terraform does: https://developer.hashicorp.com/terraform/language/values/variables#custom-validation-rules

As follow-up and with work being made in #2758 we could handle additional types to variables and handle some kind of pre-validation if type is not compatible but I think HCL already handles this kind of diag for evaluation already.


variable "FOO" {
  validation {
    condition = FOO != ""
    error_message = "FOO is required."
  }
}

target "default" {
  args = {
    FOO = FOO
  }
}
$ docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 148B / 148B done
#1 DONE 0.0s
docker-bake.hcl:3
--------------------
   1 |     variable "FOO" {
   2 |       validation {
   3 | >>>     condition = FOO != ""
   4 |         error_message = "FOO is required."
   5 |       }
--------------------
ERROR: docker-bake.hcl:3,17-26: Validation failed; FOO is required.
$ FOO=baz docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 148B / 148B done
#1 DONE 0.0s
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "FOO": "baz"
      }
    }
  }
}

Multiple validations case:

variable "FOO" {
  validation {
    condition = FOO != ""
    error_message = "FOO is required."
  }
  validation {
    condition = strlen(FOO) > 4
    error_message = "FOO must be longer than 4 characters."
  }
}

target "default" {
  args = {
    FOO = FOO
  }
}
$ docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 260B / 260B done
#1 DONE 0.0s
docker-bake.hcl:3
--------------------
   1 |     variable "FOO" {
   2 |       validation {
   3 | >>>     condition = FOO != ""
   4 |         error_message = "FOO is required."
   5 |       }
--------------------
ERROR: docker-bake.hcl:3,17-26: Validation failed; FOO is required., and 1 other diagnostic(s)
$ FOO=bar docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 260B / 260B done
#1 DONE 0.0s
docker-bake.hcl:7
--------------------
   5 |       }
   6 |       validation {
   7 | >>>     condition = strlen(FOO) > 4
   8 |         error_message = "FOO must be longer than 4 characters."
   9 |       }
--------------------
ERROR: docker-bake.hcl:7,17-32: Validation failed; FOO must be longer than 4 characters.
FOO=barbar docker buildx bake -f docker-bake.hcl --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 264B / 264B done
#1 DONE 0.0s
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "FOO": "barbar"
      }
    }
  }
}

Dependency on other variables:

variable "FOO" {}
variable "BAR" {
  validation {
    condition = FOO != ""
    error_message = "BAR requires FOO to be set."
  }
}

target "default" {
  args = {
    BAR = BAR
  }
}
$ docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 183B / 183B done
#1 DONE 0.0s
docker-bake.hcl:4
--------------------
   2 |     variable "BAR" {
   3 |       validation {
   4 | >>>     condition = FOO != ""
   5 |         error_message = "BAR requires FOO to be set."
   6 |       }
--------------------
ERROR: docker-bake.hcl:4,17-26: Validation failed; BAR requires FOO to be set.
$ FOO=baz docker buildx bake --print
#1 [internal] load local bake definitions
#1 reading docker-bake.hcl 183B / 183B done
#1 DONE 0.0s
{
  "target": {
    "default": {
      "context": ".",
      "dockerfile": "Dockerfile",
      "args": {
        "BAR": ""
      }
    }
  }
}

@crazy-max crazy-max marked this pull request as ready for review November 18, 2024 10:52
bake/bake_test.go Show resolved Hide resolved
bake/hclparser/hclparser.go Outdated Show resolved Hide resolved
@crazy-max crazy-max force-pushed the bake-var-req branch 2 times, most recently from 38aab82 to d088a79 Compare November 19, 2024 10:25
@crazy-max crazy-max requested a review from tonistiigi November 19, 2024 11:23
@tonistiigi tonistiigi merged commit a6ef9db into docker:master Nov 19, 2024
112 checks passed
@crazy-max crazy-max deleted the bake-var-req branch November 19, 2024 22:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bakefile: mark variables as required
3 participants