How can I pass MSDeploy-style parameters to MSBuil

2019-04-11 04:07发布

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.

3条回答
Summer. ? 凉城
2楼-- · 2019-04-11 04:24

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

查看更多
Luminary・发光体
3楼-- · 2019-04-11 04:26

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.
查看更多
做个烂人
4楼-- · 2019-04-11 04:43
msbuild.exe {build-script.proj}  /property:{someParameter=someValue}

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

查看更多
登录 后发表回答