Skip to content

Commit 205165b

Browse files
authored
Merge pull request #192 from vanstee/hcl2-with-interpolation
Upgrade to hcl2 to support variables and functions
2 parents 09339bf + 870b388 commit 205165b

File tree

264 files changed

+70417
-4182
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

264 files changed

+70417
-4182
lines changed

README.md

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,128 @@ target "db" {
614614
Complete list of valid target fields:
615615
args, cache-from, cache-to, context, dockerfile, inherits, labels, no-cache, output, platform, pull, secrets, ssh, tags, target
616616
617+
#### HCL variables and functions
618+
619+
Similar to how Terraform provides a way to [define variables](https://www.terraform.io/docs/configuration/variables.html#declaring-an-input-variable), the HCL file format also supports variable block definitions. These can be used to define variables with values provided by the current environment or a default value when unset.
620+
621+
622+
623+
Example of using interpolation to tag an image with the git sha:
624+
625+
```
626+
$ cat <<'EOF' > docker-bake.hcl
627+
variable "TAG" {
628+
default = "latest"
629+
}
630+
631+
group "default" {
632+
targets = ["webapp"]
633+
}
634+
635+
target "webapp" {
636+
tags = ["docker.io/username/webapp:${TAG}"]
637+
}
638+
EOF
639+
640+
$ docker buildx bake --print webapp
641+
{
642+
"target": {
643+
"webapp": {
644+
"context": ".",
645+
"dockerfile": "Dockerfile",
646+
"tags": [
647+
"docker.io/username/webapp:latest"
648+
]
649+
}
650+
}
651+
}
652+
653+
$ TAG=$(git rev-parse --short HEAD) docker buildx bake --print webapp
654+
{
655+
"target": {
656+
"webapp": {
657+
"context": ".",
658+
"dockerfile": "Dockerfile",
659+
"tags": [
660+
"docker.io/username/webapp:985e9e9"
661+
]
662+
}
663+
}
664+
}
665+
```
666+
667+
668+
A [set of generally useful functions](https://github.com/docker/buildx/blob/master/bake/hcl.go#L19-L65) provided by [go-cty](https://github.com/zclconf/go-cty/tree/master/cty/function/stdlib) are avaialble for use in HCL files. In addition, [user defined functions](https://github.com/hashicorp/hcl/tree/hcl2/ext/userfunc) are also supported.
669+
670+
671+
672+
Example of using the `add` function:
673+
674+
```
675+
$ cat <<'EOF' > docker-bake.hcl
676+
variable "TAG" {
677+
default = "latest"
678+
}
679+
680+
group "default" {
681+
targets = ["webapp"]
682+
}
683+
684+
target "webapp" {
685+
args = {
686+
buildno = "${add(123, 1)}"
687+
}
688+
}
689+
EOF
690+
691+
$ docker buildx bake --print webapp
692+
{
693+
"target": {
694+
"webapp": {
695+
"context": ".",
696+
"dockerfile": "Dockerfile",
697+
"args": {
698+
"buildno": "124"
699+
}
700+
}
701+
}
702+
}
703+
```
704+
705+
Example of defining an `increment` function:
706+
707+
```
708+
$ cat <<'EOF' > docker-bake.hcl
709+
function "increment" {
710+
params = [number]
711+
result = number + 1
712+
}
713+
714+
group "default" {
715+
targets = ["webapp"]
716+
}
717+
718+
target "webapp" {
719+
args = {
720+
buildno = "${increment(123)}"
721+
}
722+
}
723+
EOF
724+
725+
$ docker buildx bake --print webapp
726+
{
727+
"target": {
728+
"webapp": {
729+
"context": ".",
730+
"dockerfile": "Dockerfile",
731+
"args": {
732+
"buildno": "124"
733+
}
734+
}
735+
}
736+
}
737+
```
738+
617739
### `buildx imagetools create [OPTIONS] [SOURCE] [SOURCE...]`
618740
619741
Imagetools contains commands for working with manifest lists in the registry. These commands are useful for inspecting multi-platform build results.

0 commit comments

Comments
 (0)