Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automatically publish web application on build from Visual Studio 2015

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!

like image 619
aceanindita Avatar asked Mar 13 '17 10:03

aceanindita


People also ask

How do I Publish a Visual Studio web application?

On the computer where you have the ASP.NET project open in Visual Studio, right-click the project in Solution Explorer, and choose Publish. If you have previously configured any publishing profiles, the Publish pane appears. Click New or Create new profile. Select the option to import a profile.


2 Answers

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>
like image 56
Richard Szalay Avatar answered Sep 21 '22 13:09

Richard Szalay


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

like image 38
Rami A. Avatar answered Sep 19 '22 13:09

Rami A.