build 블록

build 블록

이 주제에서는 build 블록에 대한 참조 정보를 제공해요.

출처: Packer 공식 문서

본문

설명 (Description)

build 블록은 Packer가 실행해야 할 빌더, 프로비저닝 방법, 그리고 결과 아티팩트에 대해 Packer가 수행해야 할 post-processing 작업을 지정해요.

build 블록에서 빌더를 사용하려면 다음 두 가지 중 하나를 할 수 있어요.

  • 미리 정의된 소스에 대한 참조가 담긴 문자열 배열인 sources를 설정하거나,
  • 빌드 레벨의 source 블록을 정의해요. 이렇게 하면 특정 필드도 설정할 수 있어요.
# build.pkr.hcl
build {
# use the `name` field to name a build in the logs.
# For example this present config will display
# "buildname.amazon-ebs.example-1" and "buildname.amazon-ebs.example-2"
name = "buildname"
sources = [
# use the optional plural `sources` list to simply use a `source`
# without changing any field.
"source.amazon-ebs.example-1",
]
source "source.amazon-ebs.example-2" {
# Use the singular `source` block set specific fields.
# Note that fields cannot be overwritten, in other words, you cannot
# set the 'output' field from the top-level source block and here.
output = "different value"
name = "differentname"
}
provisioner "shell" {
scripts = fileset(".", "scripts/{install,secure}.sh")
}
post-processor "shell-local" {
inline = ["echo Hello World from ${source.type}.${source.name}"]
}
}

빌더를 설정하려면 최상위 수준의 source 블록을 정의하세요. 사용 가능한 빌더 목록은 builders 섹션에서 찾을 수 있어요.

빌드 이름 짓기 (Naming your builds)

build 블록의 선택적 name 필드를 사용해 빌드의 이름을 설정할 수 있어요. 이름이 있는 빌드는 packer build의 로그 줄 앞에 build 블록의 이름을 접두사로 붙여요. 예를 들어:

source "null" "first-example" {
communicator = "none"
}
source "null" "second-example" {
communicator = "none"
}
build {
name = "a"
sources = [
"sources.null.first-example",
"sources.null.second-example",
]
}
build {
sources = ["sources.null.second-example"]
}

그러면 다음과 같이 출력돼요.

> packer build ./folder
Build 'a.null.first-example' finished.
Build 'a.null.second-example' finished.
Build 'null.second-example' finished.
==> Builds finished. The artifacts of successful builds are:
--> a.null.first-example: Did not export anything. This is the null builder
--> a.null.second-example: Did not export anything. This is the null builder
--> null.second-example: Did not export anything. This is the null builder

특정 빌드만 실행하기 (Running only specific builds)

-only/-except 플래그는 소스의 type.name과 일치하며, 일치하는 빌더만 실행하거나(only) 일치하는 빌더를 제외한 모든 참조된 빌더를 실행해요(except). 같은 설정 파일을 예로 들면:

> packer build -only "*.second-example" ./folder
Build 'null.second-example' finished.
Build 'a.null.second-example' finished.
==> Builds finished. The artifacts of successful builds are:
--> a.null.second-example: Did not export anything. This is the null builder
--> null.second-example: Did not export anything. This is the null builder

여기서 'a.null.first-example'은 건너뛰어졌어요.

참고: 아직은 이름이 있는 build 블록을 이 방식으로 매칭할 수 없지만, 곧 가능해질 거예요. 그래서 여기서 "a.*"는 아무것도 매칭하지 않아요.

  • 커뮤니티가 유지 관리하는 빌더에 대한 정보는 community builders reference를 참고하세요.
  • 직접 빌더를 만드는 방법은 Create Custom Builders를 참고하세요.