简体   繁体   中英

Powershell + Azure App Service + Azure Container Registry

I am using powershell to handle various CI/CD functions.

=============================================

Here is what I have done so far.

  1. Build NodeJS App
  2. Package NodeJS App in Container
  3. Publish Container to Private Azure Container Registry

=============================================

Here is what I am trying to figure out.

  1. Create App Service Plan on the Fly
  2. Create Web App (Container) on the Fly
  3. Pull in Container from ACR into App Service

Here is my code. It doesn't seem to be working.

$azureContainerCredential = Get-AzContainerRegistryCredential -ResourceGroupName $env:AZURE_CONTAINER_REGISTRY_RESOURCE_GROUP -Name $env:AZURE_CONTAINER_REGISTRY_NAME
$azureSecuredPassword = ConvertTo-SecureString $azureContainerCredential.Password -AsPlainText -Force

$azureContainerRegistry = Get-AzContainerRegistry -ResourceGroupName $env:AZURE_CONTAINER_REGISTRY_RESOURCE_GROUP

$azureAppServicePlan = Get-AzAppServicePlan -ResourceGroupName "amlihelloworld" -Name "amlihelloworld-app-service-plan"

if($null -eq $azureAppServicePlan)
{
  "==============================================================================="
   Write-Output  "CREATING HELLO WORLD WEB APPLICATION"

   $azureAppServicePlan = New-AzAppServicePlan -Name "amlihelloworld-app-service-plan" -Location "Central 
   US" -ResourceGroupName "amlihelloworld" -Tier Standard

   $azureApp = New-AzWebApp -ResourceGroupName "amlihelloworld" -Name "amlihelloworld2" -AppServicePlan 
   $azureAppServicePlan.Name -ContainerImageName "amlihelloworld:20200422.7" -ContainerRegistryPassword 
   $azureSecuredPassword  -ContainerRegistryUrl $azureContainerRegistry.LoginServer - 
     ContainerRegistryUser $azureContainerCredential.Username

   $azureAppSlot = New-AzWebAppSlot -Name $azureApp.Name  -ResourceGroupName "amlihelloworld" -Slot 
    "development"
}

$azureApp1 = Get-AzWebApp -ResourceGroupName "amlihelloworld" -Name "amlihelloworld"

=============================================

Here is whats happening

When I switch the slots to my app for production.

It doesn't seem to be showing my app at all.

How can I tell if it uploaded my app or not?

Actually, I don't think there is a logic problem in the code, but a problem for the commands itself.

First, if you want to cut the PowerShell command into multiple lines, you need to add the character ` append each line except the last one. And you'd better use the same web app name if you want to get it. So you'd like to make a little change in your code:

$azureContainerCredential = Get-AzContainerRegistryCredential -ResourceGroupName $env:AZURE_CONTAINER_REGISTRY_RESOURCE_GROUP -Name $env:AZURE_CONTAINER_REGISTRY_NAME
$azureSecuredPassword = ConvertTo-SecureString $azureContainerCredential.Password -AsPlainText -Force

$azureContainerRegistry = Get-AzContainerRegistry -ResourceGroupName $env:AZURE_CONTAINER_REGISTRY_RESOURCE_GROUP

$azureAppServicePlan = Get-AzAppServicePlan -ResourceGroupName "amlihelloworld" -Name "amlihelloworld-app-service-plan"

if($null -eq $azureAppServicePlan)
{
  "==============================================================================="
   Write-Output  "CREATING HELLO WORLD WEB APPLICATION"

   $azureAppServicePlan = New-AzAppServicePlan -Name "amlihelloworld-app-service-plan" -Location "Central 
   US" -ResourceGroupName "amlihelloworld" -Tier Standard

   $azureApp = New-AzWebApp -ResourceGroupName "amlihelloworld" -Name "amlihelloworld2" `
   -AppServicePlan $azureAppServicePlan.Name `
   -ContainerImageName "amlihelloworld:20200422.7" `
   -ContainerRegistryPassword $azureSecuredPassword  `
   -ContainerRegistryUrl $azureContainerRegistry.LoginServer `
   -ContainerRegistryUser $azureContainerCredential.Username

   $azureAppSlot = New-AzWebAppSlot -Name $azureApp.Name  -ResourceGroupName "amlihelloworld" -Slot 
    "development"
}

$azureApp1 = Get-AzWebApp -ResourceGroupName "amlihelloworld" -Name "amlihelloworld2"

You also need to check carefully if your environment variables exist as normal.

Apparently it's a bug on Microsoft side and it's not possible to publish containers through powershell scripts. Please read here: https://github.com/Azure/azure-powershell/issues/10645

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM