使用Jenkins自动发布Windows服务项目

使用Jenkins自动发布Windows服务项目

不同于发布Web项目,自动发布Windows服务项目需要解决以下几个问题:

  1. 如何远程停止和开启服务?需要在发布前停止服务,在发布完成后开启服务。
  2. 如何上传编译文件到目标服务器?

问题1:如何远程停止和开启服务

在msbuild之前添加一个execute windows batch command,执行cmd命令,cmd命令如下:

echo **********stop remote server windows service**********
"C:Program FilesIISMicrosoft Web Deploy V3msdeploy.exe" -verb:sync -source:runCommand="net stop UbtripWs_Business" -dest:auto,computername=192.168.1.21,username=administrator,password=P@ssw0rd.123

这里使用的是msdeploy的sync操作,通过runCommand在目标服务器上执行cmd命令。

注意:

如果服务当前是已停止状态,运行runCommand (net stop UbtripWs_Business)就会报如下错误,所以先要保证服务是已启动状态,然后再构建!

image

问题2:如何上传编译文件到目标服务器

在msbuild之后添加一个execute windows batch command,执行cmd命令,cmd命令如下:

echo **********以下内容有三段,1.preSync:先Kill进程,2.同步本地与远程,3.postSync:最后启动服务**********
"C:Program FilesIISMicrosoft Web Deploy V3msdeploy.exe" -verb:sync -preSync:runCommand="TASKKILL /F /IM SSharing.Ubtrip.WinService.exe /T",waitAttempts=30,waitInterval=1000  -source:contentpath=%WORKSPACE%DEVUbtripSSharing.Ubtrip.WinServiceinDebug -dest:contentpath=C:WindowsServicesUbtripJob\,computername=192.168.1.21,username=administrator,password=P@ssw0rd.123 -enableRule:DoNotDeleteRule -postSync:runCommand="net start UbtripWs_Business",waitAttempts=20

使用msdeploy的sync操作,通过runCommand在目标服务器上执行cmd命令。preSync指在复制文件之前运行的命令,postSync是复制文件之后运行的命令。

注意:

1,虽然在msbuild之前已经执行cmd命令停止服务了,但是有的时候进程还在,这样会导致覆盖文件失败,所以需要在上传文件之前运行TASKKILL命令结束进程。

2,由于msdeploy默认的skip策略是删除在源服务器上不存在的文件,增加在目标服务器上不存在的文件,更新源和目标同时存在的文件,所以这个会导致目标服务器上的一些配置目录被删除,但是这个是我们不希望看到的,所以需要添加参数 -enableRule:DoNotDeleteRule,意思从不删除目标服务器上的文件,只做新增和更新操作。

自动排除Web.config和App.config

1,windows服务项目

通过给msdeploy添加参数-skip,命令如下:

-skip:objectName=filePath,absolutePath=App.config,skipAction=Update

2,web项目

由于web项目的构建是通过msbuild+msdeploy service的方式进行的,所以没有办法像windows服务项目那样给msdeploy添加-skip参数,对于web项目的解决方案是,修改站点的csproj项目文件,添加一个Target来告诉msbuild,构建的时候就自动排除Web.config文件,命令如下:

<!--发布的时候告诉msbuild排除Web.config文件-->
  <Target Name="CustomExcludeFiles" BeforeTargets="ExcludeFilesFromPackage">
    <ItemGroup>
      <ExcludeFromPackageFiles Include="Web.config">
      </ExcludeFromPackageFiles>
    </ItemGroup>
  </Target>

附完整构建配置

1,Windows服务项目

image

msbuild之前cmd命令:

echo **********begin restore nuget package**********
C:mcgrady	ools
uget.exe restore "%WORKSPACE%DEVUbtripSSharing.Ubtrip.sln" -source https://www.nuget.org/api/v2/
echo **********stop remote server windows service**********
"C:Program FilesIISMicrosoft Web Deploy V3msdeploy.exe" -verb:sync -source:runCommand="net stop UbtripWs_Business" -dest:auto,computername=192.168.1.21,username=administrator,password=xxxxxx

msbuild参数:

/t:Rebuild
/p:Configuration=Debug
/p:VisualStudioVersion=12.0
/p:ExcludeGeneratedDebugSymbol=false 
/p:ExcludeXmlAssemblyFiles=false

msbuild之后cmd命令:

echo **********以下内容有三段,1.preSync:先Kill进程,2.同步本地与远程,3.postSync:最后启动服务**********
"C:Program FilesIISMicrosoft Web Deploy V3msdeploy.exe" -verb:sync -preSync:runCommand="TASKKILL /F /IM SSharing.Ubtrip.WinService.exe /T",waitAttempts=30,waitInterval=1000  -source:contentpath=%WORKSPACE%DEVUbtripSSharing.Ubtrip.WinServiceinDebug -dest:contentpath=C:WindowsServicesUbtripJob\,computername=192.168.1.21,username=administrator,password=xxxxxx -enableRule:DoNotDeleteRule -skip:objectName=filePath,absolutePath=App.config,skipAction=Update -postSync:runCommand="net start UbtripWs_Business",waitAttempts=20

2,Web项目

image

msbuild之前命令:

C:mcgrady	ools
uget.exe restore "%WORKSPACE%DEVUbtripSSharing.Ubtrip.sln" -source https://www.nuget.org/api/v2/

msbuild参数:

/t:Rebuild
/p:VisualStudioVersion=12.0
/p:DeployOnBuild=True 
/p:SkipExtraFilesOnServer=True 
/p:WarningLevel=4 
/p:NoWarn=1591 
/p:DeployTarget=MSDeployPublish 
/p:MSDeployPublishMethod=WMSVC 
/p:AllowUntrustedCertificate=True 
/p:MsDeployServiceUrl=https://192.168.1.21:8172/msdeploy.axd
/p:username=webserver-devadministrator
/p:password=xxxxxx
/p:DeployIisAppPath=Ubtrip
/p:Configuration=Debug 
/p:ExcludeGeneratedDebugSymbol=false 
/p:ExcludeXmlAssemblyFiles=false

参考资料

1,官方文档: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,使用 MSDeploy 手動部署網站時如何避免 Web.config 被更新:https://blog.miniasp.com/post/2010/09/01/MSDeploy-Skip-Command-for-Webconfig-file.aspx

4,Jenkins使用教程之管理节点:

原文地址:https://www.cnblogs.com/y593216/p/13895079.html