From f08f19a834f0d34993d20b5fd2fa46c4c8f50f21 Mon Sep 17 00:00:00 2001 From: Alexey-Ayupov Date: Thu, 29 May 2025 20:16:09 +0200 Subject: [PATCH 1/3] Refactor Packer templates for Windows and Ubuntu images --- images.CI/linux-and-win/build-image.ps1 | 25 +- ....04.pkr.hcl => build.ubuntu-22_04.pkr.hcl} | 234 +--------------- ....04.pkr.hcl => build.ubuntu-24_04.pkr.hcl} | 234 +--------------- ...l.pkr.hcl => build.ubuntu-minimal.pkr.hcl} | 117 +------- images/ubuntu/templates/locals.ubuntu.pkr.hcl | 21 ++ images/ubuntu/templates/source.ubuntu.pkr.hcl | 47 ++++ .../ubuntu/templates/variable.ubuntu.pkr.hcl | 179 ++++++++++++ ...019.pkr.hcl => build.windows-2019.pkr.hcl} | 261 +----------------- ...022.pkr.hcl => build.windows-2022.pkr.hcl} | 261 +----------------- ...025.pkr.hcl => build.windows-2025.pkr.hcl} | 261 +----------------- .../windows/templates/locals.windows.pkr.hcl | 12 + .../windows/templates/source.windows.pkr.hcl | 53 ++++ .../templates/variable.windows.pkr.hcl | 187 +++++++++++++ 13 files changed, 534 insertions(+), 1358 deletions(-) rename images/ubuntu/templates/{ubuntu-22.04.pkr.hcl => build.ubuntu-22_04.pkr.hcl} (66%) rename images/ubuntu/templates/{ubuntu-24.04.pkr.hcl => build.ubuntu-24_04.pkr.hcl} (64%) rename images/ubuntu/templates/{ubuntu-minimal.pkr.hcl => build.ubuntu-minimal.pkr.hcl} (72%) create mode 100644 images/ubuntu/templates/locals.ubuntu.pkr.hcl create mode 100644 images/ubuntu/templates/source.ubuntu.pkr.hcl create mode 100644 images/ubuntu/templates/variable.ubuntu.pkr.hcl rename images/windows/templates/{windows-2019.pkr.hcl => build.windows-2019.pkr.hcl} (65%) rename images/windows/templates/{windows-2022.pkr.hcl => build.windows-2022.pkr.hcl} (64%) rename images/windows/templates/{windows-2025.pkr.hcl => build.windows-2025.pkr.hcl} (64%) create mode 100644 images/windows/templates/locals.windows.pkr.hcl create mode 100644 images/windows/templates/source.windows.pkr.hcl create mode 100644 images/windows/templates/variable.windows.pkr.hcl diff --git a/images.CI/linux-and-win/build-image.ps1 b/images.CI/linux-and-win/build-image.ps1 index 7db04fb971..523295113b 100644 --- a/images.CI/linux-and-win/build-image.ps1 +++ b/images.CI/linux-and-win/build-image.ps1 @@ -1,5 +1,6 @@ param( [String] [Parameter (Mandatory=$true)] $TemplatePath, + [String] [Parameter (Mandatory=$true)] $BuildTemplateName, [String] [Parameter (Mandatory=$true)] $ClientId, [String] [Parameter (Mandatory=$false)] $ClientSecret, [String] [Parameter (Mandatory=$true)] $Location, @@ -8,7 +9,8 @@ param( [String] [Parameter (Mandatory=$true)] $TempResourceGroupName, [String] [Parameter (Mandatory=$true)] $SubscriptionId, [String] [Parameter (Mandatory=$true)] $TenantId, - [String] [Parameter (Mandatory=$false)] $pluginVersion = "2.2.1", + [String] [Parameter (Mandatory=$false)] $UseAzureCliAuth = "false", + [String] [Parameter (Mandatory=$false)] $PluginVersion = "2.3.3", [String] [Parameter (Mandatory=$false)] $VirtualNetworkName, [String] [Parameter (Mandatory=$false)] $VirtualNetworkRG, [String] [Parameter (Mandatory=$false)] $VirtualNetworkSubnet, @@ -22,9 +24,17 @@ if (-not (Test-Path $TemplatePath)) exit 1 } -$ImageTemplateName = [io.path]::GetFileName($TemplatePath).Split(".")[0] +$buildName = $($BuildTemplateName).Split(".")[1] $InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper() +switch ($BuildTemplateName) { + "build.windows-2019.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2019-Datacenter" } + "build.windows-2022.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2022-Datacenter" } + "build.windows-2025.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2025-Datacenter" } + "build.ubuntu-22_04.pkr.hcl" { $imageURN = "canonical:0001-com-ubuntu-server-jammy:22_04-lts" } + "build.ubuntu-24_04.pkr.hcl" { $imageURN = "canonical:ubuntu-24_04-lts:server-gen1" } +} + $SensitiveData = @( 'OSType', 'StorageAccountLocation', @@ -44,13 +54,17 @@ Write-Host "Download packer plugins" packer plugins install github.com/hashicorp/azure $pluginVersion Write-Host "Validate packer template" -packer validate -syntax-only $TemplatePath +packer validate -syntax-only -only "$buildName*" $TemplatePath -Write-Host "Build $ImageTemplateName VM" -packer build -var "client_id=$ClientId" ` +Write-Host "Build $buildName VM" +packer build -only "$buildName*" ` + -var "client_id=$ClientId" ` -var "client_secret=$ClientSecret" ` -var "install_password=$InstallPassword" ` -var "location=$Location" ` + -var "image_publisher=$($imageURN.Split(":")[0])" ` + -var "image_offer=$($imageURN.Split(":")[1])" ` + -var "image_sku=$($imageURN.Split(":")[2])" ` -var "managed_image_name=$ImageName" ` -var "managed_image_resource_group_name=$ImageResourceGroupName" ` -var "subscription_id=$SubscriptionId" ` @@ -60,6 +74,7 @@ packer build -var "client_id=$ClientId" ` -var "virtual_network_resource_group_name=$VirtualNetworkRG" ` -var "virtual_network_subnet_name=$VirtualNetworkSubnet" ` -var "allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` + -var "use_azure_cli_auth=$UseAzureCliAuth" ` -var "azure_tags=$azure_tags" ` -color=false ` $TemplatePath ` diff --git a/images/ubuntu/templates/ubuntu-22.04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl similarity index 66% rename from images/ubuntu/templates/ubuntu-22.04.pkr.hcl rename to images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl index d3486533a1..b649ce4745 100644 --- a/images/ubuntu/templates/ubuntu-22.04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl @@ -1,234 +1,6 @@ -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "dockerhub_login" { - type = string - default = "${env("DOCKERHUB_LOGIN")}" -} - -variable "dockerhub_password" { - type = string - default = "${env("DOCKERHUB_PASSWORD")}" -} - -variable "helper_script_folder" { - type = string - default = "/imagegeneration/helpers" -} - -variable "image_folder" { - type = string - default = "/imagegeneration" -} - -variable "image_os" { - type = string - default = "ubuntu22" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "/imagegeneration/imagedata.json" -} - -variable "installer_script_folder" { - type = string - default = "/imagegeneration/installers" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -variable "image_offer" { - type = string - default = "0001-com-ubuntu-server-jammy" -} - -variable "image_publisher" { - type = string - default = "canonical" -} - -variable "image_sku" { - type = string - default = "22_04-lts" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 75 -} - -variable "image_os_type" { - type = string - default = "Linux" -} - -source "azure-arm" "build_image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { - sources = ["source.azure-arm.build_image"] + sources = ["source.azure-arm.image"] + name = "ubuntu-22_04" provisioner "shell" { execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" @@ -299,7 +71,7 @@ build { } provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" scripts = ["${path.root}/../scripts/build/configure-environment.sh"] } diff --git a/images/ubuntu/templates/ubuntu-24.04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl similarity index 64% rename from images/ubuntu/templates/ubuntu-24.04.pkr.hcl rename to images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl index 65eba995b4..9d4f946543 100644 --- a/images/ubuntu/templates/ubuntu-24.04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl @@ -1,234 +1,6 @@ -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "dockerhub_login" { - type = string - default = "${env("DOCKERHUB_LOGIN")}" -} - -variable "dockerhub_password" { - type = string - default = "${env("DOCKERHUB_PASSWORD")}" -} - -variable "helper_script_folder" { - type = string - default = "/imagegeneration/helpers" -} - -variable "image_folder" { - type = string - default = "/imagegeneration" -} - -variable "image_os" { - type = string - default = "ubuntu24" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "/imagegeneration/imagedata.json" -} - -variable "installer_script_folder" { - type = string - default = "/imagegeneration/installers" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -variable "image_offer" { - type = string - default = "ubuntu-24_04-lts" -} - -variable "image_publisher" { - type = string - default = "canonical" -} - -variable "image_sku" { - type = string - default = "server-gen1" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 75 -} - -variable "image_os_type" { - type = string - default = "Linux" -} - -source "azure-arm" "build_image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { - sources = ["source.azure-arm.build_image"] + sources = ["source.azure-arm.image"] + name = "ubuntu-24_04" provisioner "shell" { execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" @@ -299,7 +71,7 @@ build { } provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" scripts = ["${path.root}/../scripts/build/configure-environment.sh"] } diff --git a/images/ubuntu/templates/ubuntu-minimal.pkr.hcl b/images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl similarity index 72% rename from images/ubuntu/templates/ubuntu-minimal.pkr.hcl rename to images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl index a2e7d362bd..e66ea89bdd 100644 --- a/images/ubuntu/templates/ubuntu-minimal.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl @@ -1,115 +1,4 @@ - -locals { - image_os = "ubuntu22" - - toolset_file_name = "toolset-2204.json" - - image_folder = "/imagegeneration" - helper_script_folder = "/imagegeneration/helpers" - installer_script_folder = "/imagegeneration/installers" - imagedata_file = "/imagegeneration/imagedata.json" - - managed_image_name = var.managed_image_name != "" ? var.managed_image_name : "packer-${var.image_os}-${var.image_version}" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RESOURCE_GROUP_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "location" { - type = string - default = "${env("ARM_RESOURCE_LOCATION")}" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_D4s_v4" -} - -source "azure-arm" "build_image" { +source "azure-arm" "ubuntu-minimal_image" { location = "${var.location}" // Auth @@ -154,7 +43,7 @@ source "azure-arm" "build_image" { } build { - sources = ["source.azure-arm.build_image"] + sources = ["source.azure-arm.ubuntu-minimal_image"] // Create folder to store temporary data provisioner "shell" { @@ -221,7 +110,7 @@ build { // Create /etc/environment, configure waagent etc. provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_os}", "HELPER_SCRIPTS=${local.helper_script_folder}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.ubuntu_minimal_image_os}", "HELPER_SCRIPTS=${local.helper_script_folder}"] execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" scripts = ["${path.root}/../scripts/build/configure-environment.sh"] } diff --git a/images/ubuntu/templates/locals.ubuntu.pkr.hcl b/images/ubuntu/templates/locals.ubuntu.pkr.hcl new file mode 100644 index 0000000000..407a703900 --- /dev/null +++ b/images/ubuntu/templates/locals.ubuntu.pkr.hcl @@ -0,0 +1,21 @@ +locals { + image_properties = var.image_sku == "22_04-lts" ? { + image_os = "ubuntu22" + os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 75 + } : { + image_os = "ubuntu24" + os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 75 + } +} + +// Define local variables for the ubuntu-minimal build +locals { + ubuntu_minimal_image_os = "ubuntu22" + + toolset_file_name = "toolset-2204.json" + + image_folder = "/imagegeneration" + helper_script_folder = "/imagegeneration/helpers" + installer_script_folder = "/imagegeneration/installers" + imagedata_file = "/imagegeneration/imagedata.json" +} diff --git a/images/ubuntu/templates/source.ubuntu.pkr.hcl b/images/ubuntu/templates/source.ubuntu.pkr.hcl new file mode 100644 index 0000000000..90936f39eb --- /dev/null +++ b/images/ubuntu/templates/source.ubuntu.pkr.hcl @@ -0,0 +1,47 @@ +source "azure-arm" "image" { + client_cert_path = var.client_cert_path + client_id = var.client_id + client_secret = var.client_secret + object_id = var.object_id + oidc_request_token = var.oidc_request_token + oidc_request_url = var.oidc_request_url + subscription_id = var.subscription_id + tenant_id = var.tenant_id + use_azure_cli_auth = var.use_azure_cli_auth + + allowed_inbound_ip_addresses = var.allowed_inbound_ip_addresses + build_resource_group_name = var.build_resource_group_name + image_offer = var.image_offer + image_publisher = var.image_publisher + image_sku = var.image_sku + location = var.location + managed_image_name = var.managed_image_name + managed_image_resource_group_name = var.managed_image_resource_group_name + managed_image_storage_account_type = var.managed_image_storage_account_type + os_disk_size_gb = local.image_properties.os_disk_size_gb + os_type = var.image_os_type + private_virtual_network_with_public_ip = var.private_virtual_network_with_public_ip + temp_resource_group_name = var.temp_resource_group_name + virtual_network_name = var.virtual_network_name + virtual_network_resource_group_name = var.virtual_network_resource_group_name + virtual_network_subnet_name = var.virtual_network_subnet_name + vm_size = var.vm_size + winrm_username = var.winrm_username + + shared_image_gallery_destination { + subscription = var.subscription_id + gallery_name = var.gallery_name + resource_group = var.gallery_resource_group_name + image_name = var.gallery_image_name + image_version = var.gallery_image_version + storage_account_type = var.gallery_storage_account_type + } + + dynamic "azure_tag" { + for_each = var.azure_tags + content { + name = azure_tag.key + value = azure_tag.value + } + } +} diff --git a/images/ubuntu/templates/variable.ubuntu.pkr.hcl b/images/ubuntu/templates/variable.ubuntu.pkr.hcl new file mode 100644 index 0000000000..83962df71b --- /dev/null +++ b/images/ubuntu/templates/variable.ubuntu.pkr.hcl @@ -0,0 +1,179 @@ +// Authentication related variables +variable "client_cert_path" { + type = string + default = "${env("ARM_CLIENT_CERT_PATH")}" +} +variable "client_id" { + type = string + default = "${env("ARM_CLIENT_ID")}" +} +variable "client_secret" { + type = string + default = "${env("ARM_CLIENT_SECRET")}" + sensitive = true +} +variable "object_id" { + type = string + default = "${env("ARM_OBJECT_ID")}" +} +variable "oidc_request_token" { + type = string + default = "" +} +variable "oidc_request_url" { + type = string + default = "" +} +variable "subscription_id" { + type = string + default = "${env("ARM_SUBSCRIPTION_ID")}" +} +variable "tenant_id" { + type = string + default = "${env("ARM_TENANT_ID")}" +} +variable "use_azure_cli_auth" { + type = bool + default = false +} + +// Azure environment related variables +variable "allowed_inbound_ip_addresses" { + type = list(string) + default = [] +} +variable "azure_tags" { + type = map(string) + default = {} +} +variable "build_resource_group_name" { + type = string + default = "${env("BUILD_RG_NAME")}" +} +variable "gallery_image_name" { + type = string + default = "${env("GALLERY_IMAGE_NAME")}" +} +variable "gallery_image_version" { + type = string + default = "${env("GALLERY_IMAGE_VERSION")}" +} +variable "gallery_name" { + type = string + default = "${env("GALLERY_NAME")}" +} +variable "gallery_resource_group_name" { + type = string + default = "${env("GALLERY_RG_NAME")}" +} +variable "gallery_storage_account_type" { + type = string + default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" +} +variable "image_offer" { + type = string + default = "" +} +variable "image_os_type" { + type = string + default = "Linux" +} +variable "image_publisher" { + type = string + default = "" +} +variable "image_sku" { + type = string + default = "" +} +variable "location" { + type = string + default = "" +} +variable "managed_image_name" { + type = string + default = "" +} +variable "managed_image_resource_group_name" { + type = string + default = "${env("ARM_RESOURCE_GROUP")}" +} +variable "managed_image_storage_account_type" { + type = string + default = "Premium_LRS" +} +variable "private_virtual_network_with_public_ip" { + type = bool + default = false +} +variable "os_disk_size_gb" { + type = number + default = 0 +} +variable "temp_resource_group_name" { + type = string + default = "${env("TEMP_RESOURCE_GROUP_NAME")}" +} +variable "virtual_network_name" { + type = string + default = "${env("VNET_NAME")}" +} +variable "virtual_network_resource_group_name" { + type = string + default = "${env("VNET_RESOURCE_GROUP")}" +} +variable "virtual_network_subnet_name" { + type = string + default = "${env("VNET_SUBNET")}" +} +variable "vm_size" { + type = string + default = "Standard_D4s_v4" +} +variable "winrm_username" { // The username used to connect to the VM via WinRM + type = string // Also applies to the username used to create the VM + default = "packer" +} + +// Image related variables +variable "dockerhub_login" { + type = string + default = "${env("DOCKERHUB_LOGIN")}" +} +variable "dockerhub_password" { + type = string + default = "${env("DOCKERHUB_PASSWORD")}" +} +variable "helper_script_folder" { + type = string + default = "/imagegeneration/helpers" +} +variable "image_folder" { + type = string + default = "/imagegeneration" +} +variable "image_os" { + type = string + default = "" +} +variable "image_version" { + type = string + default = "dev" +} +variable "imagedata_file" { + type = string + default = "/imagegeneration/imagedata.json" +} +variable "installer_script_folder" { + type = string + default = "/imagegeneration/installers" +} +variable "install_password" { + type = string + default = "" + sensitive = true +} +variable "install_user" { + type = string + default = "installer" +} diff --git a/images/windows/templates/windows-2019.pkr.hcl b/images/windows/templates/build.windows-2019.pkr.hcl similarity index 65% rename from images/windows/templates/windows-2019.pkr.hcl rename to images/windows/templates/build.windows-2019.pkr.hcl index 858b7c0d47..c35048c3a9 100644 --- a/images/windows/templates/windows-2019.pkr.hcl +++ b/images/windows/templates/build.windows-2019.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win19" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2019-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 256 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2019" provisioner "powershell" { inline = [ @@ -326,7 +69,7 @@ build { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/windows-2022.pkr.hcl b/images/windows/templates/build.windows-2022.pkr.hcl similarity index 64% rename from images/windows/templates/windows-2022.pkr.hcl rename to images/windows/templates/build.windows-2022.pkr.hcl index 76069d7191..f7649b372c 100644 --- a/images/windows/templates/windows-2022.pkr.hcl +++ b/images/windows/templates/build.windows-2022.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win22" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2022-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 256 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2022" provisioner "powershell" { inline = [ @@ -315,7 +58,7 @@ build { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/windows-2025.pkr.hcl b/images/windows/templates/build.windows-2025.pkr.hcl similarity index 64% rename from images/windows/templates/windows-2025.pkr.hcl rename to images/windows/templates/build.windows-2025.pkr.hcl index a375bbb67b..50368d0bc3 100644 --- a/images/windows/templates/windows-2025.pkr.hcl +++ b/images/windows/templates/build.windows-2025.pkr.hcl @@ -1,263 +1,6 @@ -variable "agent_tools_directory" { - type = string - default = "C:\\hostedtoolcache\\windows" -} - -variable "allowed_inbound_ip_addresses" { - type = list(string) - default = [] -} - -variable "azure_tags" { - type = map(string) - default = {} -} - -variable "build_resource_group_name" { - type = string - default = "${env("BUILD_RG_NAME")}" -} - -variable "client_cert_path" { - type = string - default = "${env("ARM_CLIENT_CERT_PATH")}" -} - -variable "client_id" { - type = string - default = "${env("ARM_CLIENT_ID")}" -} - -variable "client_secret" { - type = string - default = "${env("ARM_CLIENT_SECRET")}" - sensitive = true -} - -variable "helper_script_folder" { - type = string - default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" -} - -variable "image_folder" { - type = string - default = "C:\\image" -} - -variable "image_os" { - type = string - default = "win25" -} - -variable "image_version" { - type = string - default = "dev" -} - -variable "imagedata_file" { - type = string - default = "C:\\imagedata.json" -} - -variable "temp_dir" { - type = string - default = "D:\\temp" -} - -variable "install_password" { - type = string - default = "" - sensitive = true -} - -variable "install_user" { - type = string - default = "installer" -} - -variable "location" { - type = string - default = "" -} - -variable "managed_image_name" { - type = string - default = "" -} - -variable "managed_image_resource_group_name" { - type = string - default = "${env("ARM_RESOURCE_GROUP")}" -} - -variable "managed_image_storage_account_type" { - type = string - default = "Premium_LRS" -} - -variable "object_id" { - type = string - default = "${env("ARM_OBJECT_ID")}" -} - -variable "private_virtual_network_with_public_ip" { - type = bool - default = false -} - -variable "subscription_id" { - type = string - default = "${env("ARM_SUBSCRIPTION_ID")}" -} - -variable "temp_resource_group_name" { - type = string - default = "${env("TEMP_RESOURCE_GROUP_NAME")}" -} - -variable "tenant_id" { - type = string - default = "${env("ARM_TENANT_ID")}" -} - -variable "virtual_network_name" { - type = string - default = "${env("VNET_NAME")}" -} - -variable "virtual_network_resource_group_name" { - type = string - default = "${env("VNET_RESOURCE_GROUP")}" -} - -variable "virtual_network_subnet_name" { - type = string - default = "${env("VNET_SUBNET")}" -} - -variable "vm_size" { - type = string - default = "Standard_F8s_v2" -} - -variable "image_offer" { - type = string - default = "WindowsServer" -} - -variable "image_publisher" { - type = string - default = "MicrosoftWindowsServer" -} - -variable "image_sku" { - type = string - default = "2025-Datacenter" -} - -variable "gallery_name" { - type = string - default = "${env("GALLERY_NAME")}" -} - -variable "gallery_resource_group_name" { - type = string - default = "${env("GALLERY_RG_NAME")}" -} - -variable "gallery_image_name" { - type = string - default = "${env("GALLERY_IMAGE_NAME")}" -} - -variable "gallery_image_version" { - type = string - default = "${env("GALLERY_IMAGE_VERSION")}" -} - -variable "gallery_storage_account_type" { - type = string - default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" -} - -variable "build_key_vault_name" { - type = string - default = "${env("BUILD_KEY_VAULT_NAME")}" -} - -variable "build_key_vault_secret_name" { - type = string - default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" -} - -variable "use_azure_cli_auth" { - type = bool - default = false -} - -variable "os_disk_size_gb" { - type = number - default = 150 -} - -variable "image_os_type" { - type = string - default = "Windows" -} - -source "azure-arm" "image" { - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - build_resource_group_name = "${var.build_resource_group_name}" - client_cert_path = "${var.client_cert_path}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - use_azure_cli_auth = var.use_azure_cli_auth - communicator = "winrm" - image_offer = "${var.image_offer}" - image_publisher = "${var.image_publisher}" - image_sku = "${var.image_sku}" - location = "${var.location}" - managed_image_name = "${var.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - managed_image_storage_account_type = "${var.managed_image_storage_account_type}" - object_id = "${var.object_id}" - os_disk_size_gb = var.os_disk_size_gb - os_type = var.image_os_type - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - subscription_id = "${var.subscription_id}" - temp_resource_group_name = "${var.temp_resource_group_name}" - tenant_id = "${var.tenant_id}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - vm_size = "${var.vm_size}" - winrm_insecure = "true" - winrm_use_ssl = "true" - winrm_username = "packer" - winrm_expiration_time = "1440h" - build_key_vault_name = var.build_key_vault_name - build_key_vault_secret_name = var.build_key_vault_secret_name - - shared_image_gallery_destination { - subscription = var.subscription_id - gallery_name = var.gallery_name - resource_group = var.gallery_resource_group_name - image_name = var.gallery_image_name - image_version = var.gallery_image_version - storage_account_type = var.gallery_storage_account_type - } - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - build { sources = ["source.azure-arm.image"] + name = "windows-2025" provisioner "powershell" { inline = [ @@ -315,7 +58,7 @@ provisioner "powershell" { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/locals.windows.pkr.hcl b/images/windows/templates/locals.windows.pkr.hcl new file mode 100644 index 0000000000..fbadd7c166 --- /dev/null +++ b/images/windows/templates/locals.windows.pkr.hcl @@ -0,0 +1,12 @@ +locals { + image_properties = var.image_sku == "2019-Datacenter" ? { + image_os = "win19" + os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 256 + } : var.image_sku == "2022-Datacenter" ? { + image_os = "win22" + os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 256 + } : { + image_os = "win25" + os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 150 + } +} diff --git a/images/windows/templates/source.windows.pkr.hcl b/images/windows/templates/source.windows.pkr.hcl new file mode 100644 index 0000000000..19cfadc71a --- /dev/null +++ b/images/windows/templates/source.windows.pkr.hcl @@ -0,0 +1,53 @@ +source "azure-arm" "image" { + client_cert_path = var.client_cert_path + client_id = var.client_id + client_secret = var.client_secret + object_id = var.object_id + oidc_request_token = var.oidc_request_token + oidc_request_url = var.oidc_request_url + subscription_id = var.subscription_id + tenant_id = var.tenant_id + use_azure_cli_auth = var.use_azure_cli_auth + + allowed_inbound_ip_addresses = var.allowed_inbound_ip_addresses + build_key_vault_name = var.build_key_vault_name + build_key_vault_secret_name = var.build_key_vault_secret_name + build_resource_group_name = var.build_resource_group_name + communicator = "winrm" + image_offer = var.image_offer + image_publisher = var.image_publisher + image_sku = var.image_sku + location = var.location + managed_image_name = var.managed_image_name + managed_image_resource_group_name = var.managed_image_resource_group_name + managed_image_storage_account_type = var.managed_image_storage_account_type + os_disk_size_gb = local.image_properties.os_disk_size_gb + os_type = var.image_os_type + private_virtual_network_with_public_ip = var.private_virtual_network_with_public_ip + temp_resource_group_name = var.temp_resource_group_name + virtual_network_name = var.virtual_network_name + virtual_network_resource_group_name = var.virtual_network_resource_group_name + virtual_network_subnet_name = var.virtual_network_subnet_name + vm_size = var.vm_size + winrm_expiration_time = var.winrm_expiration_time + winrm_insecure = "true" + winrm_use_ssl = "true" + winrm_username = var.winrm_username + + shared_image_gallery_destination { + subscription = var.subscription_id + gallery_name = var.gallery_name + resource_group = var.gallery_resource_group_name + image_name = var.gallery_image_name + image_version = var.gallery_image_version + storage_account_type = var.gallery_storage_account_type + } + + dynamic "azure_tag" { + for_each = var.azure_tags + content { + name = azure_tag.key + value = azure_tag.value + } + } +} diff --git a/images/windows/templates/variable.windows.pkr.hcl b/images/windows/templates/variable.windows.pkr.hcl new file mode 100644 index 0000000000..17ce8745d8 --- /dev/null +++ b/images/windows/templates/variable.windows.pkr.hcl @@ -0,0 +1,187 @@ +// Authentication related variables +variable "client_cert_path" { + type = string + default = "${env("ARM_CLIENT_CERT_PATH")}" +} +variable "client_id" { + type = string + default = "${env("ARM_CLIENT_ID")}" +} +variable "client_secret" { + type = string + default = "${env("ARM_CLIENT_SECRET")}" + sensitive = true +} +variable "object_id" { + type = string + default = "${env("ARM_OBJECT_ID")}" +} +variable "oidc_request_token" { + type = string + default = "" +} +variable "oidc_request_url" { + type = string + default = "" +} +variable "subscription_id" { + type = string + default = "${env("ARM_SUBSCRIPTION_ID")}" +} +variable "tenant_id" { + type = string + default = "${env("ARM_TENANT_ID")}" +} +variable "use_azure_cli_auth" { + type = bool + default = false +} + +// Azure environment related variables +variable "allowed_inbound_ip_addresses" { + type = list(string) + default = [] +} +variable "azure_tags" { + type = map(string) + default = {} +} +variable "build_key_vault_name" { + type = string + default = "${env("BUILD_KEY_VAULT_NAME")}" +} +variable "build_key_vault_secret_name" { + type = string + default = "${env("BUILD_KEY_VAULT_SECRET_NAME")}" +} +variable "build_resource_group_name" { + type = string + default = "${env("BUILD_RG_NAME")}" +} +variable "gallery_image_name" { + type = string + default = "${env("GALLERY_IMAGE_NAME")}" +} +variable "gallery_image_version" { + type = string + default = "${env("GALLERY_IMAGE_VERSION")}" +} +variable "gallery_name" { + type = string + default = "${env("GALLERY_NAME")}" +} +variable "gallery_resource_group_name" { + type = string + default = "${env("GALLERY_RG_NAME")}" +} +variable "gallery_storage_account_type" { + type = string + default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" +} +variable "image_offer" { + type = string + default = "" +} +variable "image_os_type" { + type = string + default = "Windows" +} +variable "image_publisher" { + type = string + default = "" +} +variable "image_sku" { + type = string + default = "" +} +variable "location" { + type = string + default = "" +} +variable "managed_image_name" { + type = string + default = "" +} +variable "managed_image_resource_group_name" { + type = string + default = "${env("ARM_RESOURCE_GROUP")}" +} +variable "managed_image_storage_account_type" { + type = string + default = "Premium_LRS" +} +variable "private_virtual_network_with_public_ip" { + type = bool + default = false +} +variable "os_disk_size_gb" { + type = number + default = 0 +} +variable "temp_resource_group_name" { + type = string + default = "${env("TEMP_RESOURCE_GROUP_NAME")}" +} +variable "virtual_network_name" { + type = string + default = "${env("VNET_NAME")}" +} +variable "virtual_network_resource_group_name" { + type = string + default = "${env("VNET_RESOURCE_GROUP")}" +} +variable "virtual_network_subnet_name" { + type = string + default = "${env("VNET_SUBNET")}" +} +variable "vm_size" { + type = string + default = "Standard_F8s_v2" +} +variable "winrm_expiration_time" { // A time duration with which to set the WinRM certificate to expire + type = string // Also applies to key vault secret expiration time + default = "1440h" +} +variable "winrm_username" { // The username used to connect to the VM via WinRM + type = string // Also applies to the username used to create the VM + default = "packer" +} + +// Image related variables +variable "agent_tools_directory" { + type = string + default = "C:\\hostedtoolcache\\windows" +} +variable "helper_script_folder" { + type = string + default = "C:\\Program Files\\WindowsPowerShell\\Modules\\" +} +variable "image_folder" { + type = string + default = "C:\\image" +} +variable "image_os" { + type = string + default = "" +} +variable "image_version" { + type = string + default = "dev" +} +variable "imagedata_file" { + type = string + default = "C:\\imagedata.json" +} +variable "install_password" { + type = string + default = "" + sensitive = true +} +variable "install_user" { + type = string + default = "installer" +} +variable "temp_dir" { + type = string + default = "D:\\temp" +} From 7f6da82ecb27a5306fee0d0a8ce51ca3682a7391 Mon Sep 17 00:00:00 2001 From: Alexey-Ayupov Date: Fri, 6 Jun 2025 12:51:17 +0200 Subject: [PATCH 2/3] Remove ubuntu-minimal image, resolve comments --- images.CI/linux-and-win/build-image.ps1 | 13 +- .../templates/build.ubuntu-22_04.pkr.hcl | 2 +- .../templates/build.ubuntu-24_04.pkr.hcl | 2 +- .../templates/build.ubuntu-minimal.pkr.hcl | 171 ------------------ images/ubuntu/templates/locals.ubuntu.pkr.hcl | 33 ++-- images/ubuntu/templates/source.ubuntu.pkr.hcl | 7 +- .../ubuntu/templates/variable.ubuntu.pkr.hcl | 16 +- .../templates/build.windows-2019.pkr.hcl | 2 +- .../templates/build.windows-2022.pkr.hcl | 2 +- .../templates/build.windows-2025.pkr.hcl | 2 +- .../windows/templates/locals.windows.pkr.hcl | 32 +++- .../windows/templates/source.windows.pkr.hcl | 7 +- .../templates/variable.windows.pkr.hcl | 16 +- 13 files changed, 60 insertions(+), 245 deletions(-) delete mode 100644 images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl diff --git a/images.CI/linux-and-win/build-image.ps1 b/images.CI/linux-and-win/build-image.ps1 index 523295113b..431702ce99 100644 --- a/images.CI/linux-and-win/build-image.ps1 +++ b/images.CI/linux-and-win/build-image.ps1 @@ -9,6 +9,7 @@ param( [String] [Parameter (Mandatory=$true)] $TempResourceGroupName, [String] [Parameter (Mandatory=$true)] $SubscriptionId, [String] [Parameter (Mandatory=$true)] $TenantId, + [String] [Parameter (Mandatory=$true)] $ImageOS, # e.g. "ubuntu22", "ubuntu22" or "win19", "win22", "win25" [String] [Parameter (Mandatory=$false)] $UseAzureCliAuth = "false", [String] [Parameter (Mandatory=$false)] $PluginVersion = "2.3.3", [String] [Parameter (Mandatory=$false)] $VirtualNetworkName, @@ -27,14 +28,6 @@ if (-not (Test-Path $TemplatePath)) $buildName = $($BuildTemplateName).Split(".")[1] $InstallPassword = [System.GUID]::NewGuid().ToString().ToUpper() -switch ($BuildTemplateName) { - "build.windows-2019.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2019-Datacenter" } - "build.windows-2022.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2022-Datacenter" } - "build.windows-2025.pkr.hcl" { $imageURN = "MicrosoftWindowsServer:WindowsServer:2025-Datacenter" } - "build.ubuntu-22_04.pkr.hcl" { $imageURN = "canonical:0001-com-ubuntu-server-jammy:22_04-lts" } - "build.ubuntu-24_04.pkr.hcl" { $imageURN = "canonical:ubuntu-24_04-lts:server-gen1" } -} - $SensitiveData = @( 'OSType', 'StorageAccountLocation', @@ -62,9 +55,7 @@ packer build -only "$buildName*" ` -var "client_secret=$ClientSecret" ` -var "install_password=$InstallPassword" ` -var "location=$Location" ` - -var "image_publisher=$($imageURN.Split(":")[0])" ` - -var "image_offer=$($imageURN.Split(":")[1])" ` - -var "image_sku=$($imageURN.Split(":")[2])" ` + -var "image_os=$ImageOS" ` -var "managed_image_name=$ImageName" ` -var "managed_image_resource_group_name=$ImageResourceGroupName" ` -var "subscription_id=$SubscriptionId" ` diff --git a/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl index b649ce4745..180fbf6d60 100644 --- a/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-22_04.pkr.hcl @@ -71,7 +71,7 @@ build { } provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" scripts = ["${path.root}/../scripts/build/configure-environment.sh"] } diff --git a/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl index 9d4f946543..6aba4e71fc 100644 --- a/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl +++ b/images/ubuntu/templates/build.ubuntu-24_04.pkr.hcl @@ -71,7 +71,7 @@ build { } provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "HELPER_SCRIPTS=${var.helper_script_folder}"] execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" scripts = ["${path.root}/../scripts/build/configure-environment.sh"] } diff --git a/images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl b/images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl deleted file mode 100644 index e66ea89bdd..0000000000 --- a/images/ubuntu/templates/build.ubuntu-minimal.pkr.hcl +++ /dev/null @@ -1,171 +0,0 @@ -source "azure-arm" "ubuntu-minimal_image" { - location = "${var.location}" - - // Auth - tenant_id = "${var.tenant_id}" - subscription_id = "${var.subscription_id}" - client_id = "${var.client_id}" - client_secret = "${var.client_secret}" - client_cert_path = "${var.client_cert_path}" - - // Base image - image_offer = "0001-com-ubuntu-server-jammy" - image_publisher = "canonical" - image_sku = "22_04-lts" - - // Target location - managed_image_name = "${local.managed_image_name}" - managed_image_resource_group_name = "${var.managed_image_resource_group_name}" - - // Resource group for VM - build_resource_group_name = "${var.build_resource_group_name}" - temp_resource_group_name = "${var.temp_resource_group_name}" - - // Networking for VM - private_virtual_network_with_public_ip = "${var.private_virtual_network_with_public_ip}" - virtual_network_resource_group_name = "${var.virtual_network_resource_group_name}" - virtual_network_name = "${var.virtual_network_name}" - virtual_network_subnet_name = "${var.virtual_network_subnet_name}" - allowed_inbound_ip_addresses = "${var.allowed_inbound_ip_addresses}" - - // VM Configuration - vm_size = "${var.vm_size}" - os_disk_size_gb = "75" - os_type = "Linux" - - dynamic "azure_tag" { - for_each = var.azure_tags - content { - name = azure_tag.key - value = azure_tag.value - } - } -} - -build { - sources = ["source.azure-arm.ubuntu-minimal_image"] - - // Create folder to store temporary data - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["mkdir ${local.image_folder}", "chmod 777 ${local.image_folder}"] - } - - provisioner "file" { - destination = "${local.helper_script_folder}" - source = "${path.root}/../scripts/helpers" - } - - // Add apt wrapper to implement retries - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - script = "${path.root}/../scripts/build/configure-apt-mock.sh" - } - - // Install MS package repos, Configure apt - provisioner "shell" { - environment_vars = ["HELPER_SCRIPTS=${local.helper_script_folder}","DEBIAN_FRONTEND=noninteractive"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = [ - "${path.root}/../scripts/build/install-ms-repos.sh", - "${path.root}/../scripts/build/configure-apt.sh" - ] - } - - // Configure limits - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - script = "${path.root}/../scripts/build/configure-limits.sh" - } - - provisioner "file" { - destination = "${local.installer_script_folder}" - source = "${path.root}/../scripts/build" - } - - provisioner "file" { - destination = "${local.image_folder}" - sources = [ - "${path.root}/../assets/post-gen", - "${path.root}/../scripts/tests" - ] - } - - provisioner "file" { - destination = "${local.installer_script_folder}/toolset.json" - source = "${path.root}/../toolsets/${local.toolset_file_name}" - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["mv ${local.image_folder}/post-gen ${local.image_folder}/post-generation"] - } - - // Generate image data file - provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGEDATA_FILE=${local.imagedata_file}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-image-data.sh"] - } - - // Create /etc/environment, configure waagent etc. - provisioner "shell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.ubuntu_minimal_image_os}", "HELPER_SCRIPTS=${local.helper_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-environment.sh"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/install-apt-vital.sh"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/install-powershell.sh"] - } - - provisioner "shell" { - environment_vars = ["HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} pwsh -f {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/Install-PowerShellModules.ps1"] - } - - provisioner "shell" { - environment_vars = ["DEBIAN_FRONTEND=noninteractive", "HELPER_SCRIPTS=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = [ - "${path.root}/../scripts/build/install-git.sh", - "${path.root}/../scripts/build/install-git-lfs.sh", - "${path.root}/../scripts/build/install-github-cli.sh", - "${path.root}/../scripts/build/install-zstd.sh" - ] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - expect_disconnect = true - inline = ["echo 'Reboot VM'", "sudo reboot"] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - pause_before = "1m0s" - scripts = ["${path.root}/../scripts/build/cleanup.sh"] - start_retry_timeout = "10m" - } - - provisioner "shell" { - environment_vars = ["HELPER_SCRIPT_FOLDER=${local.helper_script_folder}", "INSTALLER_SCRIPT_FOLDER=${local.installer_script_folder}", "IMAGE_FOLDER=${local.image_folder}"] - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - scripts = ["${path.root}/../scripts/build/configure-system.sh"] - } - - provisioner "shell" { - execute_command = "sudo sh -c '{{ .Vars }} {{ .Path }}'" - inline = ["sleep 30", "/usr/sbin/waagent -force -deprovision+user && export HISTSIZE=0 && sync"] - } - -} diff --git a/images/ubuntu/templates/locals.ubuntu.pkr.hcl b/images/ubuntu/templates/locals.ubuntu.pkr.hcl index 407a703900..fe9b189aba 100644 --- a/images/ubuntu/templates/locals.ubuntu.pkr.hcl +++ b/images/ubuntu/templates/locals.ubuntu.pkr.hcl @@ -1,21 +1,18 @@ locals { - image_properties = var.image_sku == "22_04-lts" ? { - image_os = "ubuntu22" - os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 75 - } : { - image_os = "ubuntu24" - os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 75 - } -} - -// Define local variables for the ubuntu-minimal build -locals { - ubuntu_minimal_image_os = "ubuntu22" - - toolset_file_name = "toolset-2204.json" + image_properties_map = { + "ubuntu22" = { + publisher = "canonical" + offer = "0001-com-ubuntu-server-jammy" + sku = "22_04-lts" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 75) + }, + "ubuntu24" = { + publisher = "canonical" + offer = "ubuntu-24_04-lts" + sku = "server-gen1" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 75) + } + } - image_folder = "/imagegeneration" - helper_script_folder = "/imagegeneration/helpers" - installer_script_folder = "/imagegeneration/installers" - imagedata_file = "/imagegeneration/imagedata.json" + image_properties = local.image_properties_map[var.image_os] } diff --git a/images/ubuntu/templates/source.ubuntu.pkr.hcl b/images/ubuntu/templates/source.ubuntu.pkr.hcl index 90936f39eb..4080b9b861 100644 --- a/images/ubuntu/templates/source.ubuntu.pkr.hcl +++ b/images/ubuntu/templates/source.ubuntu.pkr.hcl @@ -11,9 +11,10 @@ source "azure-arm" "image" { allowed_inbound_ip_addresses = var.allowed_inbound_ip_addresses build_resource_group_name = var.build_resource_group_name - image_offer = var.image_offer - image_publisher = var.image_publisher - image_sku = var.image_sku + image_offer = local.image_properties.offer + image_publisher = local.image_properties.publisher + image_sku = local.image_properties.sku + image_version = var.source_image_version location = var.location managed_image_name = var.managed_image_name managed_image_resource_group_name = var.managed_image_resource_group_name diff --git a/images/ubuntu/templates/variable.ubuntu.pkr.hcl b/images/ubuntu/templates/variable.ubuntu.pkr.hcl index 83962df71b..05ba858770 100644 --- a/images/ubuntu/templates/variable.ubuntu.pkr.hcl +++ b/images/ubuntu/templates/variable.ubuntu.pkr.hcl @@ -70,22 +70,10 @@ variable "gallery_storage_account_type" { type = string default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" } -variable "image_offer" { - type = string - default = "" -} variable "image_os_type" { type = string default = "Linux" } -variable "image_publisher" { - type = string - default = "" -} -variable "image_sku" { - type = string - default = "" -} variable "location" { type = string default = "" @@ -110,6 +98,10 @@ variable "os_disk_size_gb" { type = number default = 0 } +variable "source_image_version" { + type = string + default = "latest" +} variable "temp_resource_group_name" { type = string default = "${env("TEMP_RESOURCE_GROUP_NAME")}" diff --git a/images/windows/templates/build.windows-2019.pkr.hcl b/images/windows/templates/build.windows-2019.pkr.hcl index c35048c3a9..3039664921 100644 --- a/images/windows/templates/build.windows-2019.pkr.hcl +++ b/images/windows/templates/build.windows-2019.pkr.hcl @@ -69,7 +69,7 @@ build { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/build.windows-2022.pkr.hcl b/images/windows/templates/build.windows-2022.pkr.hcl index f7649b372c..ff84335156 100644 --- a/images/windows/templates/build.windows-2022.pkr.hcl +++ b/images/windows/templates/build.windows-2022.pkr.hcl @@ -58,7 +58,7 @@ build { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/build.windows-2025.pkr.hcl b/images/windows/templates/build.windows-2025.pkr.hcl index 50368d0bc3..fbb1ea9333 100644 --- a/images/windows/templates/build.windows-2025.pkr.hcl +++ b/images/windows/templates/build.windows-2025.pkr.hcl @@ -58,7 +58,7 @@ provisioner "powershell" { } provisioner "powershell" { - environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${local.image_properties.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] + environment_vars = ["IMAGE_VERSION=${var.image_version}", "IMAGE_OS=${var.image_os}", "AGENT_TOOLSDIRECTORY=${var.agent_tools_directory}", "IMAGEDATA_FILE=${var.imagedata_file}", "IMAGE_FOLDER=${var.image_folder}", "TEMP_DIR=${var.temp_dir}"] execution_policy = "unrestricted" scripts = [ "${path.root}/../scripts/build/Configure-WindowsDefender.ps1", diff --git a/images/windows/templates/locals.windows.pkr.hcl b/images/windows/templates/locals.windows.pkr.hcl index fbadd7c166..d03ab45244 100644 --- a/images/windows/templates/locals.windows.pkr.hcl +++ b/images/windows/templates/locals.windows.pkr.hcl @@ -1,12 +1,24 @@ locals { - image_properties = var.image_sku == "2019-Datacenter" ? { - image_os = "win19" - os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 256 - } : var.image_sku == "2022-Datacenter" ? { - image_os = "win22" - os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 256 - } : { - image_os = "win25" - os_disk_size_gb = var.os_disk_size_gb != 0 ? var.os_disk_size_gb : 150 - } + image_properties_map = { + "win19" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2019-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 256) + }, + "win22" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2022-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 256) + }, + "win25" = { + publisher = "MicrosoftWindowsServer" + offer = "WindowsServer" + sku = "2025-Datacenter" + os_disk_size_gb = coalesce(var.os_disk_size_gb, 150) + } + } + + image_properties = local.image_properties_map[var.image_os] } diff --git a/images/windows/templates/source.windows.pkr.hcl b/images/windows/templates/source.windows.pkr.hcl index 19cfadc71a..36aac4035f 100644 --- a/images/windows/templates/source.windows.pkr.hcl +++ b/images/windows/templates/source.windows.pkr.hcl @@ -14,9 +14,10 @@ source "azure-arm" "image" { build_key_vault_secret_name = var.build_key_vault_secret_name build_resource_group_name = var.build_resource_group_name communicator = "winrm" - image_offer = var.image_offer - image_publisher = var.image_publisher - image_sku = var.image_sku + image_offer = local.image_properties.offer + image_publisher = local.image_properties.publisher + image_sku = local.image_properties.sku + image_version = var.source_image_version location = var.location managed_image_name = var.managed_image_name managed_image_resource_group_name = var.managed_image_resource_group_name diff --git a/images/windows/templates/variable.windows.pkr.hcl b/images/windows/templates/variable.windows.pkr.hcl index 17ce8745d8..daf69c66fb 100644 --- a/images/windows/templates/variable.windows.pkr.hcl +++ b/images/windows/templates/variable.windows.pkr.hcl @@ -78,22 +78,10 @@ variable "gallery_storage_account_type" { type = string default = "${env("GALLERY_STORAGE_ACCOUNT_TYPE")}" } -variable "image_offer" { - type = string - default = "" -} variable "image_os_type" { type = string default = "Windows" } -variable "image_publisher" { - type = string - default = "" -} -variable "image_sku" { - type = string - default = "" -} variable "location" { type = string default = "" @@ -118,6 +106,10 @@ variable "os_disk_size_gb" { type = number default = 0 } +variable "source_image_version" { + type = string + default = "latest" +} variable "temp_resource_group_name" { type = string default = "${env("TEMP_RESOURCE_GROUP_NAME")}" From 2724394d1e58d20602455416b014dcfbdf413e09 Mon Sep 17 00:00:00 2001 From: Shamil Mubarakshin <127750046+shamil-mubarakshin@users.noreply.github.com> Date: Tue, 24 Jun 2025 10:04:05 +0000 Subject: [PATCH 3/3] Update GenerateResourcesAndImage.ps1 and docs --- docs/create-image-and-azure-resources.md | 12 ++++--- helpers/GenerateResourcesAndImage.ps1 | 43 +++++++++++++++--------- 2 files changed, 35 insertions(+), 20 deletions(-) diff --git a/docs/create-image-and-azure-resources.md b/docs/create-image-and-azure-resources.md index b4ef500839..36e74926d8 100644 --- a/docs/create-image-and-azure-resources.md +++ b/docs/create-image-and-azure-resources.md @@ -76,7 +76,6 @@ In any case, you will need these software installed: This repository includes a script that assists in generating images in Azure. All you need is an Azure subscription, a resource group in that subscription and a build agent configured as described above. -We suggest starting with building the UbuntuMinimal image because it includes only basic software and builds in less than 30 minutes. All the commands below should be executed in PowerShell. @@ -99,7 +98,7 @@ Finally, run the `GenerateResourcesAndImage` function, setting the mandatory arg - `ResourceGroupName` - the name of the resource group that will store the resulting artifact (e.g., "imagegen-test"). The resource group must already exist in your Azure subscription; - `AzureLocation` - the location where resources will be created (e.g., "East US"); -- `ImageType` - the type of image to build (we suggest choosing "UbuntuMinimal" here; other valid options are "Windows2019", "Windows2022", "Windows2025", "Ubuntu2204", "Ubuntu2404"). +- `ImageType` - the type of image to build (valid options are "Windows2019", "Windows2022", "Windows2025", "Ubuntu2204", "Ubuntu2404"). This function automatically creates all required Azure resources and initiates the Packer image generation for the selected image type. @@ -200,11 +199,14 @@ Then, you can invoke Packer in your CI/CD pipeline using the following commands: ```powershell packer plugins install github.com/hashicorp/azure 2.2.1 -packer build -var "subscription_id=$SubscriptionId" ` + +packer build -only "$BuildName*" ` + -var "subscription_id=$SubscriptionId" ` -var "client_id=$ClientId" ` -var "client_secret=$ClientSecret" ` -var "install_password=$InstallPassword" ` -var "location=$Location" ` + -var "image_os=$ImageOS" ` -var "managed_image_name=$ImageName" ` -var "managed_image_resource_group_name=$ImageResourceGroupName" ` -var "tenant_id=$TenantId" ` @@ -213,13 +215,15 @@ packer build -var "subscription_id=$SubscriptionId" ` Where: +- `BuildName` - name of the build defined in Packer template's `build{}` block (e.g. "ubuntu-24_04", "windows-2025"); - `SubscriptionId` - your Azure Subscription ID; - `ClientId` and `ClientSecret` - Service Principal credentials; - `TenantId` - Azure Tenant ID; - `InstallPassword` - password for the user used to install software (Windows only); - `Location` - location where resources will be created (e.g., "East US"); +- `ImageOS` - the type of OS that will be deployed as a temporary VM (e.g. "ubuntu24", "win25"); - `ImageName` and `ImageResourceGroupName` - name of the resource group where the managed image will be stored; -- `TemplatePath` - path to the Packer template file (e.g., "images/windows/templates/windows-2022.pkr.hcl"). +- `TemplatePath` - path to the folder with Packer template files (e.g., "images/windows/templates"). ### Required variables diff --git a/helpers/GenerateResourcesAndImage.ps1 b/helpers/GenerateResourcesAndImage.ps1 index c2dea768d1..592313e7d5 100644 --- a/helpers/GenerateResourcesAndImage.ps1 +++ b/helpers/GenerateResourcesAndImage.ps1 @@ -6,10 +6,9 @@ enum ImageType { Windows2025 = 3 Ubuntu2204 = 4 Ubuntu2404 = 5 - UbuntuMinimal = 6 } -Function Get-PackerTemplatePath { +Function Get-PackerTemplate { param ( [Parameter(Mandatory = $True)] [string] $RepositoryRoot, @@ -20,33 +19,41 @@ Function Get-PackerTemplatePath { switch ($ImageType) { # Note: Double Join-Path is required to support PowerShell 5.1 ([ImageType]::Windows2019) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2019.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2019.pkr.hcl" + $imageOS = "win19" } ([ImageType]::Windows2022) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2022.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2022.pkr.hcl" + $imageOS = "win22" } ([ImageType]::Windows2025) { - $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "windows-2025.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "windows" "templates") "build.windows-2025.pkr.hcl" + $imageOS = "win25" } ([ImageType]::Ubuntu2204) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-22.04.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "build.ubuntu-22_04.pkr.hcl" + $imageOS = "ubuntu22" } ([ImageType]::Ubuntu2404) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-24.04.pkr.hcl" - } - ([ImageType]::UbuntuMinimal) { - $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "ubuntu-minimal.pkr.hcl" + $relativeTemplatePath = Join-Path (Join-Path "ubuntu" "templates") "build.ubuntu-24_04.pkr.hcl" + $imageOS = "ubuntu24" } default { throw "Unknown type of image" } } $imageTemplatePath = [IO.Path]::Combine($RepositoryRoot, "images", $relativeTemplatePath) + # Specific template selection using Packer's "-only" functionality + $buildName = [IO.Path]::GetFileName($imageTemplatePath).Split(".")[1] if (-not (Test-Path $imageTemplatePath)) { throw "Template for image '$ImageType' doesn't exist on path '$imageTemplatePath'." } - return $imageTemplatePath; + return [PSCustomObject] @{ + "BuildName" = $buildName + "ImageOS" = $imageOS + "Path" = [IO.Path]::GetDirectoryName($imageTemplatePath) + } } Function Show-LatestCommit { @@ -81,7 +88,7 @@ Function GenerateResourcesAndImage { .PARAMETER ResourceGroupName The name of the resource group to store the resulting artifact. Resource group must already exist. .PARAMETER ImageType - The type of image to generate. Valid values are: Windows2019, Windows2022, Windows2025, Ubuntu2204, Ubuntu2404, UbuntuMinimal. + The type of image to generate. Valid values are: Windows2019, Windows2022, Windows2025, Ubuntu2204, Ubuntu2404. .PARAMETER ManagedImageName The name of the managed image to create. The default is "Runner-Image-{{ImageType}}". .PARAMETER AzureLocation @@ -155,8 +162,8 @@ Function GenerateResourcesAndImage { } # Get template path - $TemplatePath = Get-PackerTemplatePath -RepositoryRoot $ImageGenerationRepositoryRoot -ImageType $ImageType - Write-Debug "Template path: $TemplatePath." + $PackerTemplate = Get-PackerTemplate -RepositoryRoot $ImageGenerationRepositoryRoot -ImageType $ImageType + Write-Debug "Template path: $($PackerTemplate.Path)." # Prepare list of allowed inbound IP addresses if ($RestrictToAgentIpAddress) { @@ -208,17 +215,19 @@ Function GenerateResourcesAndImage { Write-Host "Validating packer template..." & $PackerBinary validate ` + "-only=$($PackerTemplate.BuildName)*" ` "-var=client_id=fake" ` "-var=client_secret=fake" ` "-var=subscription_id=$($SubscriptionId)" ` "-var=tenant_id=fake" ` "-var=location=$($AzureLocation)" ` + "-var=image_os=$($PackerTemplate.ImageOS)" ` "-var=managed_image_name=$($ManagedImageName)" ` "-var=managed_image_resource_group_name=$($ResourceGroupName)" ` "-var=install_password=$($InstallPassword)" ` "-var=allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` "-var=azure_tags=$($TagsJson)" ` - $TemplatePath + $PackerTemplate.Path if ($LastExitCode -ne 0) { throw "Packer template validation failed." @@ -276,17 +285,19 @@ Function GenerateResourcesAndImage { Write-Debug "Tenant id: $TenantId." & $PackerBinary build -on-error="$($OnError)" ` + -only "$($PackerTemplate.BuildName)*" ` -var "client_id=$($ServicePrincipalAppId)" ` -var "client_secret=$($ServicePrincipalPassword)" ` -var "subscription_id=$($SubscriptionId)" ` -var "tenant_id=$($TenantId)" ` -var "location=$($AzureLocation)" ` + -var "image_os=$($PackerTemplate.ImageOS)" ` -var "managed_image_name=$($ManagedImageName)" ` -var "managed_image_resource_group_name=$($ResourceGroupName)" ` -var "install_password=$($InstallPassword)" ` -var "allowed_inbound_ip_addresses=$($AllowedInboundIpAddresses)" ` -var "azure_tags=$($TagsJson)" ` - $TemplatePath + $PackerTemplate.Path if ($LastExitCode -ne 0) { throw "Failed to build image."