1. 程式人生 > 實用技巧 >使用Jenkins自動釋出Windows服務專案

使用Jenkins自動釋出Windows服務專案

使用Jenkins自動釋出Windows服務專案

不同於釋出Web專案,自動釋出Windows服務專案需要解決以下幾個問題:

  1. 如何遠端停止和開啟服務?需要在釋出前停止服務,在釋出完成後開啟服務。
  2. 如何上傳編譯檔案到目標伺服器?

問題1:如何遠端停止和開啟服務

在msbuild之前新增一個execute windows batch command,執行cmd命令,cmd命令如下:

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]

這裡使用的是msdeploy的sync操作,通過runCommand在目標伺服器上執行cmd命令。

注意:

如果服務當前是已停止狀態,執行runCommand (net stop UbtripWs_Business)就會報如下錯誤,所以先要保證服務是已啟動狀態,然後再構建!

問題2:如何上傳編譯檔案到目標伺服器

在msbuild之後新增一個execute windows batch command,執行cmd命令,cmd命令如下:

echo **********以下內容有三段,1.preSync:先Kill程序,2.同步本地與遠端,3.postSync:最後啟動服務**********
"C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -preSync:runCommand="TASKKILL /F /IM SSharing.Ubtrip.WinService.exe /T",waitAttempts=30,waitInterval=1000  -source:contentpath=%WORKSPACE%\DEV\Ubtrip\SSharing.Ubtrip.WinService\bin\Debug\ -dest:contentpath=C:\WindowsServices\UbtripJob\,computername=192.168.1.21,username=administrator,[email protected] -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服務專案

msbuild之前cmd命令:

echo **********begin restore nuget package**********
C:\mcgrady\tools\nuget.exe restore "%WORKSPACE%\DEV\Ubtrip\SSharing.Ubtrip.sln" -source https://www.nuget.org/api/v2/
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,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 Files\IIS\Microsoft Web Deploy V3\msdeploy.exe" -verb:sync -preSync:runCommand="TASKKILL /F /IM SSharing.Ubtrip.WinService.exe /T",waitAttempts=30,waitInterval=1000  -source:contentpath=%WORKSPACE%\DEV\Ubtrip\SSharing.Ubtrip.WinService\bin\Debug\ -dest:contentpath=C:\WindowsServices\UbtripJob\,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專案

msbuild之前命令:

C:\mcgrady\tools\nuget.exe restore "%WORKSPACE%\DEV\Ubtrip\SSharing.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-dev\administrator
/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使用教程之管理節點: