Search code examples
asp.net-mvcvisual-studio-2010msdeploywebdeploy

Use MSDeploy to copy buildresult to targetserver


There is a webproject with a batchfile that generates all files needed on the targetserver and puts them in a folder "/Deployable" .

The batch file is quite involved because the project contains a pluginsystem and all plugins need to be copied to a certain location.

When I use webdeploy to deploy to the targetserver it happens what you expect: there are some of the needed assemblies copied over, but not the files as specified in the batchfile.

My plan is now to first execute the batchfile and then use webdeploy to copy the folder "/Deployable" to the targetserver. Can this be done with webdeploy?

This is what I see in Visual Studio deploy menue: enter image description here

This is the resulting publish profile

<?xml version="1.0" encoding="utf-8"?> <publishData>
<publishProfile publishUrl="http://myserver/msdeployagentservice"
deleteExistingFiles="False" 
ftpAnonymousLogin="False" 
ftpPassiveMode="True"
msdeploySite="mysite/" 
msdeploySiteID="" 
msdeployRemoteSitePhysicalPath="" 
msdeployAllowUntrustedCertificate="False" 
msdeploySkipExtraFilesOnServer="False" 
msdeployMarkAsApp="False" 
profileName="publish_to_myserver" 
publishMethod="MSDeploy" 
replaceMatchingFiles="True" 
userName="myuser" 
savePWD="True" userPWD="xxx" SelectedForPublish="True" /> 
</publishData>>


Solution

  • I think there is an ability to add third-party files into webdeploy package by modifying .csproj file, however, I have never had to use it.


    Alternatively, you can easily achieve the same result by using MSDeploy's command-line client and its sync verb, by specifying your /Deployable folder as the -source argument and your target server's msdeploy service as the -dest, e.g.:

    $(WebDeployToolPath)\msdeploy -verb:sync -source:dirPath='Deployable\' -dest:dirPath='$(DeployDirectoryLocalPath)',computerName=$(DeployTargetURL),userName='$(DeployUserName)',password='$(Password)',authType='Basic' -verbose -allowUntrusted
    

    Substitutions:

    • $(WebDeployToolPath) - full path to folder with msdeploy executable (e.g. c:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe )
    • Deployable\ - full path to the folder you wnat to copy to the target server.
    • $(DeployDirectoryLocalPath) - full local path to the target folder on the target server.
    • $(DeployTargetURL) - web deploy service URL (e.g. https://192.168.142.55:8172/MsDeploy.axd or http://myserver/msdeployagentservice)
    • $(DeployUserName) - username to be used for deployment (should be admin for Win 2003)
    • $(Password) - user's password.

    That's it - this command will synchronize Deployable\ folder with $(DeployDirectoryLocalPath) folder (i.e. make the content exactly match).

    You can wrap it into an msbuild target in your .csproj file:

     <PropertyGroup>
         <DeployTargetURL Condition="'$(DeployTargetURL)'==''">https://192.168.142.55:8172/MsDeploy.axd</DeployTargetURL>
         <DeployUserName Condition="'$(DeployUserName)'==''">tergetServer\Administrator</DeployUserName>
         <Password Condition="'$(Password)'==''">AdminPassword</Password>
         <WebDeployToolPath Condition="'$(WebDeployToolPath)'==''">c:\Program Files (x86)\IIS\Microsoft Web Deploy V3\msdeploy.exe</WebDeployToolPath>
    

    <Target Name="Deploy">
     <Exec Command="&quot;$(WebDeployToolPath)&quot;\msdeploy -verb:sync -source:dirPath='Deployable\' -dest:dirPath='$(DeployDirectoryLocalPath)',computerName=$(DeployTargetURL),userName='$(DeployUserName)',password='$(Password)',authType='Basic' -verbose -allowUntrusted " />
    </Target>
    

    And than run it from the command-line in the following way:

    %windir%\Microsoft.NET\Framework\v4.0.30319\MSBuild.exe MyProject.proj /t:Deploy*