Azure Web App - Rolling Restart

Octopus.AzurePowerShell exported 2021-04-08 by tekguy belongs to ‘Azure’ category.

Performs a delayed rolling restart of all instances within an Azure Web App


Note This template is designed to run against an azure web app octopus target

Depends on Azure CLI and powershell to be installed on the running machine

Parameters

When steps based on the template are included in a project’s deployment process, the parameters below can be set.

Azure Account

azWebApp.AzureAcct =

The azure account that has access to the web app

App Service Name

azWebApp.WebAppName =

The azure web app name

Azure Resource Group

azWebApp.ResourceGroup =

The azure resource group

Restart Delay

azWebApp.RestartDelay = 30

Delay between instance restart

Script body

Steps based on this template will execute the following PowerShell script.

try {az --version}
catch
{
    throw "az CLI not installed"
}

$webAppName = $OctopusParameters["azWebApp.WebAppName"]
$resourceGroup = $OctopusParameters["azWebApp.ResourceGroup"]
$restartDelay = $OctopusParameters["azWebApp.RestartDelay"]



Write-Host "Web App Name: $webAppName"
Write-Host "Resource Group: $resourceGroup"
Write-Host "Restart Delay: $restartDelay"

Write-Host "Checking $webAppName status in resource group $resourceGroup"

$appState = az webapp list --resource-group $resourceGroup --query "[?name=='$webAppName'].{state: state, hostName: defaultHostName}" | ConvertFrom-Json

# only execute if running
if($appState.state -eq "running")
{
    $appInstances = az webapp list-instances -n $webAppName --resource-group $resourceGroup --query '[].{Id: id}' | ConvertFrom-Json
    Write-Host "" $appInstances.Count "Instance(s) found`r`n" -ForegroundColor Green

    if($appInstances.count -gt 0){
        foreach ($instance in $appInstances){
            Write-Host "Restarting Instance: $instance"
            az webapp restart --ids $instance.Id
            Write-Host "Pausing $restartDelay second(s)`r`n" -ForegroundColor Yellow
            Start-Sleep -Seconds $restartDelay
        }
    }
}

Provided under the Apache License version 2.0.

Report an issue

To use this template in Octopus Deploy, copy the JSON below and paste it into the Library → Step templates → Import dialog.

{
  "Id": "43cfc12d-5ae9-425d-ab01-7124ffdd9ee6",
  "Name": "Azure Web App - Rolling Restart",
  "Description": "Performs a delayed rolling restart of all instances within an Azure Web App\n<hr />\n\n*<p>Note This template is designed to run against an azure web app octopus target </p>*\n*<p>Depends on Azure CLI and powershell to be installed on the running machine</p>*",
  "Version": 1,
  "ExportedAt": "2021-04-08T20:19:36.856Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "tekguy",
  "Packages": [],
  "Parameters": [
    {
      "Id": "6670dc01-4323-41b8-87f4-a824608bad71",
      "Name": "azWebApp.AzureAcct",
      "Label": "Azure Account",
      "HelpText": "The azure account that has access to the web app",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AzureAccount"
      }
    },
    {
      "Id": "56dc3fcb-237d-4618-944a-441bd3d089c5",
      "Name": "azWebApp.WebAppName",
      "Label": "App Service Name",
      "HelpText": "The azure web app name",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "7fc59af9-00cf-4e3f-995d-1ccca3057324",
      "Name": "azWebApp.ResourceGroup",
      "Label": "Azure Resource Group",
      "HelpText": "The azure resource group",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    },
    {
      "Id": "8fda8110-e3bb-4881-89dc-c148a7bd1943",
      "Name": "azWebApp.RestartDelay",
      "Label": "Restart Delay",
      "HelpText": "Delay between instance restart",
      "DefaultValue": "30",
      "DisplaySettings": {
        "Octopus.ControlType": "SingleLineText"
      }
    }
  ],
  "Properties": {
    "Octopus.Action.Script.ScriptSource": "Inline",
    "Octopus.Action.Script.Syntax": "PowerShell",
    "OctopusUseBundledTooling": "False",
    "Octopus.Action.Azure.AccountId": "#{azWebApp.AzureAcct}",
    "Octopus.Action.Script.ScriptBody": "try {az --version}\ncatch\n{\n    throw \"az CLI not installed\"\n}\n\n$webAppName = $OctopusParameters[\"azWebApp.WebAppName\"]\n$resourceGroup = $OctopusParameters[\"azWebApp.ResourceGroup\"]\n$restartDelay = $OctopusParameters[\"azWebApp.RestartDelay\"]\n\n\n\nWrite-Host \"Web App Name: $webAppName\"\nWrite-Host \"Resource Group: $resourceGroup\"\nWrite-Host \"Restart Delay: $restartDelay\"\n\nWrite-Host \"Checking $webAppName status in resource group $resourceGroup\"\n\n$appState = az webapp list --resource-group $resourceGroup --query \"[?name=='$webAppName'].{state: state, hostName: defaultHostName}\" | ConvertFrom-Json\n\n# only execute if running\nif($appState.state -eq \"running\")\n{\n    $appInstances = az webapp list-instances -n $webAppName --resource-group $resourceGroup --query '[].{Id: id}' | ConvertFrom-Json\n    Write-Host \"\" $appInstances.Count \"Instance(s) found`r`n\" -ForegroundColor Green\n\n    if($appInstances.count -gt 0){\n        foreach ($instance in $appInstances){\n            Write-Host \"Restarting Instance: $instance\"\n            az webapp restart --ids $instance.Id\n            Write-Host \"Pausing $restartDelay second(s)`r`n\" -ForegroundColor Yellow\n            Start-Sleep -Seconds $restartDelay\n        }\n    }\n}\n"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/azure-web-app-rolling-restart.json",
  "Website": "/step-templates/43cfc12d-5ae9-425d-ab01-7124ffdd9ee6",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzoUSnt8YxXlFuGHSbIaxvj+fip4btkLn1blkWLaF5v03yLhLOYlVuGYfMOMZzNGxCOzhjTJqFkXnjq3Dr1yyvPI3hGl3Ih3zzHHNKudRstRhX5O58vIcShY67Gq6EPIESlzUWvazaGAOGbvU7ArDu/g8M4o8opDZWvbvPzlL/MMBE8jT9T9W7PbAJlHPTBFRf9yVTEcs63msXz2UHLSgf650G/d5t+wjbxxB2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