如何在 HCL 格式的模板中使用一个供应商进行多个 Packer 构建?
How do I use one provisioner for multiple Packer builds in an HCL-formatted template?
我正在使用 Packer 为我项目的 CI 管道配置 VM,该管道托管在受支持的云提供商上。我的供应脚本有点复杂,所以我需要仔细迭代它们以确保它们正确。为了省钱,我使用了与云生成器中使用的 provisioners 相同的本地图像生成器。该本地图像不会用于生产,甚至不会用于开发;它只是用来帮助我验证我的配置脚本(以及生成的环境)。
因为我正在测试我的配置脚本,所以我想与所有相关的 build
块共享一个 provisioner
块。但是,我一辈子都不知道该怎么做;现在,我一直在复制和粘贴我的 provisioner
块。 only
字段是唯一变化的字段,因为我通常不想同时构建本地和云图像。 如何在 HCL 格式模板中的多个 build
块中使用一个 provisioner
块,以及偶尔的覆盖?
这是我要缩小的(简化版)代码:
# Variables go here...
source "vagrant" "windows-local" {
source_path = "gusztavvargadr/visual-studio"
box_name = "build-runner-windows-local"
box_version = "v2019.0.2010"
communicator = "ssh"
# Other fields omitted for brevity
}
source "amazon-ebs" "windows" {
access_key = "${var.aws_access_key}"
ami_name = "build-runner-windows"
communicator = "winrm"
instance_type = "t2.micro"
region = "${var.aws_region}"
secret_key = "${var.aws_secret_key}"
source_ami_filter {
filters = {
name = "Windows_Server-2019-English-Full-Base-2020.11.11"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
}
# Other fields omitted for brevity
}
build {
name = "windows-aws"
sources = ["source.amazon-ebs.windows"]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["amazon-ebs.windows"] # Note: Not the same value as in the windows-local build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
build {
name = "windows-local"
sources = ["source.vagrant.windows-local"]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["vagrant.windows-local"] # Note: Not the same value as in the windows-aws build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
您只需要将源构建器添加到构建中的源中。在你的情况下,那将是:
build {
sources = [
"source.vagrant.windows-local",
"source.amazon-ebs.windows"
]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["vagrant.windows-local"] # Note: Not the same value as in the windows-aws build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
然后,您可以从构建中删除名称并将它们添加到特定的源代码块中,例如:
source "vagrant" "windows-local" {
name = "windows-local"
source_path = "gusztavvargadr/visual-studio"
box_name = "build-runner-windows-local"
# ...
}
source "amazon-ebs" "windows" {
name = "windows-aws"
access_key = "${var.aws_access_key}"
ami_name = "build-runner-windows"
# ...
}
如果您需要更多信息,这里的一些文档可能会对您有所帮助:
https://www.packer.io/docs/templates/hcl_templates/blocks/build/source
https://www.packer.io/docs/templates/hcl_templates/blocks/build/provisioner
我正在使用 Packer 为我项目的 CI 管道配置 VM,该管道托管在受支持的云提供商上。我的供应脚本有点复杂,所以我需要仔细迭代它们以确保它们正确。为了省钱,我使用了与云生成器中使用的 provisioners 相同的本地图像生成器。该本地图像不会用于生产,甚至不会用于开发;它只是用来帮助我验证我的配置脚本(以及生成的环境)。
因为我正在测试我的配置脚本,所以我想与所有相关的 build
块共享一个 provisioner
块。但是,我一辈子都不知道该怎么做;现在,我一直在复制和粘贴我的 provisioner
块。 only
字段是唯一变化的字段,因为我通常不想同时构建本地和云图像。 如何在 HCL 格式模板中的多个 build
块中使用一个 provisioner
块,以及偶尔的覆盖?
这是我要缩小的(简化版)代码:
# Variables go here...
source "vagrant" "windows-local" {
source_path = "gusztavvargadr/visual-studio"
box_name = "build-runner-windows-local"
box_version = "v2019.0.2010"
communicator = "ssh"
# Other fields omitted for brevity
}
source "amazon-ebs" "windows" {
access_key = "${var.aws_access_key}"
ami_name = "build-runner-windows"
communicator = "winrm"
instance_type = "t2.micro"
region = "${var.aws_region}"
secret_key = "${var.aws_secret_key}"
source_ami_filter {
filters = {
name = "Windows_Server-2019-English-Full-Base-2020.11.11"
root-device-type = "ebs"
virtualization-type = "hvm"
}
most_recent = true
owners = ["amazon"]
}
# Other fields omitted for brevity
}
build {
name = "windows-aws"
sources = ["source.amazon-ebs.windows"]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["amazon-ebs.windows"] # Note: Not the same value as in the windows-local build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
build {
name = "windows-local"
sources = ["source.vagrant.windows-local"]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["vagrant.windows-local"] # Note: Not the same value as in the windows-aws build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
您只需要将源构建器添加到构建中的源中。在你的情况下,那将是:
build {
sources = [
"source.vagrant.windows-local",
"source.amazon-ebs.windows"
]
provisioner "powershell" {
environment_vars = [
"AWS_ACCESS_KEY_ID=${var.aws_access_key}",
"AWS_SECRET_ACCESS_KEY=${var.aws_secret_key}",
"AWS_DEFAULT_REGION=${var.aws_region}",
"A_DOZEN_MORE_VARS=..."
]
only = ["vagrant.windows-local"] # Note: Not the same value as in the windows-aws build
scripts = [
"windows/first-script.ps1",
"windows/second-script.ps1",
"windows/cleanup.ps1"
]
}
provisioner "windows-restart" {}
}
然后,您可以从构建中删除名称并将它们添加到特定的源代码块中,例如:
source "vagrant" "windows-local" {
name = "windows-local"
source_path = "gusztavvargadr/visual-studio"
box_name = "build-runner-windows-local"
# ...
}
source "amazon-ebs" "windows" {
name = "windows-aws"
access_key = "${var.aws_access_key}"
ami_name = "build-runner-windows"
# ...
}
如果您需要更多信息,这里的一些文档可能会对您有所帮助: https://www.packer.io/docs/templates/hcl_templates/blocks/build/source https://www.packer.io/docs/templates/hcl_templates/blocks/build/provisioner