次の方法で共有


Azure Image Builder トリガーを使用して自動イメージ ビルドを設定する方法

Azure Image Builder (AIB) のトリガーを使用して、ビルド パイプラインで特定の条件が満たされたときに自動イメージ ビルドを設定できます。

重要

リージョンごとに使用できるトリガーの数には制限があるのでご注意ください (具体的には、サブスクリプションごとに、1 つのリージョンあたり 100 個です)。

Note

現時点では、新しいソース イメージのトリガーの設定のみがサポートされていますが、今後はさまざまな種類のトリガーがサポートされる予定です。

前提条件

最初のトリガーを設定する前に、Azure Image Builder API バージョン 2022-07-01 を使用していることを確認してください。

Azure Image Builder でトリガーを設定する方法

プロバイダーを登録する

トリガーによって VM Image Builder を使用するには、以下のプロバイダーを登録する必要があります。 次のコマンドを実行して登録状況を確認します。

az provider show -n Microsoft.VirtualMachineImages -o json | grep registrationState
az provider show -n Microsoft.KeyVault -o json | grep registrationState
az provider show -n Microsoft.Compute -o json | grep registrationState
az provider show -n Microsoft.Storage -o json | grep registrationState
az provider show -n Microsoft.Network -o json | grep registrationState
az provider show -n Microsoft.ContainerInstance -o json | grep registrationState

出力に "registered" と表示されない場合は、次のコマンドを実行します。

az provider register -n Microsoft.VirtualMachineImages
az provider register -n Microsoft.Compute
az provider register -n Microsoft.KeyVault
az provider register -n Microsoft.Storage
az provider register -n Microsoft.Network
az provider register -n Microsoft.ContainerInstance

自動イメージ ビルド トリガー機能を登録します。

az feature register --namespace Microsoft.VirtualMachineImages --name Triggers

変数の設定

まず、コマンドで繰り返し使用するいくつかの変数を設定する必要があります。

# Resource group name - ibTriggersTestRG in this example
resourceGroupName=ibTriggersRG
# Datacenter location - West US 2 in this example
location=westus2
# Additional region to replicate the image to - East US in this example
additionalregion=eastus2
# Name of the Azure Compute Gallery - ibTriggersGallery in this example
acgName=ibTriggersGallery
# Name of the image definition to be created - ibTriggersImageDef in this example
imageDefName=ibTriggersImageDef
# Name of the Trigger to be created - ibTrigger in this example
ibTriggerName=ibTrigger
# Name of the image template to be created - ibTriggersImageTemplate in this example
imageTemplateName=ibTriggersImageTemplate
# Reference name in the image distribution metadata
runOutputName=ibTriggersTestRun
# Create a variable for your subscription ID
subscriptionID=$(az account show --query id --output tsv)

リソース グループの作成

次に、イメージ テンプレートを保存できるリソース グループを作成する必要があります。 リソース グループを作成するには、次のコマンドを使用します。

az group create -n $resourceGroupName -l $location

サービスのマネージド ID を作成する

また、イメージ テンプレート (および、可能性として Azure Image Builder ビルド VM) に使用されるマネージド ID も作成する必要があります。 この例では、"共同作成者" アクセス権を持つマネージド ID を作成しますが、Azure Image Builder サービスが正しく機能するために必要なアクセス許可を含めていれば、マネージド ID に割り当てるアクセス許可またはロールを調整できます。

Azure Image Builder サービスに必要なアクセス許可の詳細については、ドキュメント「Azure CLI を使用して Azure VM Image Builder のアクセス許可を構成する」を参照してください

Azure Image Builder でマネージド ID を割り当てて使用する方法の詳細については、VM Image Builder テンプレート リファレンスでの ID に関するドキュメントを参照してください

次のコマンドを使用して、イメージ テンプレートに使用されるマネージド ID を作成します。

# Create user-assigned identity for VM Image Builder to access the storage account where the script is stored
identityName=aibBuiUserId$(date +'%s')
az identity create -g $resourceGroupName -n $identityName

# Get the identity client and principal ID
imgBuilderCliId=$(az identity show -g $resourceGroupName -n $identityName --query clientId -o tsv)

# Get the user identity URI that's needed for the template
imgBuilderId=/subscriptions/$subscriptionID/resourcegroups/$resourceGroupName/providers/Microsoft.ManagedIdentity/userAssignedIdentities/$identityName

# Grant "Contributor" access to the user-assigned identity
az role assignment create \
    --assignee $imgBuilderCliId \
    --role "Contributor" \
    --scope /subscriptions/$subscriptionID/resourceGroups/$resourceGroupName

Azure Compute Gallery で VM Image Builder を使用するには、既存のギャラリーとイメージ定義が必要です。 VM Image Builder では、ギャラリーとイメージ定義は自動的には作成されません。

使用するギャラリーとイメージ定義がまだない場合は、最初に作成します。

まず、ギャラリーを作成します。

az sig create \
    -g $resourceGroupName \
    --gallery-name $acgName

次に、イメージ定義を作成します。

az sig image-definition create \
   -g $resourceGroupName \
   --gallery-name $acgName \
   --gallery-image-definition $imageDefName \
   --publisher myIbPublisher \
   --offer myOffer \
   --sku 18.04-LTS \
   --os-type Linux

イメージ テンプレートを作成する

サンプルの JSON テンプレートをダウンロードし、変数を使用して構成します。 次のイメージ テンプレートでは、ソースとしてプラットフォーム イメージを使用しますが、Azure Compute Gallery に新しいイメージ バージョンがあるときに自動イメージ ビルドを設定する場合は、ソースを Azure Compute Gallery イメージに変更できます。

curl https://raw.githubusercontent.com/Azure/azvmimagebuilder/main/quickquickstarts/9_Setting_up_a_Trigger_with_a_Custom_Linux_Image/helloImageTemplate.json -o helloImageTemplateforTriggers.json
sed -i -e "s/<subscriptionID>/$subscriptionID/g" helloImageTemplateforTriggers.json
sed -i -e "s/<rgName>/$resourceGroupName/g" helloImageTemplateforTriggers.json
sed -i -e "s/<imageDefName>/$imageDefName/g" helloImageTemplateforTriggers.json
sed -i -e "s/<acgName>/$acgName/g" helloImageTemplateforTriggers.json
sed -i -e "s/<region1>/$location/g" helloImageTemplateforTriggers.json
sed -i -e "s/<region2>/$additionalregion/g" helloImageTemplateforTriggers.json
sed -i -e "s/<runOutputName>/$runOutputName/g" helloImageTemplateforTriggers.json
sed -i -e "s%<imgBuilderId>%$imgBuilderId%g" helloImageTemplateforTriggers.json

イメージ テンプレートの要件:

  • source は、プラットフォーム イメージまたは Azure Compute Gallery イメージである必要があります (現時点では、これら 2 つのソースのみが許可されています)
  • プラットフォーム イメージを使用している場合、ソースのバージョンは Latest であることが必要です。 Azure Compute Gallery イメージの場合、バージョン名を含むリソース ID の最後の部分は Latest に設定する必要があります。
  • Azure Compute Gallery にイメージを配布する場合、バージョンは指定できません。 バージョンは自動的に増分されます。
  • ソースが Azure Compute Gallery イメージに設定され、配布が Azure Compute Gallery に設定されている場合、ソース ギャラリー イメージと配布ギャラリー イメージを同じにすることはできません。 Azure Compute Gallery イメージ定義 ID は、ソース ギャラリー イメージと配布ギャラリー イメージの両方で同じにすることはできません。
  • イメージ テンプレートの provisioningState は "Succeeded" になっている (つまり、テンプレートは問題なく作成されている) 必要があります。 テンプレートが正常にプロビジョニングされていないと、トリガーを作成できません。

テンプレートを構成したら、次のコマンドを使用して、イメージ構成を Azure Image Builder サービスに送信します。

az image builder create -g $resourceGroupName -n $imageTemplateName --image-template helloImageTemplateforTriggers.json

次のコマンドを使用すると、イメージ テンプレートが正常に作成されたことを確認できます。

az image builder show --name $imageTemplateName --resource-group $resourceGroupName

Note

上記のコマンドを実行した場合、provisioningState は "Succeeded" となる必要があります。これは、テンプレートが問題なく作成されたことを意味します。 provisioningState が Succeeded でない場合、トリガーでそのイメージ テンプレートを使用できません。

ソース トリガーを作成する

サンプルのトリガー テンプレートをダウンロードし、変数を使用して構成します。 次のトリガーでは、ソース イメージが更新されるたびに新しいイメージ ビルドを開始します。

curl https://raw.githubusercontent.com/kof-f/azvmimagebuilder/main/quickquickstarts/9_Setting_up_a_Trigger_with_a_Custom_Linux_Image/trigger.json -o trigger.json
sed -i -e "s/<region1>/$location/g" trigger.json

トリガー要件:

  • トリガー内の場所は、イメージ テンプレート内の場所と同じである必要があります。 これは、az resource create コマンドレットの要件です。
  • 現在、1 つの kind のトリガー ("SourceImage") がサポートされています
  • イメージごとに 1 つの "SourceImage" トリガーのみがサポートされます。 イメージに "SourceImage" トリガーが既にある場合、このトリガーを新たに作成できません。
  • kind フィールドは、別の種類のトリガーに更新できません。 トリガーを削除して再作成するか、適切な構成を使用して別のトリガーを作成する必要があります。

トリガーをリソース グループに追加するには、次のコマンドを使用します。

az image builder trigger create --name $ibTriggerName --resource-group $resourceGroupName --image-template-name $imageTemplateName --kind SourceImage

また、次のコマンドを使用すると、トリガーが正常に作成されたことを確認できます。

az image builder trigger show --name $ibTriggerName --image-template-name $imageTemplateName --resource-group $resourceGroupName

Note

上記のコマンドを実行した場合、provisioningStateSucceeded となる必要があります。これは、トリガーが問題なく作成されたことを意味します。 status では、コードが Healthy になっていて、メッセージには Trigger is active. が示されている必要があります

リソースをクリーンアップする

トリガーを削除する

トリガーを削除するには、次のコマンドを使用します。

az image builder trigger delete --name $ibTriggerName --image-template-name $imageTemplateName --resource-group $resourceGroupName

イメージ テンプレートを削除する

イメージ テンプレートを削除するには、次のコマンドを使用します。

az image builder delete --name $imageTemplateName --resource-group $resourceGroupName

次の手順

詳細については、Image Builder テンプレート リファレンスを参照してください。