Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AfterPublish script doesn't run when I publish a web app

I've got an ASP.NET web app that I publish to our website when I make changes or fix bugs. We want to automatically save a backup of the project files to our server (separate from the SVN check in), so I wrote a batch file to copy the entire project from my local drive to the server. The batch file works properly when run stand alone, so that's not the problem. The batch file is not in the path of the project if that's a concern.

Then I added these lines to my .csproj file right above the closing </project> tag:

<Target Name="AfterPublish" >
    <Exec Command="C:\deg\bat\backupRMA.cmd" />
</Target>

This follows the instructions in MSDN for overriding a target.

I have also tried the method outlined in this SO question. Which looked like this:

<Target Name="BackUpRMAToIDrive" AfterTargets="MSDeployPublish" >
    <Exec Command="C:\deg\bat\backupRMA.cmd" />
</Target>

That doesn't work either.

For completeness, here's the batch file, it's pretty simple, but I can explain the switches if anyone's interested:

xcopy C:\deg\ASP.NET\OnlineRMA_SinglePage\*.* /cherkyDi I:\common\AppDevBranch\Service\rma

I publish in VS2010 using Build > Publish RMA, which works fine. It's just the backup script never runs. The only commonality is the script itself. I've seen other examples that use a copy from source dir to destination dir, but I thought I understood that you could just call an external script through the Exec command? Here's the example SO question I found for that method.

I'm certain I'm missing something obvious, but I'm onto my second day of messing around with something that seems pretty straight forward, so what am I missing?

like image 771
delliottg Avatar asked Oct 18 '13 17:10

delliottg


People also ask

How do I publish a Visual Studio Web app?

In Solution Explorer, right-click your project and choose Publish. If you're publishing this web app for the first time, next you see the Publish wizard. Visual Studio filters the list of destinations depending on the type of web app.

What command does Visual Studio use to publish?

Basic command-line publishing The default publish folder format is bin\Debug\{TARGET FRAMEWORK MONIKER}\publish\. For example, bin\Debug\netcoreapp2. 2\publish\. The dotnet publish command calls MSBuild, which invokes the Publish target.

What is a Pubxml file?

pubxml file contains settings that apply to one publish profile. The values you enter in the Publish Web wizard are stored in these files. Creating a .wpp.targets File. When you want to configure settings that apply to all profiles you use in a project, you create a . wpp.

How do I change the publish path in Visual Studio?

To specify a publishing location With a project selected in Solution Explorer, on the Project menu, click Properties. Click the Publish tab. In ClickOnce for . NET Core 3.1 and .


2 Answers

When you run the cmd file manually, the script surely runs with administrator privileges, or enough privileges to succesfully copy the files to the destination (in the I: unit in this case).

The thread that executes the script from Visual Studio probably has very restricted privileges, due to security reasons.

Your best bet, is run the script from powershell, passing the following argument to it:

-ExecutionPolicy Unrestricted

Your XML will look like this:

<Target Name="CustomPostPublishActions" AfterTargets="MSDeployPublish">
  <PropertyGroup>
    <PowerShellExe Condition=" '$(PowerShellExe)'=='' "> 
      %WINDIR%\System32\WindowsPowerShell\v1.0\powershell.exe
    </PowerShellExe>
    <ScriptLocation Condition=" '$(ScriptLocation)'=='' ">
      C:\deg\bat\backupRMA.cmd
    </ScriptLocation>
  </PropertyGroup>
  <Exec Command="$(PowerShellExe) -NonInteractive -executionpolicy Unrestricted 
                 -command &quot;&amp; invoke-command -scriptblock { 
                          &amp;&apos;$(ScriptLocation)&apos; 
                          }
                          &quot;"/>  
</Target>

You can also check this MSDN link for similar commands:

http://www.asp.net/web-forms/tutorials/deployment/advanced-enterprise-web-deployment/running-windows-powershell-scripts-from-msbuild-project-files

like image 103
canolucas Avatar answered Nov 15 '22 14:11

canolucas


I saw a comment from a MS developer stating that publishing that goes to the 'File System' does not call MSDeployPublish target. You can see that here.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/60b196e9-28e1-4d9d-b464-d57e42353754/vs2012-afterpublish-for-web-application?forum=msbuild

http://sedodream.com/PermaLink,guid,b352f04a-2449-4cbb-8125-7acdef9552e1.aspx

Digging around in the *.targets files there were a few targets I could place in the AfterTargets attribute that accomplished running after the publish had copied to the temporary obj output folder. I could never find one that ran after copying to the actual folder defined in the publish profile.

Here is what I think you would need:

  <Target Name="BackUpRMAToIDrive" AfterTargets="CopyAllFilesToSingleFolderForPackage">
    <Exec Command="C:\deg\bat\backupRMA.cmd" />
  </Target>

I'm not sure what you are backing up though - the original source or the published source. At the time of this Exec, if you need the path to the 'published files', you can pass in a parameter to your script via $(WebProjectOutputDir)\$(WPPAllFilesInSingleFolder). This would be something like c:\{path to *.csproj}\obj\{build configuration}\Package\PackageTmp.

like image 38
Terry Avatar answered Nov 15 '22 14:11

Terry