Azure Web App - Restart

Octopus.AzurePowerShell exported 2022-05-17 by zogamorph belongs to ‘Azure’ category.

Restarts 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

Start if stopped

azWebApp.StartIfStopped = False

If the web app is stopped, start it

Script body

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

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

$webApp = $OctopusParameters["Octopus.Action.Azure.WebAppName"]
$resourceGroup = $OctopusParameters["Octopus.Action.Azure.ResourceGroupName"]
$startIfStopped = $OctopusParameters["azWebApp.StartIfStopped"]

Write-Host "Checking webapp $webApp status in resource group $resourceGroup"

$appState = az webapp list --resource-group $resourceGroup --query "[?name=='$webApp'].{state: state, hostName: defaultHostName}" | ConvertFrom-Json
if($appState.state -eq "stopped")
{
    if($startIfStopped -eq 'true')
    {
        Write-Host "Webapp is not running. Starting..." -NoNewline
    	az webapp start --name $webApp --resource-group $resourceGroup
        Write-Host "Done"
    }
    else
    {
      Throw "Webapp is not running."
    }
}

Write-Host "Webapp running, restarting"

else
{
	Write-Host "Restarting $webApp in resource group $resourceGroup"
	az webapp restart --name $webApp --resource-group $resourceGroup
}

Start-Sleep -s 5

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

if($appState.state -ne "running")
{
	Throw "Webapp failed to start.  Check the app's activity/error log"
}

write-host "Webapp $webApp running. Check at: $($appState.hostName)"

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": "1f40e418-17bf-4b3e-bbe7-c6d41cbded93",
  "Name": "Azure Web App - Restart",
  "Description": "Restarts 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": 2,
  "ExportedAt": "2022-05-17T10:59:12.694Z",
  "ActionType": "Octopus.AzurePowerShell",
  "Author": "zogamorph",
  "Packages": [],
  "Parameters": [
    {
      "Id": "e732ddaa-a43b-4369-8813-065286069d65",
      "Name": "azWebApp.AzureAcct",
      "Label": "Azure Account",
      "HelpText": "The azure account that has access to the web app",
      "DefaultValue": "",
      "DisplaySettings": {
        "Octopus.ControlType": "AzureAccount"
      }
    },
    {
      "Id": "901ec60b-0ffd-4d08-be6f-fa9e8aec457a",
      "Name": "azWebApp.StartIfStopped",
      "Label": "Start if stopped",
      "HelpText": "If the web app is stopped, start it",
      "DefaultValue": "False",
      "DisplaySettings": {
        "Octopus.ControlType": "Checkbox"
      }
    }
  ],
  "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$webApp = $OctopusParameters[\"Octopus.Action.Azure.WebAppName\"]\n$resourceGroup = $OctopusParameters[\"Octopus.Action.Azure.ResourceGroupName\"]\n$startIfStopped = $OctopusParameters[\"azWebApp.StartIfStopped\"]\n\nWrite-Host \"Checking webapp $webApp status in resource group $resourceGroup\"\n\n$appState = az webapp list --resource-group $resourceGroup --query \"[?name=='$webApp'].{state: state, hostName: defaultHostName}\" | ConvertFrom-Json\nif($appState.state -eq \"stopped\")\n{\n    if($startIfStopped -eq 'true')\n    {\n        Write-Host \"Webapp is not running. Starting...\" -NoNewline\n    \taz webapp start --name $webApp --resource-group $resourceGroup\n        Write-Host \"Done\"\n    }\n    else\n    {\n      Throw \"Webapp is not running.\"\n    }\n}\n\nWrite-Host \"Webapp running, restarting\"\n\nelse\n{\n\tWrite-Host \"Restarting $webApp in resource group $resourceGroup\"\n\taz webapp restart --name $webApp --resource-group $resourceGroup\n}\n\nStart-Sleep -s 5\n\n$appState = az webapp list --resource-group $resourceGroup --query \"[?name=='$webApp'].{state: state, hostName: defaultHostName}\" | ConvertFrom-Json\n\nif($appState.state -ne \"running\")\n{\n\tThrow \"Webapp failed to start.  Check the app's activity/error log\"\n}\n\nwrite-host \"Webapp $webApp running. Check at: $($appState.hostName)\"\n"
  },
  "Category": "Azure",
  "HistoryUrl": "https://github.com/OctopusDeploy/Library/commits/master/step-templates//opt/buildagent/work/75443764cd38076d/step-templates/azure-web-app-restart.json",
  "Website": "/step-templates/1f40e418-17bf-4b3e-bbe7-c6d41cbded93",
  "Logo": "iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAMAAACahl6sAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAADNQTFRF////AHjXf7vrv931QJrh7/f8EIDaIIncMJHfYKvmz+b3n8zw3+76j8Ttr9XycLPpUKLkkKvYFAAABGZJREFUeNrsnNmCqjoQRc1MEiD8/9cer7Yt2KBJZQC8ez07sKlKTQlcLgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAzoUSnt8YxXlFuGHSbIaxvj+fip4btkLn1blkWLaF5v03yLhLOYlVuGYfMOMZzNGxCOzhjTJqFkXnjq3Dr1yyvPI3hGl3Ih3zzHHNKudRstRhX5O58vIcShY67Gq6EPIESlzUWvazaGAOGbvU7ArDu/g8M4o8opDZWvbvPzlL/MMBE8jT9T9W7PbAJlHPTBFRf9yVTEcs63msXz2UHLSgf650G/d5t+wjbxxB2UCMqGrk8/LFSD7uJMeNt5bcJCyQZyAe5Fo9KYfWS2flQrr4b4tpuzaeWjYs49rt9LHf9uZD7+VbyVi9EBNrjYjuq2sxQOrl+p+HuBVu45qvqfq691ttYFQ5KyKbyJgaIY/NGxrlWZwlwGvmvu1oY3PuAv0niTq6tZ78jk//9uc1r1r4lQki7y7sp2Tu4V1y2iLoqFTqi1lIGcpFiebrZNZ1dOkF0cCIlO8jQ47nCkam9Lilz9GhD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