How can I pass MSDeploy-style parameters to MSBuild via the commandline?
Asked Answered
R

3

4

I am setting up TeamCity to deploy our Website Project application (using a *.wdproj) and Web Deploy application to IIS.

I have a build configuration that uses MSBuild.exe with the MSDeployPublish to build and then deploy the application.

We now want to get the application to deploy to multiple target environments, therefore need a way to supply different settings based on the target environment.

I have added a parameters.xml file to the Web Deployment Project, and have verified that the parameters set in here are making all the way through the target IIS server and being correctly applied - great!

Now what I want to do is have different parameter settings per environment. I was hoping I could use something like the MSDeploy.exe -setParam argument to specify different values for each environment, however I can find no way to get my parameter values into MSBuild via the commandline.

I suspect I might need to do one of the following:

  1. Split MSBuild and MSDeploy into separate build steps.

  2. Configure a task somewhere in the pipeline to take 1 of n versions of parameters.something.xml and move it into parameters.xml so it gets picked up by the rest of the pipeline.

I'm looking for the simplest way to move ahead at this point, any suggestions welcome.

For reference, here is the command I'm experimenting with now:

msbuild /target:MSDeployPublish MySite_deploy.wdproj /P:Configuration=Debug
/P:DeployOnBuild=True /P:DeployTarget=MSDeployPublish
/P:MsDeployServiceUrl=www.myserver.com:8172/MsDeploy.axd
/P:AllowUntrustedCertificate=True /P:MSDeployPublishMethod=WMSvc
/P:CreatePackageOnPublish=True /P:UserName=MyUser /p:Password=MyPassword
/P:DeployIisAppPath=www.myserver.com/MySite
/P:ServerURL=http://www.tryingtoforcethis.com

It's working beautifully except the value for ServerURL, which is a parameter I've defined in my parameters.xml, is not making its way into the target site. The default I specified in parameters.xml, however, is. So I know that the parameters are working, I just can't figure out how to add them to the msbuild commandline.

Ruscher answered 22/3, 2013 at 4:36 Comment(0)
R
3

Well, I think the short answer is that with MSBuild 4.0 and VS2010, you can't just pass arbitrary parameters into MSDeployPublish from the call to MSBuild.

I found these posts helpful:

http://forums.iis.net/t/1167657.aspx/1 - Ming Chen's comments

http://www.hanselman.com/blog/TinyHappyFeatures3PublishingImprovementsChainedConfigTransformsAndDeployingASPNETAppsFromTheCommandLine.aspx - the comments from Richard Szalay at the bottom

After reading these, and sifting through the Microsoft.Web.Publishing.targets file for some time trying to find a "way in", I finally settled on having multiple copies of Parameters.xml in my project folder in source control, labelled according to their environment eg:

  • Parameters.Test.xml
  • Parameters.Staging.xml
  • Parameters.Live.xml

Then, prior to the package and deploy, I just copy one of these files into Parameters.xml and it gets picked up by the rest of the pipeline - done!

BTW I had a temporary problem getting the parameters.xml copy and subsequent cleanup to work within a single MSBuild.exe call due to what seems to be some sort of file access issue, I've detailed it here:

MSBuild.exe Copy task not working properly unless a version of the file already appears in target

Ruscher answered 23/3, 2013 at 21:55 Comment(2)
I did something similar to this a while ago. I used the msbuild command and did the config transforms and set the publish settings for each configuration. But I still needed a few more tweaks because we had 8 qa sites and I didn't want 8 qa config transforms, so I after the msbuild task I copied the appropriate params.xml file (i had one teamcity build configuration for each qa site), and then did msdeploy, which is where you could add your skip, etc commands. It sounds like you are good to go now, or are there any hurdles left?Prepuce
I am good to go! It may not be the most elegant, but it's working for now. Phew. I'm closing the door on this part of the project. :-)Ruscher
M
2

To answer your question, the parameterization of your command line is not a concern of MSBuild. Instead, you should utilize external tools. For example, if you run your msbuild command from a batch file you could pass the parameters to the batch file and run it for each environment with different parameters. Another approach is to use a build system like TeamCity or VSTS and utilize their parameterization mechanism. Adapted for the VSTS or TFS, your command could look like this:

msbuild MySite_deploy.wdproj /target:MSDeployPublish /p:Configuration=Debug
/p:DeployOnBuild=True /p:MsDeployServiceUrl=$(IIsHostNameIp)
/p:AllowUntrustedCertificate=True /p:MSDeployPublishMethod=WMSvc
/p:CreatePackageOnPublish=True /p:UserName=$(IIsUserName) /p:Password=$(IIsPassword)
/p:DeployIisAppPath=$(IIsSite)

In addition, I would suggest some clean up for your origianl command line:

  1. Using both /p:target and /p:DeployTarget is redundant. Any one of them is enough. Also it could be replaced with /p:WebPublishMethod.
  2. For /p:MSDeployServiceUrl it is enough to only provide a DNS name or IP. the port and the Url is automatically derived from the /p:MSDeployPublishingMethod=WMSVC.
  3. The custom parameter /p:ServerURL is unknown and won't be mapped anywhere.
Murguia answered 1/5, 2017 at 15:15 Comment(1)
Thanks for your answer. We have moved on significantly since my original experiments. We now have distinct build, package and deploy steps in TeamCity. The deploy step uses msdeploy.exe and the per-environment paramters XML file. Holding the parameters in XML under source control has proven to be excellent to track and control new settings needed at deploy time.Ruscher
B
0
msbuild.exe {build-script.proj}  /property:{someParameter=someValue}

In your build script you can use $(someParameter) as a variable

Borage answered 22/3, 2013 at 5:1 Comment(5)
My parameter is called ServerURL. If I specify the msbuild /property argument, it doesn't seem to take effect, but my default value in parameters.xml does. It appears to me that simply passing a parameter to MSBUILD.EXE doens't guarantee it will be used by MSDEPLOY. Also I think it would need to be someParameter=someValue, i.e. equal sign not a colon.Ruscher
I assume you are importing your xml file. In that case after you import the file, you need to override the value. <PropertyGroup> <ServerURL>$(ServerURLFromCommandLine)</ServerURL> </PropertyGroup>Borage
Parameters.xml seems to be exclusively for the use of the MSDeployPublish target, it doesn't contain <PropertyGroup> elements etc. and I don't think it's processed by MSBuild.Ruscher
Please take a look at this solution (code.msdn.microsoft.com/Deploying-Web-Applications-9d9093c0)Borage
Thanks for your suggestion Syam - I hadn't seen that sample before, I will take a look at it when I get a moment. For now I am accepting the answer I arrived at because it was the closest to where I'd gotten to when I got stuck. Thank you.Ruscher

© 2022 - 2024 — McMap. All rights reserved.