@@ -258,14 +258,21 @@ function create_new_base_image() {
258258
259259 # Use imported sig image to create the build VM
260260 WINDOWS_IMAGE_URL=" "
261- windows_sigmode_source_subscription_id=$SUBSCRIPTION_ID
262- windows_sigmode_source_resource_group_name=$AZURE_RESOURCE_GROUP_NAME
263- windows_sigmode_source_gallery_name=$SIG_GALLERY_NAME
264- windows_sigmode_source_image_name=$IMPORTED_IMAGE_NAME
265- windows_sigmode_source_image_version=" 1.0.0"
261+ windows_sigmode_source_id=" /subscriptions/${SUBSCRIPTION_ID} /resourceGroups/${AZURE_RESOURCE_GROUP_NAME} /providers/Microsoft.Compute/galleries/${SIG_GALLERY_NAME} /images/${IMPORTED_IMAGE_NAME} /versions/1.0.0"
266262}
267263
268264function prepare_windows_vhd() {
265+ local skip_vhd
266+ skip_vhd=$( jq -r " .WindowsBaseVersions.\" ${WINDOWS_SKU} \" .skip_vhd // false" < $CDIR /windows/windows_settings.json)
267+ if [ " ${skip_vhd} " = " true" ]; then
268+ echo " skip_vhd is set to true for ${WINDOWS_SKU} , skipping VHD build."
269+ echo " ##vso[task.setvariable variable=skipping_vhd_build;isOutput=true]true"
270+ echo " ##vso[task.setvariable variable=skipping_vhd_build]true"
271+ exit 0
272+ fi
273+ echo " ##vso[task.setvariable variable=skipping_vhd_build;isOutput=true]false"
274+ echo " ##vso[task.setvariable variable=skipping_vhd_build]false"
275+
269276 echo " Set the base image sku and version from windows_settings.json"
270277
271278 WINDOWS_IMAGE_SKU=$( jq -r " .WindowsBaseVersions.\" ${WINDOWS_SKU} \" .base_image_sku" < $CDIR /windows/windows_settings.json)
@@ -309,12 +316,59 @@ function prepare_windows_vhd() {
309316 exit 1
310317 fi
311318
312- # Create the sig image from the official images defined in windows-settings.json by default
313- windows_sigmode_source_subscription_id=" "
314- windows_sigmode_source_resource_group_name=" "
315- windows_sigmode_source_gallery_name=" "
316- windows_sigmode_source_image_name=" "
317- windows_sigmode_source_image_version=" "
319+ # By default, packer uses marketplace source (image_publisher/offer/sku/version).
320+ # For VHD imports, we use shared_image_gallery.id (full ARM resource ID).
321+ # For embargo builds, we use direct_shared_gallery_image_id to source from a 1P shared gallery.
322+ windows_sigmode_source_id=" "
323+ windows_sigmode_direct_shared_gallery_image_id=" "
324+
325+ local sig_source_gallery_name
326+ if sig_source_gallery_name=$( jq -re " .WindowsBaseVersions.\" ${WINDOWS_SKU} \" .sig_source_gallery_name" < $CDIR /windows/windows_settings.json) ; then
327+ if [ -n " ${sig_source_gallery_name} " ] && [ " ${sig_source_gallery_name} " != " null" ]; then
328+ local sig_image_name=" ${WINDOWS_IMAGE_SKU} "
329+ # Use AZURE_LOCATION for gallery queries — PACKER_BUILD_LOCATION may not be normalized yet for Windows
330+ local gallery_location=" ${AZURE_LOCATION:- ${PACKER_BUILD_LOCATION} } "
331+
332+ # List latest 3 available versions for this image in the shared gallery
333+ echo " Latest 3 available versions in gallery:"
334+ az sig image-version list-shared \
335+ --gallery-unique-name " ${sig_source_gallery_name} " \
336+ --gallery-image-definition " ${sig_image_name} " \
337+ --location " ${gallery_location} " \
338+ --shared-to tenant \
339+ --query " [-3:].name" -o tsv | while read -r ver; do
340+ echo " - ${ver} "
341+ done
342+
343+ # Resolve version dynamically if base_image_version is empty
344+ if [ -z " ${WINDOWS_IMAGE_VERSION} " ] || [ " ${WINDOWS_IMAGE_VERSION} " = " null" ]; then
345+ echo " base_image_version is empty, resolving latest from shared gallery ${sig_source_gallery_name} /${sig_image_name} ..."
346+ WINDOWS_IMAGE_VERSION=$( az sig image-version list-shared \
347+ --gallery-unique-name " ${sig_source_gallery_name} " \
348+ --gallery-image-definition " ${sig_image_name} " \
349+ --location " ${gallery_location} " \
350+ --shared-to tenant \
351+ --query " sort_by(@, &name)[-1].name" -o tsv)
352+ if [ -z " ${WINDOWS_IMAGE_VERSION} " ]; then
353+ echo " ERROR: Failed to resolve latest image version from gallery ${sig_source_gallery_name} /${sig_image_name} "
354+ exit 1
355+ fi
356+ echo " Resolved base_image_version: ${WINDOWS_IMAGE_VERSION} "
357+ fi
358+
359+ windows_sigmode_direct_shared_gallery_image_id=" /SharedGalleries/${sig_source_gallery_name} /Images/${sig_image_name} /Versions/${WINDOWS_IMAGE_VERSION} "
360+ # Clear marketplace and raw VHD source fields — packer requires exactly one source type
361+ WINDOWS_IMAGE_URL=" "
362+ WINDOWS_BASE_IMAGE_URL=" "
363+ WINDOWS_IMAGE_PUBLISHER=" "
364+ WINDOWS_IMAGE_OFFER=" "
365+ WINDOWS_IMAGE_SKU=" "
366+ WINDOWS_IMAGE_VERSION=" "
367+ echo " Using direct shared gallery source:"
368+ echo " ID: ${windows_sigmode_direct_shared_gallery_image_id} "
369+
370+ fi
371+ fi
318372
319373 # default: build VHD images from a marketplace base image
320374 export AZCOPY_AUTO_LOGIN_TYPE=" AZCLI" # use AZCLI for AzCopy authentication
@@ -323,8 +377,8 @@ function prepare_windows_vhd() {
323377 mkdir -p " ${AZCOPY_LOG_LOCATION} "
324378 mkdir -p " ${AZCOPY_JOB_PLAN_LOCATION} "
325379
326- echo " VALID IMAGE URL: ${WINDOWS_CONTAINERIMAGE_JSON_URL} "
327380 if [ -n " ${WINDOWS_CONTAINERIMAGE_JSON_URL} " ]; then
381+ echo " VALID IMAGE URL: ${WINDOWS_CONTAINERIMAGE_JSON_URL} "
328382 download_windows_json_artifact
329383 extract_windows_image_urls
330384 else
@@ -333,14 +387,14 @@ function prepare_windows_vhd() {
333387 fi
334388
335389 # Check if base, nano, and servercore urls are set
336- if [ -z " ${windows_nanoserver_image_url} " ] || [ -z " ${windows_servercore_image_url} " ] || [ -z " ${WINDOWS_BASE_IMAGE_URL} " ]; then
337- echo " Error: One of the Windows image URLs are not set."
390+ if [ -n " ${windows_nanoserver_image_url} " ] && [ -n " ${windows_servercore_image_url} " ] && [ -n " ${WINDOWS_BASE_IMAGE_URL} " ]; then
391+ echo " All Windows image URLs are set."
338392 else
339- # If all URLs are set, print them
340- echo " Using Windows base image URL: ${WINDOWS_BASE_IMAGE_URL} "
341- echo " Using Windows Nano Server image URL: ${windows_nanoserver_image_url} "
342- echo " Using Windows Server Core image URL: ${windows_servercore_image_url} "
393+ echo " At least one of the Windows image URLs are not set:"
343394 fi
395+ echo " Windows base image URL: ${WINDOWS_BASE_IMAGE_URL} "
396+ echo " Windows Nano Server image URL: ${windows_nanoserver_image_url} "
397+ echo " Windows Server Core image URL: ${windows_servercore_image_url} "
344398
345399 # build from a pre-supplied VHD blob a.k.a. external raw VHD
346400 if [ -n " ${WINDOWS_BASE_IMAGE_URL} " ]; then
0 commit comments