8

Is there any way to automatically have a web application published using a pre-created publish profile on successful build? I don't want to have to click the publish icon, need this to happen on successful build of the web project, on Visual Studio 2015 - without using macros.

Any samples would be appreciated!

aceanindita
  • 484
  • 1
  • 4
  • 11

3 Answers3

9

Add the following to your project file:

<Target Name="AfterBuild">
  <MSBuild Condition="'$(DeployOnBuild)'!='true'" Projects="$(MSBuildProjectFullPath)" Properties="DeployOnBuild=true;PublishProfile=Local;BuildingInsideVisualStudio=False"/>
</Target>

The value of PublishProfile (Local in the example above) is the name of the publish profile you want to run.

Sources:
https://www.dotnetcatch.com/2017/03/24/publish-webdeploy-automatically-with-vs-build/
https://stackoverflow.com/a/41830433/90287

Jonas Äppelgran
  • 2,617
  • 26
  • 30
Rami A.
  • 10,302
  • 4
  • 44
  • 87
  • 2
    This solution worked good for me. I used `` and modified the Condition of the MSBuild task to only run on Release builds: `Condition=" '$(DeployOnBuild)' != 'true' and '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "`. – Jonas Äppelgran Dec 09 '19 at 08:46
9

Rami's solution works, but it requires another "Build" pass. While this won't actually recompile, it will still cause unnecessary delay if your solution is large.

You can't trigger the web publish via DeployOnBuild as that's automatically disabled when building from Visual Studio.

You can, however, trigger the process as part of the same MSBuild invocation via some MSBuild trickery:

<!-- In Directory.Build.props or the csproj (before the web targets import) -->
<PropertyGroup>
  <PublishProfile>Local</PublishProfile>
</PropertyGroup>

And also:

<!-- After the above, or in ProjectName.wpp.targets -->
<PropertyGroup>
  <AutoPublish Condition="'$(AutoPublish)' == '' and '$(Configuration)' == 'Debug' and '$(BuildingInsideVisualStudio)' == 'true' and '$(PublishProfile)' != ''">true</AutoPublish>

  <AutoPublishDependsOn Condition="'$(AutoPublish)' == 'true'">
    $(AutoPublishDependsOn);
    WebPublish
  </AutoPublishDependsOn>
</PropertyGroup>

<Target Name="AutoPublish" AfterTargets="Build" DependsOnTargets="$(AutoPublishDependsOn)">
</Target>

If you're finding that your publish project isn't being built when you make content changes, add the following:

<!-- csproj ONLY, won't work elsewhere -->
<PropertyGroup>
  <DisableFastUpToDateCheck>true</DisableFastUpToDateCheck>
</PropertyGroup>
Richard Szalay
  • 83,269
  • 19
  • 178
  • 237
  • I have implemented your solution on a VB ASP.NET web site and indeed when the site is being built it is also deployed to my local folder. However, although my publish profile specifies to delete the existing files `True` the setting is ignored. This means that when I publish from the VS UI files are deleted on the target folder but when using your code existing files remain (MSBuild in it's output also says it is skipping existing unmodified files and is also not deleting files in the target directory that are no longer published). Any ideas? – Dani Avni Mar 24 '21 at 14:33
2

Try like below,

msbuild mysln.sln /p:DeployOnBuild=true /p:PublishProfile=<profile-name>

you have to pass following as build parameter in the project property.

/p:DeployOnBuild=true 
/p:PublishProfile=<profile-name>
  • Thanks, I tried adding these build params in the csproj file and then build the project from the IDE, but that didnt work as expected. ... ... true sitecorejumpstart Any ideas? – aceanindita Mar 13 '17 at 16:14
  • Can you check this link https://social.msdn.microsoft.com/Forums/vstudio/en-US/c2d10c74-ed44-4635-acb9-ab08612701e2/deployonbuild-not-working?forum=tfsbuild – kabilan Mohanasundaram Mar 14 '17 at 04:24
  • Also can you share the output text(from the output window), that you got when you try to build. – kabilan Mohanasundaram Mar 14 '17 at 04:27
  • Thank you for the link, I looked there, but the issue mentioned there seems to be with a server deployment where visual studio wasnt installed. Here's the diagnostic output from the project build. I'd appreciate any help! https://www.dropbox.com/s/eb9ho6eugra2zn5/DiagnosticBuildOutput.txt?dl=0 – aceanindita Mar 15 '17 at 08:29
  • 1
    Also, here's the project file https://www.dropbox.com/s/tollt67muzcatm4/Epsilon.Web.Shared.txt?dl=0 – aceanindita Mar 15 '17 at 08:38