Search code examples
msbuildvisual-studio-2022azure-webjobs

Conditionally include file during publish to Azure WebJob


I have a webjob on Azure that I want to conditionally include the "settings.job" file based on the build configuration in Visual Studio 2022. My current pubxml file is as follows:

<Project>
    <PropertyGroup>
        <WebPublishMethod>MSDeploy</WebPublishMethod>
        <PublishProvider>AzureWebSite</PublishProvider>
        <LastUsedBuildConfiguration>Nightly</LastUsedBuildConfiguration>
        <LastUsedPlatform>Any CPU</LastUsedPlatform>
        <SiteUrlToLaunchAfterPublish>https://myfakeapp.azurewebsites.net</SiteUrlToLaunchAfterPublish>
        <LaunchSiteAfterPublish>false</LaunchSiteAfterPublish>
        <ResourceId>my/fake/resourceid</ResourceId>
        <UserName>$myfakeusername</UserName>
        <_SavePWD>false</_SavePWD>
        <ExcludeApp_Data>false</ExcludeApp_Data>
        <MSDeployServiceURL>myfakeapp.scm.azurewebsites.net:443</MSDeployServiceURL>
        <MSDeployPublishMethod>WMSVC</MSDeployPublishMethod>
        <SkipExtraFilesOnServer>true</SkipExtraFilesOnServer>
        <EnableMsDeployAppOffline>true</EnableMsDeployAppOffline>
        <EnableMSDeployBackup>true</EnableMSDeployBackup>
        <DeployIisAppPath>myfakeapp</DeployIisAppPath>
        <WebJobType>Triggered</WebJobType>
        <WebJobName>MyFakeApp_Nightly_Job</WebJobName>
        <TargetFramework>net6.0</TargetFramework>
        <SelfContained>false</SelfContained>
    </PropertyGroup>
    <Target Name="CopyFiles" BeforeTargets="Publish">
        <ItemGroup>
            <JobFile Include="Settings.job" />
        </ItemGroup>
        <Copy SourceFiles="@(JobFile)" DestinationFolder="$(OutDir)" Condition=" '$(Configuration)' == 'Nightly' " SkipUnchangedFiles="false" />
    </Target>
</Project>

This works locally, in that the settings.job file is conditionally copied to the output folder, but the file is not published to Azure. I have tried multiple combinations of BeforeTargets & AfterTargets : Build, Publish, PrepareForPublish all to no avail.

The only way I can get the file to publish out to Azure is if I set it to always copy in the project file:

<ItemGroup>
    <None Update="Settings.job">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
    </None>
</ItemGroup>

Why does the conditionally copied file not get included in the publish to Azure? Has anyone been able to get this concept working?


Solution

  • I finally figured out how to make the conditional publish work. I just moved the condition check to the project file instead of trying to make it happen in the pubxml file:

    <ItemGroup>
      <None Update="Settings.job" Condition="'$(Configuration)' == 'Nightly'">
        <CopyToOutputDirectory>Always</CopyToOutputDirectory>
      </None>
    </ItemGroup>