Search code examples
.net-coreazure-devopsazure-web-app-serviceazure-pipelines-release-pipelinewebdeploy

Automatize App Offline in Azure App Service and Visual Studio


I want to Make App_Offline.htm Custom and updatable I'm using MSDeploy in order to publish from both Azure Pipeline and Visual Studio 2019. Publish Works fins in Both. My Web Project is on .Net Core 2.2

My problem is that I want to Take The App Offline, BeforePublish and Delete app_offline.htm AfterPublish, Using MSDeploy. For the moment, I EnableMSDeployAppOffline, which shows the default "Under Maintenance" for Seconds. Until App_offline.htm is uploaded in the app service. And then I delete it using Kudu online.

Later I decided to use MSDeploy CMDLine to remotely add and delete file app_offline.htm, beforePublish, and AfterPublish ... Which Works also, I have to save my password in plain text in the release pipeline, and in Visual Studio, in all way "Batch Script, PowerShell Script ...".

My Publish Profile

<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
  <PropertyGroup>
    <WebPublishMethod>MSDeploy</WebPublishMethod>
    <PublishProvider>AzureWebSite</PublishProvider>
    <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
    <LastUsedPlatform>Any CPU</LastUsedPlatform>
    <SiteUrlToLaunchAfterPublish>mySiteUrl</SiteUrlToLaunchAfterPublish>
    <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
    <ExcludeApp_Data>False</ExcludeApp_Data>
    <ProjectGuid>GUID</ProjectGuid>
    <MSDeployServiceURL>myapp.scm.azurewebsites.net:443</MSDeployServiceURL>
    <DeployIisAppPath>increditing-preview</DeployIisAppPath>
    <RemoteSitePhysicalPath />
    <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
    <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
    <EnableMSDeployBackup>True</EnableMSDeployBackup>
    <UserName>$myapp</UserName>
    <_SavePWD>True</_SavePWD>
    <_DestinationType>AzureWebSite</_DestinationType>
    <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>
    <MSDeployUseChecksum>true</MSDeployUseChecksum>
    <MSDeployPath Condition="'$(MSDeployPath)'==''">
       $(Registry:HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\IISExtensions\MSDeploy\3@InstallPath)</MSDeployPath>
    <MSDeployExe Condition=" '$(MSDeployExe)'=='' ">$(MSDeployPath)msdeploy.exe</MSDeployExe>
    <DeleteCMD>
      "$(MSDeployExe)" -verb:delete -dest:contentPath="$(DeployIisAppPath)/App_offline.htm",ComputerName="https://$(DeployIisAppPath).scm.azurewebsites.net/msdeploy.axd?site=$(DeployIisAppPath)",UserName="$(UserName)",Password="Password",IncludeAcls='False',AuthType='Basic'
    </DeleteCMD>
  </PropertyGroup>
  <Target Name="CustomActionsAfterPublish" AfterTargets="AfterPublish">
    <PropertyGroup>
    </PropertyGroup>
    <Message Text="Inside AfterPublish" Importance="high" />

    <!-- MSDeploy does not work  -->
    <!--<MSDeploy Verb="delete"
              Source="@(MsDeploySourceProviderSetting)"
              Destination="contentPath=myapp/App_Offline.htm,computername=$(MsDeployServiceUrl)"
              AllowUntrusted="$(AllowUntrustedCertificate)"
              UseChecksum="$(MSDeployUseChecksum)"
              UserAgent="$(_MSDeployUserAgent)"
              ExePath="$(MSDeployPath)" />-->
     <!-- Executing this command I have to use my Password in Pain Text   -->
    <Exec Command="$(DeleteCMD)" ></Exec>

   
  </Target>
</Project>

Solution

  • https://github.com/projectkudu/kudu/issues/2949

    All I have to do in order to avoid Kudu issue is :

    Add an Application setting in my Azure App Service

    enter image description here

    and add < EnableMSDeployAppOffline >true to my publish profile .

        <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
          <PropertyGroup>
            <WebPublishMethod>MSDeploy</WebPublishMethod>
            <PublishProvider>AzureWebSite</PublishProvider>
            <LastUsedBuildConfiguration>Release</LastUsedBuildConfiguration>
            <LastUsedPlatform>Any CPU</LastUsedPlatform>
            <SiteUrlToLaunchAfterPublish>mySiteUrl</SiteUrlToLaunchAfterPublish>
            <LaunchSiteAfterPublish>True</LaunchSiteAfterPublish>
            <ExcludeApp_Data>False</ExcludeApp_Data>
            <ProjectGuid>GUID</ProjectGuid>
            <MSDeployServiceURL>myapp.scm.azurewebsites.net:443</MSDeployServiceURL>
            <DeployIisAppPath>increditing-preview</DeployIisAppPath>
            <RemoteSitePhysicalPath />
            <SkipExtraFilesOnServer>True</SkipExtraFilesOnServer>
            <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
            <EnableMSDeployBackup>True</EnableMSDeployBackup>
            <UserName>$myapp</UserName>
            <_SavePWD>True</_SavePWD>
            <_DestinationType>AzureWebSite</_DestinationType>
            /// EnableMSDeployAppOffline 
            <EnableMSDeployAppOffline>true</EnableMSDeployAppOffline>
            <MSDeployUseChecksum>true</MSDeployUseChecksum>
    
    </Project>
    

    And check Take App Offline in Azure Release Pipeline: Deploy Azure App Service Task

    enter image description here