0

According to documentation "An ARM template is idempotent, which means it can be executed as many times as you wish, and the result will be the same every time". But I just learned that when I redeploy AppService (without any changes) it removes my application. Endpoints were not responding anymore and there was no application logs so I went to Azure portal console, ran DIR, and to my surprise the only file that is there is hostingstart.html! Is it documented somewhere? This changes completely how I need to handle ARM templates in my Release pipeline.

I'm using linked templates. In main template I have this resource:

{
      "name": "myApp",
      "type": "Microsoft.Resources/deployments",
      "apiVersion": "2019-10-01",
      "dependsOn": [
        "storage"
      ],
      "properties": {
        "mode": "Incremental",
        "templateLink": {
          "uri": "[uri(variables('templateBaseUrl'), 'myApp.json')]"
        },
        "parameters": {
          "env": {
            "value": "[parameters('env')]"
          },
          "myAppAppServiceSku": {
            "value": "[parameters('myAppAppServiceSku')]"
          },
          "storageAccountName": {
            "value": "[variables('storageAccountName')]"
          }
        }
      }
    }

and the linked template

"resources": [
    {
      "name": "[variables('myAppServerFarmName')]",
      "type": "Microsoft.Web/serverfarms",
      "apiVersion": "2018-02-01",
      "location": "[resourceGroup().location]",
      "tags": {
        "ENV": "[parameters('env')]"
      },
      "sku": {
        "name": "[parameters('myAppAppServiceSku')]"
      },
      "properties": {
      }
    },
    {
      "name": "[variables('myAppWebSiteName')]",
      "type": "Microsoft.Web/sites",
      "apiVersion": "2018-11-01",
      "dependsOn": [
        "[variables('myAppServerFarmName')]"
      ],
      "location": "[resourceGroup().location]",
      "tags": {
        "ENV": "[parameters('env')]"
      },
      "properties": {
        "serverFarmId": "[resourceId('Microsoft.Web/serverfarms/', variables('myAppServerFarmName'))]",
        "siteConfig": {
          "alwaysOn": true
        }
      },
      "resources": [
        {
          "name": "appSettings",
          "type": "config",
          "apiVersion": "2018-11-01",
          "dependsOn": [
            "[variables('myAppWebSiteName')]"
          ],
          "tags": {
            "ENV": "[parameters('env')]"
          },
          "properties": {
            "storageAccountName": "[parameters('storageAccountName')]",
            "storageKey": "[listKeys(resourceId('Microsoft.Storage/storageAccounts/', parameters('storageAccountName')), '2019-04-01').keys[0].value]"
          }
        }
      ]
    }
  ]

EDIT: I have checked with deployment using Kudu ZIP Deploy. And after this deployment it redeployment of ARM templates does not remove code! So it works as expected. So deployment from Release Pipelines is different in some way.

After I execute both steps everything looks fine. But when I then just execute first step application code is removed. This is how it looks right now.

Release pipeline

And steps have one task each: ARM templates stage Deploy App stage

Piotr Perak
  • 10,718
  • 9
  • 49
  • 86
  • 2
    We deploy ARM templates dozens of times a day and the app service content is never removed. – juunas Jan 22 '20 at 07:39
  • I just tested it again minutes ago. Always clears the contents. And I'm not changing anything in template. I just redeploy from Release Pipelines (Classic? Those with UI). – Piotr Perak Jan 22 '20 at 07:53
  • @juunas do you do it using Azure resource group deployment Version 2 task in Release Pipelines? – Piotr Perak Jan 22 '20 at 08:07
  • Version 2 or 3 depending on the project. After ARM deployment, new versions of the app are deployed through package deploy or Web Deploy. – juunas Jan 22 '20 at 08:21
  • 1
    So if you deploy new version you don't know if previous one is removed by ARM deployment. I just tested it with `New-AzResourceGroupDeployment -ResourceGroupName $resourceGroup -TemplateUri $templateUri -TemplateParameterUri $templateParameterUri -Mode Incremental -DeploymentDebugLogLevel All` and it also removed the app. – Piotr Perak Jan 22 '20 at 08:27
  • 1
    We also deploy tens of App Services in ARM templates via Octopus Deploy daily and never have them remove the software. – David C Jan 22 '20 at 11:40
  • @DavidC can you test it with `New-AzResourceGroupDeployment` and same template that was used to deploy it originally? – Piotr Perak Jan 22 '20 at 11:50
  • 1
    I used your template as a test harness and the PowerShell command to deploy it. Then uploaded an ASP.Net website to it via Push Zip deploy in Kudu, then re-ran the ARM template deploy and the binaries were still on the app service after the deploy. – David C Jan 22 '20 at 12:41
  • Something very weird is going on. I do this: Deploy new web app, go to console and create `myfile.txt` with some content in it. Then I deploy app and in console I see my application but `myfile.txt` is removed. Now I redeploy using same template (both from Azure Release Pipeline and `New-AzResourceGroupDeployment` and now when I go to console I again see `myfile.txt` with valid contents but no application! Like it did some revert or rollback of some kind. – Piotr Perak Jan 22 '20 at 13:15
  • @DavidC with Kudu deployment it works. Then it looks like it is Azure resource group deployment task related issue. – Piotr Perak Jan 23 '20 at 11:29

1 Answers1

0

See Azure Functions ARM template redeployment deletes my published functions for more info. Essentially you need to add this to your template:

{ "name": "WEBSITE_RUN_FROM_PACKAGE", "value": "1" }
kwill
  • 10,867
  • 1
  • 28
  • 26
  • This is different type of deployment. I'm using WebDeploy. Since then I refactored my release pipeline and i works as expected now. I have no idea what was wrong. – Piotr Perak Feb 05 '20 at 07:54
  • @PiotrPerak you went from a none "run from package" to a run from package. You have had a release that was set to "choose best deployment mode" which picked run from package. In your new release you picked the deployment mode your self. – mslot Mar 08 '20 at 20:54