user989988
user989988

Reputation: 3746

Swap deployment slot using Switch-AzWebAppSlot

I have the following script for swapping deployment slots:

            $functionApp = $functionAppName;
            Write-Host "Starting to swap $functionApp";
            Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ApplySlotConfig
            Start-Sleep -Seconds 30
            Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction CompleteSlotSwap
            Start-Sleep -Seconds 30
            Write-Host "Completed swapping $functionApp";

Is there a way to verify that Switch-AzWebAppSlot has executed successfully or not before proceeding to the next line? For example:

status = Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ApplySlotConfig

if (status = "Success")
{
  Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction CompleteSlotSwap
}

UPDATE:

I updated the script based on the suggested solution as follows:

try {
            $functionApp = "functionAppname";           
            Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction ApplySlotConfig
            $date = Get-Date -Format "yyyy-MM-dd"
            $log = Get-AzLog -ResourceGroup $resourceGroup -StartTime $date -Caller SlotSwapJobProcessor
            $status = $log[0].Status
            if ("Succeeded" -eq $status) {
                Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName staging -DestinationSlotName production -SwapWithPreviewAction CompleteSlotSwap
                $log = Get-AzLog -ResourceGroup $resourceGroup -StartTime $date -Caller SlotSwapJobProcessor
                $status = $log[0].Status
                if ("Succeeded" -eq $status) {
                    Write-Host "Completed swapping $functionApp";
                }
                else
                {
                    Write-Host "Failed to swap $functionApp"
                }
            }
            else
            {
                Write-Host "Failed to swap $functionApp"
            }
}
catch
{
        Write-Host "Something went wrong, please try again..."
}

but I see no data in $log. What am I missing?

Upvotes: 0

Views: 516

Answers (1)

anon
anon

Reputation:

Using the below script, I can get the status of last Slot Swap operation done on the application in Azure:

Connect-AzAccount
Set-AzContext -SubscriptionId "<subscription-id>"


$functionApp = "KrishNet6FuncApp1205";
$resourceGroup = "HariTestRG";


Write-Host "Starting to swap $functionApp";
Switch-AzWebAppSlot -ResourceGroupName $resourceGroup -Name $functionApp -SourceSlotName production -DestinationSlotName staging -SwapWithPreviewAction ApplySlotConfig
Start-Sleep -Seconds 30
Write-Host "Completed swapping $functionApp";

Get-AzLog -ResourceGroup $resourceGroup -StartTime 2022-06-23 -Caller SlotSwapJobProcessor


Output:

enter image description here

Upvotes: 1

Related Questions