Friday, December 15, 2023

Deploy Azure Container App with Azure CLI

This blog explains how to setup an Azure DevOps release pipeline to deploy a container app to the Azure Container Environment using an Azure CLI script.

Pre-requisites: 

  • Azure DevOps deployment pipeline
  •  Azure container app environment 
  • Azure Container registry

Add an Azure CLI task to the Azure DevOps pipeline and add following script. The script installs a few required extensions and create a container app.


# install support extensions
az extension add --name containerapp --upgrade

az provider register --namespace Microsoft.App

az provider register --namespace Microsoft.OperationalInsights

#create or update container app
$appurl = az containerapp create --name YOUR_CONTAINER_APP_NAME --resource-group CONTAINER_APP_RESOURCEGROUP --image YOUR_CONTAINER_REGISTRY_NAME.azurecr.io/mydemo/IMAGE_REPO:IMAGETAG --environment CONTAINER_APP_ENVIRONMENT --registry-server CONTAINER_REGISTRY_NAME.azurecr.io --registry-username CONTAINER_REGISTRY_USERNAME --registry-password CONTAINER_REGISTRY_PASSWORD --ingress internal --target-port 80 --query properties.configuration.ingress.fqdn --ingress external

#set app url as env variable
Write-Host "##vso[task.setvariable variable=containerappurl;issecret=false]$appurl"



Once the container app is created, we need to add a private DNS record to enable access to the container app. Add another Azure CLI task and include the following script to add the private DNS record using the pipeline.

REGION - region value available in the container app url
CONTAINER_APP_ENVIRONMENT_NAME - container app environment name
CONTAINER_APP_ENVIRONMENT_RESOURCEGROUP - resource group of container app environment
PRIVATE_DNS_RESOURCEGROUP - private DNS resource group name

# get dns record name
$recordname = ($(containerappurl) -split '.REGION')[0]


#get Container app environment static ip
$getcaeiP = az containerapp env show -n CONTAINER_APP_ENVIRONMENT_NAME -g CONTAINER_APP_ENVIRONMENT_RESOURCEGROUP | ConvertFrom-Json
$caeip = $getcaeiP.properties.staticIp

#add DNS A record
if((az network private-dns record-set a list -g PRIVATE_DNS_RESOURCEGROUP -z REGION.azurecontainerapps.io | ConvertFrom-Json).Name -contains $recordname)
    {
     Write-Host "DNS record already added"
    }
    else
    {
     az network private-dns record-set a add-record -g PRIVATE_DNS_RESOURCEGROUP -z REGION.azurecontainerapps.io -n $recordname -a $caeip
    }


No comments:

Post a Comment