4

I have an Azure DevOps Pipeline which includes an Azure App Service Deploy task (AzureRmWebAppDeployment) for deploying an ASP.NET Core 3.1 project:

- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'Azure Subscription(01234567-89ab-cdef-0123-456789abcdef)'
    appType: 'webApp'
    WebAppName: 'MyStagingSite'
    packageForLinux: '$(Build.ArtifactStagingDirectory)/**/*.zip'
    enableCustomDeployment: true
    DeploymentType: 'webDeploy'
    enableXmlTransform: false
    enableXmlVariableSubstitution: true
    RemoveAdditionalFilesFlag: false

The Azure App Service destination, however, contains files in several pre-established folders which are managed independent of the continuous delivery process. We would like to use the Remove additional files at destination flag (RemoveAdditionalFilesFlag) while leaving those folders intact.

Disclaimer: I don't consider this a best practice and, in the future, we will be moving these files off to a separate storage location. Until then, I'd like to find a solution that will work resolve this.

In Visual Studio 2019, we achieve this by excluding those files from our publishing process using the MsDeploySkipRules in our csproj file:

<Project Sdk="Microsoft.NET.Sdk.Web">
  …
  <ItemGroup>
    <MsDeploySkipRules Include="CustomSkipFolder">
      <ObjectName>dirPath</ObjectName>
      <AbsolutePath>wwwroot\\Uploads</AbsolutePath>
    </MsDeploySkipRules>
  </ItemGroup>
</Project>

This approach works well for Visual Studio and is honored by its web deployment publishing process. These rules do not appear to be honored by the AzureRmWebAppDeployment task, however, even when using the "Web Deploy" deployment method (DeploymentType).

Is there a way to honor the MsDeploySkipRules when using the AzureRmWebAppDeployment task? If not, if there a way to provide a list of folders which should be skipped or ignored as part of the deployment process? Or, alternatively, if there another task that will permit one of these options when publishing to an Azure App Service?

Note: I also posted this to the DevOps Beta, but as the site hasn't reached critical mass yet, I'm cross-posting it here.

Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77

2 Answers2

7

As @yang-shen-msft notes in the accepted answer, there doesn't appear to be a way to honor the MSDeploySkipRules defined in the csproj file. Instead, files and folders can be skipped by defining the Additional Arguments (AdditionalArguments) parameter of the Azure App Service Deploy (AzureRmWebAppDeployment) task.

Since there doesn't appear to be any official documentation for the -skip rules, and the MSDeploy.exe documentation that Azure Pipelines references is out-of-date, the following provides additional details.

Translating the csproj format

First, it's useful to recognize that when you deploy a project via Visual Studio, it's simply taking the MSDeploySkipRules configured in the csproj file and adding them to its internal call of msdeploy.exe as -skip rules. So, given the following rule defined in the csproj:

<Project Sdk="Microsoft.NET.Sdk.Web">
  …
  <ItemGroup>
    <MsDeploySkipRules Include="CustomSkipFolder">
      <ObjectName>dirPath</ObjectName>
      <AbsolutePath>wwwroot\\Uploads</AbsolutePath>
    </MsDeploySkipRules>
  </ItemGroup>
</Project>

The -skip rule is interpreted as:

msdeploy.exe -skip:objectName=dirPath,absolutePath=wwwroot\\Uploads

Translating this to the Azure App Service Deploy (AzureRmWebAppDeployment) task, the resulting yml might look like:

- task: AzureRmWebAppDeployment@4
  inputs:
    ConnectionType: 'AzureRM'
    azureSubscription: 'Azure Subscription'
    appType: 'webApp'
    WebAppName: 'MyStagingSite'
    packageForLinux: '$(Build.ArtifactStagingDirectory)/**/*.zip'
    enableCustomDeployment: true
    DeploymentType: 'webDeploy'
    enableXmlTransform: false
    enableXmlVariableSubstitution: true
    RemoveAdditionalFilesFlag: true
    AdditionalArguments: '-skip:objectName=dirPath,absolutePath=wwwroot\\Uploads'

Note that multiple -skip rules can be defined on the same msdeploy.exe call.

    AdditionalArguments: '-skip:objectName=dirPath,absolutePath=wwwroot\\Uploads -skip:objectName=dirPath,absolutePath=wwwroot\\Downloads'

-skip Rules Documentation

Unfortunately, as mentioned above, there doesn't appear to be any official, first-party documentation for the -skip rules on msdeploy.exe. The 2014 documentation acknowledges them, and provides two examples, but doesn't expand on the options. That said, way back in 2012, @richard-szalay wrote a useful article, "Demystifying MSDeploy skip rules", which provides a lot of details for anyone requiring additional control.

Additional Notes

  • The ObjectName refers to the Web Deploy Provider, which can be used to update far more than just the file system.

  • I am setting ObjectName to the dirPath Web Deploy Provider so that I can skip entire directories. If you want to skip files, use filePath instead.

  • Both dirPath and filePath accept regular expressions in the AbsolutePath, potentially allowing repetitive rules following a consistent pattern to be consolidated.

  • The AbsolutePath is actually relative to your publishing root—e.g., D:\home\site\wwwroot on an Azure App Service.

  • As I am publishing an ASP.NET Core site, my static files are, by convention, in a nested wwwroot folder, thus my setting:

    absolutePath=wwwroot\\Uploads
    

    actually maps to:

    D:\home\site\wwwroot\wwwroot\Uploads
    
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • On Kudu, this is my path: `D:\home\site\wwwroot`. So would it be `D:\home` instead of `dirPath` and `site\wwwroot` instead of `wwwroot\Uploads`? – Cataster Jul 06 '21 at 04:23
  • @Cataster: Good question. My base path is the same as yours. The `dirPath` argument represents the [Web Deploy Provider](https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd569040(v=ws.10)); if you want to skip a directory, you likely want to stick with `dirPath`—or `filePath` if you want to skip an individual file. The `dirPath` _starts_ at `wwwroot`. I am using ASP.NET Core, so there’s _another_ `wwwroot` under `wwwroot`; as such, my `absolutePath=wwwroot\\Uploads` actually maps to `D:\home\site\wwwroot\wwwroot\Uploads`. Hope this helps! – Jeremy Caney Jul 06 '21 at 07:31
  • @Cataster: As an aside, it is my understanding that `dirPath` also accepts regular expressions, so if there’s a predictable pattern to the directories you want to skip, you may be able to consolidate them into a single rule. I haven’t had the need for this, so haven’t confirmed the functionality or idiosyncrasies of the syntax, but it’s useful to be aware of. – Jeremy Caney Jul 06 '21 at 07:36
  • Thanks jeremy. So since I'm skipping files, i set the web deploy provider to filePath, and set absolutePath to wwwroot\\web.dev.config, but the file wasnt skipped...so I'm guessing absolutePath would be the file I'm skipping directly? Since mine is not .NET Core? – Cataster Jul 06 '21 at 15:35
  • 1
    @Cataster: Since you're _not_ using .NET Core, you'll probably just want to set `absolutePath` to `web.dev.config`. That would map to `D:\home\site\wwwroot\web.dev.config`. – Jeremy Caney Jul 06 '21 at 20:59
  • Jeremy, setting the absolutePath to the file directly worked!!! Thank you so much!! :D btw, how do i skip files using wildcard? For example, `*.zip` but that resulted in error `Error: Error: The regular expression '*.zip' is invalid. Error: parsing "*.zip" - Quantifier {x,y} following nothing. Error count: 1.` – Cataster Jul 07 '21 at 17:42
  • 1
    nvm i figured it out, i had to use \\ along with the wildcard `\\*.zip`. – Cataster Jul 07 '21 at 18:13
  • 1
    @Cataster: Glad to hear! FYI: I’ve now updated my answer to include notes from this exchange so it’s easy for future readers to get this information. – Jeremy Caney Jul 07 '21 at 20:08
  • 1
    Once again thank you very much Jeremy, you really nailed it down while MSFT lounging around for the past 3 days since a question I posted had been opened. At the very least I can reward you with an upvote :) – Cataster Jul 07 '21 at 20:53
1

Is there a way to honor the MsDeploySkipRules when using the AzureRmWebAppDeployment task?

Not sure if there's a way to apply MsDeploySkipRules but there's exising way to skip specidic files during the deplyment process.

For detailed information, please check Exclude/Skip files in VSTS Build and release .

As suggested in the above thread, add additional arguments to your task to skip the specific files.

Fill your filepath in the skip argument. More information at Azure App Service Deploy task (press Ctrl+ F to search for additional arguments) and Web Deploy Operation Settings for the syntax.

Please have a try and hope this could help.

Yang Shen - MSFT
  • 1,136
  • 1
  • 7
  • 9
  • Does this help skip or ignore your folders? If so, you can accept it. Or if you met some issues when implementing it, please fell free to tell. Thanks! – Yang Shen - MSFT Mar 13 '20 at 08:46
  • I was definitely hoping to find a way to centralize this configuration, but at least this gives me an opportunity to reinstate `RemoveAdditionalFilesFlag` without worrying about these exceptions. Thank you! I've implemented this and confirmed that it works as expected. I also apologize for taking so long to mark this as the accepted answer; I wanted to confirm that it worked before doing so. – Jeremy Caney Mar 20 '20 at 19:34
  • Aside, do you know where the latest documentation for `msdeploy.exe` is? I've been unable to find it, and every source (including the Azure Pipelines task) points to the 2014 documentation you linked to. The issue is it doesn't actually document the parameters or syntax for using the `-skip:` rules. I was able to infer this based on my `csproj`'s `` directives, and I've found [third-party documentation](https://blog.richardszalay.com/2012/12/17/demystifying-msdeploy-skip-rules/), but can't find official documentation from Microsoft. – Jeremy Caney Mar 20 '20 at 19:45