random .NET and web development musings

I’ve already got a bunch of lovely build scripts and remote management scripts that I won’t go into here, what I am going to talk about is how I’ve wired them together using MSDeploy to push my code to my remote server.

First off, install the Web Deployment Tool on your remote machine, but to help prevent haxing don’t put the remote agent service on the default URI. Instead I used a custom port and set up a firewall rule to only allow this port from our office’s IP address. Download the installer and run:

    msiexec /I WebDeploy_x64_en-US.msi /passive ADDLOCAL=ALL LISTENURL=http://+:1234/MSDeploy/

N.B. you can change the port and path to whatever you like.

Here are the installation docs.

Next, goto Services and start “Web Deployment Agent Service”, or run:

    net start msdepsvc

You probably also want to set it to Automatic instead of Manual. Then, set up your MSDeploy script to push your code:

    c:\program files\iis\microsoft web deploy\msdeploy.exe -verb:sync 
        -source:dirPath='d:\some\local\path'         
        -dest:dirPath='f:\some\remote\path',computerName=http://xxx.xxx.xxx.xxx:1234/MSDeploy,
            userName=remoteUser,password=remotePassword -verbose

N.B. “dirPath” is for pushing a whole directory, you can’t use this for a single file (I’m sure there’s a different switch for that though, see the docs, good luck.)

You can then execute a remote command:

    c:\program files\iis\microsoft web deploy\msdeploy.exe -verb:sync 
        -source:runCommand='f:\some\remote\path\somefile.bat',waitInterval=15000,waitAttempts=1
        -dest:auto',computerName=http://xxx.xxx.xxx.xxx:1234/MSDeploy,
            userName=remoteUser,password=remotePassword -verbose

The docs say that “waitAttempts” is the number of subsequent retry attempts so initially set it to 0, but I found that any waitInterval causes a timeout unless I set “waitAttempts” to 1 (presumably anything >0). Crap, but it works.

Note that in this example runCommand will call:

    "C:\Windows\System32\cmd.exe" /c "f:\some\remote\path\somefile.bat"

which means the current directory for the execution of the batch file is “C:\Windows\System32”. If you put:

    cd /d %~dp0

in your batch file, it will change the working dir to the path where the batch file resides. 1337.

Here are some docs.

MSDemply runCommand docs
MSDeploy Command line docs
MSDeploy Command line docs part deux (the bit you actually need)

5 COMMENTS
David
August 10, 2011
ad

Thanks a bunch. These notes saved me a bunch of time screwing around with MsDeploy tonight!

August 10, 2011
ad

Glad I could help :)

David
August 11, 2011
ad

Me again. You would happen to know the answer to this question, would you?

http://stackoverflow.com/questions/7030021/elevated-privileges-required-for-msdeploy-runcommand

August 11, 2011
ad

No sorry, i could never work out how to make it run as a different user. it always runs as Anonymous, despite whatever credentials you give it. :(

David
August 12, 2011
ad

Thanks for the response. For the short term, I’m automating with Powershell scripts using Invoke-Command, which seems to work well enough, though it introduces the dreaded ‘Yet Another Tool Dependency’ problem…

Post a comment