功能
目录
HCL 函数非常适合在构建中作值 配置,而不仅仅是 concatenation 或 interpolation。
标准库
Bake 附带了对go-cty
标准库函数.
以下示例显示了add
功能。
docker-bake.hcl
variable "TAG" {
default = "latest"
}
group "default" {
targets = ["webapp"]
}
target "webapp" {
args = {
buildno = "${add(123, 1)}"
}
}
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": ["webapp"]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"args": {
"buildno": "124"
}
}
}
}
用户定义的函数
您可以创建用户定义的函数,如果内置的标准库函数不能执行所需的作 满足您的需求。
以下示例定义了一个increment
功能。
docker-bake.hcl
function "increment" {
params = [number]
result = number + 1
}
group "default" {
targets = ["webapp"]
}
target "webapp" {
args = {
buildno = "${increment(123)}"
}
}
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": ["webapp"]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"args": {
"buildno": "124"
}
}
}
}
函数中的变量
您可以引用变量和标准库 函数。
您不能从其他函数引用用户定义的函数。
以下示例使用全局变量 (REPO
) 在自定义函数中。
docker-bake.hcl
# docker-bake.hcl
variable "REPO" {
default = "user/repo"
}
function "tag" {
params = [tag]
result = ["${REPO}:${tag}"]
}
target "webapp" {
tags = tag("v1")
}
使用--print
标志显示tag
功能
使用REPO
设置标签的前缀。
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": ["webapp"]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"tags": ["user/repo:v1"]
}
}
}