Search code examples
azureazure-image-builder

Download large files in Azure image builder process


We recently implemented Azure image builder for our image solution, everything is working fine manually.

  1. in AIB template customize, I am creating a new folder, downloading azcopy.exe.
  2. in second customize, I am passing a sas token and az copy cmd to download the large file from blob to new folder.
  3. and another ps script that runs at the end.

Now when planned to integrate with ADO- pipelines, I am stuck at one step. in ADO task, I am generating the sas token to pass as a parameter, but how to pass the token inside the powershell inline script.

  1. Customizer to download Azcopy. `

{

                    "type": "PowerShell",

                    "name": "GetAzCopy",

                    "inline": [

                    "New-Item -Type Directory -Path 'c:\\' -Name apps",
                    "invoke-webrequest -uri 'https://aka.ms/downloadazcopy-v10-windows' -OutFile 'c:\\apps\\azcopy.zip'",

                    "Expand-Archive 'c:\\apps\\azcopy.zip' 'c:\\apps'",

                    "copy-item 'C:\\apps\\azcopy_windows_amd64_*\\azcopy.exe\\' -Destination 'c:\\apps'"

                    ]

                },`
  1. Customizer to download large files from SAS uri.

`{

"type": "PowerShell",

"name": "downloadapps",

"inline": [
                     
 "c:\\apps\\azcopy.exe copy "[parameters('Sasuri')]" c:\\apps\\AVDapps.zip",
 "Expand-Archive 'c:\\apps\\AVDapps.zip' c:\\apps"
           ]

} `

Please review the whole json. `{

"$schema": "http://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",

"contentVersion": "1.0.0.0",

"parameters": {
    "imageTemplateName": {
        "type": "string"
    },

     "Sasuri": {
  "type": "string"
}
},

"variables": {},

"resources": [
    {
        "name": "[parameters('imageTemplateName')]",
        "type": "Microsoft.VirtualMachineImages/imageTemplates",
        "apiVersion": "2021-10-01",
        "location": "West Europe",
        "dependsOn": [],
        "tags": {
            "imagebuilderTemplate": "win10Pooled",
            "userIdentity": "enabled"
        },

        "identity": {
            "type": "UserAssigned",
            "userAssignedIdentities": {
                "identity{}"
            }
        },

        "properties": {
            "buildTimeoutInMinutes": 120,
            "vmProfile": {
                "vmSize": "Standard_D2_v3",
                "osDiskSizeGB": 127,
                "vnetConfig": {
                    "name": "networkname",
                    "subnetName": "subnetname",
                    "resourceGroupName": "networkresourcegroup",
                    "subnetId": "subnetid"
                }
            },

            "source": {
                "type": "PlatformImage",
                "publisher": "MicrosoftWindowsDesktop",
                "offer": "office-365",
                "sku": "win10-21h2-avd-m365",
                "version": "latest"
            },

            "customize": [
                
                {
                    "type": "PowerShell",
                    "name": "GetAzCopy",
                    "inline": [
                    "New-Item -Type Directory -Path 'c:\\' -Name apps",
                    "invoke-webrequest -uri 'https://aka.ms/downloadazcopy-v10-windows' -OutFile 'c:\\apps\\azcopy.zip'",
                    "Expand-Archive 'c:\\apps\\azcopy.zip' 'c:\\apps'",
                    "copy-item 'C:\\apps\\azcopy_windows_amd64_*\\azcopy.exe\\' -Destination 'c:\\apps'"
                    ]
                },
                {
                        "type": "PowerShell",
                        "name": "GetArchive",
                        "inline": [
                         
                        "c:\\apps\\azcopy.exe copy "[parameters('Fileuri')]" c:\\apps\\AVDapps.zip",
                        "Expand-Archive 'c:\\apps\\AVDapps.zip' c:\\apps"
                        ]
                }
                
            ],
            "distribute": [
                {
                    "type": "SharedImage",
                    "galleryImageId": "galleryid",
                    "location": "westeurope",
                    "runOutputName": "win10Client",
                    "artifactTags": {
                        "source": "azVmImageBuilder",
                        "baseosimg": "windows10Pooled"
                    },
                    "replicationRegions": [
                        "westeurope"
                    ]
                }
            ]
        }
    }
]}

`

Thank you. Naveen.

  1. I tried passing sas uri as a parameter to the inline powershell script or tried invoke-webrequest command as mentioned in the MSdoc
  2. In pipeline itself tried to replace the value with the output of the earlier task with no luck. As I mentioned, entering SAS token manually works fine without any issues.

Solution

  • Finally I got it working :) Instead of passing sas URI as a parameter, I have used a powershell task to replace the text in the ARM template in ADO pipeline.

    ((Get-Content -path $MyFile -Encoding UTF8 ) -replace 'placewhereuneedtoplaceuri',"'FileURI'") | Set-Content $MyFile

    It was so simple, I was running around all posts to pass/override the parameters instead replacing the text with the uri worked for me. Post replacing, deploying the template without override parameters initiated the deployment as needed.