Bake 함수
Bake 함수 (Functions)
HCL 함수는 단순한 문자열 연결이나 보간보다 더 복잡한 방식으로 빌드 구성을 조작해야 할 때 아주 유용해요.
출처: 문서
본문
표준 라이브러리 (Standard library)
Bake에는 표준 라이브러리 함수가 기본 내장되어 있어요. 다음은 내장 add 함수로 123 + 1을 계산하는 예시예요:
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"
}
}
}
}
사용자 정의 함수 (User-defined functions)
내장 표준 라이브러리 함수가 요구사항을 충족하지 못한다면, 원하는 동작을 하는 사용자 정의 함수를 만들 수 있어요. 다음은 increment 함수를 정의해 사용하는 예시예요:
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"
}
}
}
}
함수 안에서의 변수 (Variables in functions)
함수 안에서 변수와 표준 라이브러리 함수를 참조할 수 있어요. 다음 예시는 커스텀 함수에서 REPO 변수를 참조해 태그 접두어를 설정해요:
# docker-bake.hcl
variable "REPO" {
default = "user/repo"
}
function "tag" {
params = [tag]
result = ["${REPO}:${tag}"]
}
target "webapp" {
tags = tag("v1")
}
$ docker buildx bake --print webapp
{
"group": {
"default": {
"targets": ["webapp"]
}
},
"target": {
"webapp": {
"context": ".",
"dockerfile": "Dockerfile",
"tags": ["user/repo:v1"]
}
}
}