ARM şablonlarında dağıtım betikleri için geliştirme ortamını yapılandırma

Dağıtım betiği görüntüsüyle ARM şablonu dağıtım betikleri geliştirmek ve test etmek için bir geliştirme ortamı oluşturmayı öğrenin. Azure kapsayıcı örneği oluşturabilir veya Docker kullanabilirsiniz. Her iki seçenek de bu makalede ele alınmıştır.

Önkoşullar

Azure PowerShell kapsayıcısı

Azure PowerShell dağıtım betiğiniz yoksa, aşağıdaki içeriği kullanarak bir hello.ps1 dosyası oluşturabilirsiniz:

param([string] $name)
$output = 'Hello {0}' -f $name
Write-Output $output
$DeploymentScriptOutputs = @{}
$DeploymentScriptOutputs['text'] = $output

Azure CLI kapsayıcısı

Azure CLI kapsayıcı görüntüsü için aşağıdaki içeriği kullanarak bir hello.sh dosyası oluşturabilirsiniz:

FIRSTNAME=$1
LASTNAME=$2
OUTPUT="{\"name\":{\"displayName\":\"$FIRSTNAME $LASTNAME\",\"firstName\":\"$FIRSTNAME\",\"lastName\":\"$LASTNAME\"}}"
echo -n "Hello "
echo $OUTPUT | jq -r '.name.displayName'

Dekont

Azure CLI dağıtım betiğini çalıştırdığınızda, adlı AZ_SCRIPTS_OUTPUT_PATH bir ortam değişkeni betik çıkış dosyasının konumunu depolar. Ortam değişkeni geliştirme ortamı kapsayıcısında kullanılamaz. Azure CLI çıkışlarıyla çalışma hakkında daha fazla bilgi için bkz . CLI betiğinden gelen çıkışlarla çalışma.

Azure PowerShell kapsayıcı örneğini kullanma

Betiklerinizi bilgisayarınızda yazmak için bir depolama hesabı oluşturmanız ve depolama hesabını kapsayıcı örneğine bağlamanız gerekir. Böylece betiğinizi depolama hesabına yükleyebilir ve kapsayıcı örneğinde çalıştırabilirsiniz.

Dekont

Betiğinizi test etmek için oluşturduğunuz depolama hesabı, dağıtım betiği hizmetinin betiği yürütmek için kullandığı depolama hesabıyla aynı değildir. Dağıtım betiği hizmeti, her yürütmede dosya paylaşımı olarak benzersiz bir ad oluşturur.

Azure PowerShell kapsayıcı örneği oluşturma

Aşağıdaki Azure Resource Manager şablonu (ARM şablonu) bir kapsayıcı örneği ve dosya paylaşımı oluşturur ve ardından dosya paylaşımını kapsayıcı görüntüsüne bağlar.

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specify a project name that is used for generating resource names."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specify the resource location."
      }
    },
    "containerImage": {
      "type": "string",
      "defaultValue": "mcr.microsoft.com/azuredeploymentscripts-powershell:az9.7",
      "metadata": {
        "description": "Specify the container image."
      }
    },
    "mountPath": {
      "type": "string",
      "defaultValue": "/mnt/azscripts/azscriptinput",
      "metadata": {
        "description": "Specify the mount path."
      }
    }
  },
  "variables": {
    "storageAccountName": "[toLower(format('{0}store', parameters('projectName')))]",
    "fileShareName": "[format('{0}share', parameters('projectName'))]",
    "containerGroupName": "[format('{0}cg', parameters('projectName'))]",
    "containerName": "[format('{0}container', parameters('projectName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2023-01-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
      "apiVersion": "2023-01-01",
      "name": "[format('{0}/default/{1}', variables('storageAccountName'), variables('fileShareName'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2023-05-01",
      "name": "[variables('containerGroupName')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[parameters('containerImage')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGB": "[json('1.5')]"
                }
              },
              "ports": [
                {
                  "protocol": "TCP",
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "filesharevolume",
                  "mountPath": "[parameters('mountPath')]"
                }
              ],
              "command": [
                "/bin/sh",
                "-c",
                "pwsh -c 'Start-Sleep -Seconds 1800'"
              ]
            }
          }
        ],
        "osType": "Linux",
        "volumes": [
          {
            "name": "filesharevolume",
            "azureFile": {
              "readOnly": false,
              "shareName": "[variables('fileShareName')]",
              "storageAccountName": "[variables('storageAccountName')]",
              "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2023-01-01').keys[0].value]"
            }
          }
        ]
      },
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    }
  ]
}

Bağlama yolunun varsayılan değeridir /mnt/azscripts/azscriptinput. Bu, kapsayıcı örneğinde dosya paylaşımına bağlandığı yoldur.

Şablonda belirtilen varsayılan kapsayıcı görüntüsü mcr.microsoft.com/azuredeploymentscripts-powershell:az9.7. Desteklenen tüm Azure PowerShell sürümlerinin listesine bakın.

Şablon, kapsayıcı örneğini 1.800 saniye sonra askıya alır. Kapsayıcı örneğinin sonlandırılan duruma geçmesi ve oturumun sona ermesi için 30 dakikanız vardır.

Şablonu dağıtmak için:

$projectName = Read-Host -Prompt "Enter a project name that is used to generate resource names"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateFile = Read-Host -Prompt "Enter the template file path and file name"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroup -Location $location -name $resourceGroupName
New-AzResourceGroupDeployment -resourceGroupName $resourceGroupName -TemplateFile $templatefile -projectName $projectName

Dağıtım betiğini karşıya yükleme

Dağıtım betiğinizi depolama hesabına yükleyin. PowerShell betiği örneği aşağıda verilmiştir:

$projectName = Read-Host -Prompt "Enter the same project name that you used earlier"
$fileName = Read-Host -Prompt "Enter the deployment script file name with the path"

$resourceGroupName = "${projectName}rg"
$storageAccountName = "${projectName}store"
$fileShareName = "${projectName}share"

$context = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
Set-AzStorageFileContent -Context $context -ShareName $fileShareName -Source $fileName -Force

Azure portalını veya Azure CLI'yi kullanarak da dosyayı karşıya yükleyebilirsiniz.

Dağıtım betiğini test edin

  1. Azure portalında kapsayıcı örneğini ve depolama hesabını dağıttığınız kaynak grubunu açın.

  2. Kapsayıcı grubunu açın. Varsayılan kapsayıcı grubu adı, cg ile eklenen proje adıdır. Kapsayıcı örneği Çalışıyor durumundadır.

  3. Kaynak menüsünde Kapsayıcılar'ı seçin. Kapsayıcı örneği adı, kapsayıcıyla birlikte eklenen proje adıdır.

    Screenshot of the deployment script connect container instance option in the Azure portal.

  4. Bağlan'ı ve ardından Bağlan'ı seçin. Kapsayıcı örneğine bağlanamıyorsanız kapsayıcı grubunu yeniden başlatın ve yeniden deneyin.

  5. Konsol bölmesinde aşağıdaki komutları çalıştırın:

    cd /mnt/azscripts/azscriptinput
    ls
    pwsh ./hello.ps1 "John Dole"
    

    Çıkış Hello John Dole'dır.

    Screenshot of the deployment script connect container instance test output displayed in the console.

Azure CLI kapsayıcı örneği kullanma

Betiklerinizi bilgisayarınızda yazmak için bir depolama hesabı oluşturun ve depolama hesabını kapsayıcı örneğine bağlayın. Ardından betiğinizi depolama hesabına yükleyebilir ve betiği kapsayıcı örneğinde çalıştırabilirsiniz.

Dekont

Betiğinizi test etmek için oluşturduğunuz depolama hesabı, dağıtım betiği hizmetinin betiği yürütmek için kullandığı depolama hesabıyla aynı değildir. Dağıtım betiği hizmeti, her yürütmede dosya paylaşımı olarak benzersiz bir ad oluşturur.

Azure CLI kapsayıcı örneği oluşturma

Aşağıdaki ARM şablonu bir kapsayıcı örneği ve dosya paylaşımı oluşturur ve ardından dosya paylaşımını kapsayıcı görüntüsüne bağlar:

{
  "$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
  "contentVersion": "1.0.0.0",
  "parameters": {
    "projectName": {
      "type": "string",
      "metadata": {
        "description": "Specify a project name that is used for generating resource names."
      }
    },
    "location": {
      "type": "string",
      "defaultValue": "[resourceGroup().location]",
      "metadata": {
        "description": "Specify the resource location."
      }
    },
    "containerImage": {
      "type": "string",
      "defaultValue": "mcr.microsoft.com/azure-cli:2.9.1",
      "metadata": {
        "description": "Specify the container image."
      }
    },
    "mountPath": {
      "type": "string",
      "defaultValue": "/mnt/azscripts/azscriptinput",
      "metadata": {
        "description": "Specify the mount path."
      }
    }
  },
  "variables": {
    "storageAccountName": "[toLower(format('{0}store', parameters('projectName')))]",
    "fileShareName": "[format('{0}share', parameters('projectName'))]",
    "containerGroupName": "[format('{0}cg', parameters('projectName'))]",
    "containerName": "[format('{0}container', parameters('projectName'))]"
  },
  "resources": [
    {
      "type": "Microsoft.Storage/storageAccounts",
      "apiVersion": "2022-09-01",
      "name": "[variables('storageAccountName')]",
      "location": "[parameters('location')]",
      "sku": {
        "name": "Standard_LRS"
      },
      "kind": "StorageV2",
      "properties": {
        "accessTier": "Hot"
      }
    },
    {
      "type": "Microsoft.Storage/storageAccounts/fileServices/shares",
      "apiVersion": "2022-09-01",
      "name": "[format('{0}/default/{1}', variables('storageAccountName'), variables('fileShareName'))]",
      "dependsOn": [
        "[resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName'))]"
      ]
    },
    {
      "type": "Microsoft.ContainerInstance/containerGroups",
      "apiVersion": "2023-05-01",
      "name": "[variables('containerGroupName')]",
      "location": "[parameters('location')]",
      "properties": {
        "containers": [
          {
            "name": "[variables('containerName')]",
            "properties": {
              "image": "[parameters('containerImage')]",
              "resources": {
                "requests": {
                  "cpu": 1,
                  "memoryInGB": "[json('1.5')]"
                }
              },
              "ports": [
                {
                  "protocol": "TCP",
                  "port": 80
                }
              ],
              "volumeMounts": [
                {
                  "name": "filesharevolume",
                  "mountPath": "[parameters('mountPath')]"
                }
              ],
              "command": [
                "/bin/bash",
                "-c",
                "echo hello; sleep 1800"
              ]
            }
          }
        ],
        "osType": "Linux",
        "volumes": [
          {
            "name": "filesharevolume",
            "azureFile": {
              "readOnly": false,
              "shareName": "[variables('fileShareName')]",
              "storageAccountName": "[variables('storageAccountName')]",
              "storageAccountKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts', variables('storageAccountName')), '2022-09-01').keys[0].value]"
            }
          }
        ]
      },
      "dependsOn": [
        "storageAccount"
      ]
    }
  ]
}

Bağlama yolunun varsayılan değeridir /mnt/azscripts/azscriptinput. Bu, kapsayıcı örneğinde dosya paylaşımına bağlandığı yoldur.

Şablonda belirtilen varsayılan kapsayıcı görüntüsü mcr.microsoft.com/azure-cli:2.9.1. Desteklenen Azure CLI sürümlerinin listesine bakın.

Önemli

Dağıtım betiği, Microsoft Container Registry'den (MCR) kullanılabilir CLI görüntülerini kullanır. Bir dağıtım betiği için CLI görüntüsünün onaylanması yaklaşık bir ay sürer. 30 gün içinde yayımlanan CLI sürümlerini kullanmayın. Görüntülerin yayın tarihlerini bulmak için bkz . Azure CLI sürüm notları. Desteklenmeyen bir sürüm kullanıyorsanız, hata iletisi desteklenen sürümleri listeler.

Şablon, kapsayıcı örneğini 1.800 saniye sonra askıya alır. Kapsayıcı örneğinin terminal durumuna geçmesi ve oturumun bitmesi için 30 dakikanız vardır.

Şablonu dağıtmak için:

$projectName = Read-Host -Prompt "Enter a project name that is used to generate resource names"
$location = Read-Host -Prompt "Enter the location (i.e. centralus)"
$templateFile = Read-Host -Prompt "Enter the template file path and file name"
$resourceGroupName = "${projectName}rg"

New-AzResourceGroup -Location $location -name $resourceGroupName
New-AzResourceGroupDeployment -resourceGroupName $resourceGroupName -TemplateFile $templatefile -projectName $projectName

Dağıtım betiğini karşıya yükleme

Dağıtım betiğinizi depolama hesabına yükleyin. Aşağıda bir PowerShell örneği verilmiştir:

$projectName = Read-Host -Prompt "Enter the same project name that you used earlier"
$fileName = Read-Host -Prompt "Enter the deployment script file name with the path"

$resourceGroupName = "${projectName}rg"
$storageAccountName = "${projectName}store"
$fileShareName = "${projectName}share"

$context = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccountName).Context
Set-AzStorageFileContent -Context $context -ShareName $fileShareName -Source $fileName -Force

Azure portalını veya Azure CLI'yi kullanarak da dosyayı karşıya yükleyebilirsiniz.

Dağıtım betiğini test edin

  1. Azure portalında kapsayıcı örneğini ve depolama hesabını dağıttığınız kaynak grubunu açın.

  2. Kapsayıcı grubunu açın. Varsayılan kapsayıcı grubu adı, cg ile eklenen proje adıdır. Kapsayıcı örneği Çalışıyor durumunda gösterilir.

  3. Kaynak menüsünde Kapsayıcılar'ı seçin. Kapsayıcı örneği adı, kapsayıcıyla birlikte eklenen proje adıdır.

    Screenshot of the deployment script connect container instance option in the Azure portal.

  4. Bağlan'ı ve ardından Bağlan'ı seçin. Kapsayıcı örneğine bağlanamıyorsanız kapsayıcı grubunu yeniden başlatın ve yeniden deneyin.

  5. Konsol bölmesinde aşağıdaki komutları çalıştırın:

    cd /mnt/azscripts/azscriptinput
    ls
    ./hello.sh John Dole
    

    Çıkış Hello John Dole'dır.

    Screenshot of the deployment script container instance test output displayed in the console.

Docker kullanma

Dağıtım betiği geliştirme ortamınız olarak önceden yapılandırılmış bir Docker kapsayıcı görüntüsü kullanabilirsiniz. Docker'ı yüklemek için bkz . Docker'ı alma. Ayrıca, docker kapsayıcısına dağıtım betiklerini içeren dizini bağlamak için dosya paylaşımını yapılandırmanız gerekir.

  1. Dağıtım betiği kapsayıcı görüntüsünü yerel bilgisayara çekin:

    docker pull mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    Örnek PowerShell 4.3.0 sürümünü kullanır.

    BIR MCR'den CLI görüntüsü çekmek için:

    docker pull mcr.microsoft.com/azure-cli:2.0.80
    

    Bu örnekte CLI 2.0.80 sürümü kullanılmaktadır. Dağıtım betiği, burada bulunan varsayılan CLI kapsayıcıları görüntülerini kullanır.

  2. Docker görüntüsünü yerel olarak çalıştırın.

    docker run -v <host drive letter>:/<host directory name>:/data -it mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    Konak sürücü harfini> ve <ana bilgisayar dizini adını> paylaşılan sürücüde var olan bir klasörle değiştirin.< Klasörü kapsayıcıdaki /data klasörüne eşler. Örneğin, D:\docker'ı eşlemek için:

    docker run -v d:/docker:/data -it mcr.microsoft.com/azuredeploymentscripts-powershell:az4.3
    

    -bu, kapsayıcı görüntüsünü canlı tutmak anlamına gelir.

    CLI örneği:

    docker run -v d:/docker:/data -it mcr.microsoft.com/azure-cli:2.0.80
    
  3. Aşağıdaki ekran görüntüsünde, paylaşılan sürücüde helloworld.ps1 dosyanız olduğu göz önünde bulundurulduğunda PowerShell betiğinin nasıl çalıştırıldığını gösterilmektedir.

    Screenshot of the Resource Manager template deployment script using Docker command.

Betik başarıyla test edildikten sonra şablonlarınızda dağıtım betiği olarak kullanabilirsiniz.

Sonraki adımlar

Bu makalede dağıtım betiklerini kullanmayı öğrendiniz. Dağıtım betiği öğreticisinde gezinmek için: