Unlike publishing Web projects, automating the publishing of Windows service projects requires the following issues to be addressed:
- How do I stop and start a service remotely? You need to stop the service before publishing and start the service after the release is complete.
- How do I upload a compiled file to a target server?
Issue 1: How to stop and start a service remotely
Before MSBuild, add an Execute Windows Batch command, execute the cmd command, and the cmd command is as follows:
echo **********stop remote server Windows service**********"C:\Program files\iis\microsoft Web Deploy v3\ Msdeploy.exe "-verb:sync-source:runcommand="net stop ubtripws_business" - Dest:auto,computername=192.168. 1.21, username=administrator,[email protected]123
The Msdeploy sync operation is used here to execute the CMD command on the target server via RunCommand.
Problem:
If the service is currently in a stopped state, running RunCommand (net stop ubtripws_business) will cause an error.
Issue 2: How to upload a compiled file to the target server
After MSBuild, add an Execute Windows Batch command, execute the cmd command, and the cmd command is as follows:
Echo ********** has three paragraphs below,1. Presync: Kill Process First,2. Synchronizing local and remote,3. Postsync: Last start service **********"C:\Program files\iis\microsoft Web Deploy v3\msdeploy.exe"-verb:sync-presync:runcommand="taskkill/f/im ssharing.ubtrip.winservice.exe/t", waitattempts= -, waitinterval= +-source:contentpath=%workspace%\dev\ubtrip\ssharing.ubtrip.winservice\bin\debug\-dest:contentpath=C:\ Windowsservices\ubtripjob\,computername=192.168.1.21, Username=administrator,[email protected]123-enablerule:donotdeleterule-postsync:runcommand="net start ubtripws_business", waitattempts= -
Use the Msdeploy sync operation to execute the CMD command on the target server via RunCommand. Presync refers to a command that runs before a file is copied, Postsync is a command that runs after a file is copied.
Attention:
Msdeploy Contentpath Default is to delete the target server's files and then add, but we do not want to delete the original file, so we need to add parameter-enablerule:donotdeleterule.
Automatically exclude Web. config and app. Config
1,windows Service Project
By adding parameter-skip to Msdeploy, the command is as follows:
-skip:objectname=filepath,absolutepath=web\.config,skipaction=update
2,web Project
Because the Web project is built by Msbuild+msdeploy service, there is no way to add-skip parameters to msdeploy like a Windows Service project, and the solution for a Web project is to Modify the site's Csproj project file and add a target to tell MSBuild to automatically exclude the Web. config file when it is built, with the following command:
<!--tell MSBuild to exclude the Web. config file when publishing - <TargetName= "Customexcludefiles"beforetargets= "Excludefilesfrompackage"> <ItemGroup> <ExcludefrompackagefilesInclude= "Web. config"> </Excludefrompackagefiles> </ItemGroup> </Target>
Full build configuration included
1,windows Service Project
MSBuild before cmd command:
1echo **********begin restore NuGet package**********2C:\mcgrady\tools\nuget.exe Restore"%workspace%\dev\ubtrip\ssharing.ubtrip.sln"-source https://www.nuget.org/api/v2/3echo **********stop remote Server Windows service**********4 "C:\Program files\iis\microsoft Web Deploy v3\msdeploy.exe"-verb:sync-source:runcommand="net stop ubtripws_business"-dest:auto,computername=192.168.1.21, Username=administrator,[email protected]123
View Code
MSBuild parameters:
MSBuild after cmd command:
2,web Project
MSBuild before command:
MSBuild parameters:
Resources
1, Official Document: https://docs.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/dd569106 (v=ws.10)
2,msdeploy Skip Rules:https://blog.richardszalay.com/2012/12/17/demystifying-msdeploy-skip-rules/
3. How to prevent web. config from being updated when using MSDeploy to deploy the Web site: https://blog.miniasp.com/post/2010/09/01/ Msdeploy-skip-command-for-webconfig-file.aspx
Automatically publish a Windows Service project using Jenkins