使用Jenkins自動發布Windows服務項目

來源:互聯網
上載者:User

標籤:click   mini   user   inline   bsp   iis   blog   複製   job   

不同於發布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]123

這裡使用的是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]123 -enableRule:DoNotDeleteRule -postSync:runCommand="net start UbtripWs_Business",waitAttempts=20

使用msdeploy的sync操作,通過runCommand在目標伺服器上執行cmd命令。preSync指在複製檔案之前啟動並執行命令,postSync是複製檔案之後啟動並執行命令。

注意:

msdeploy的contentpath預設是先刪除目標伺服器的檔案再添加的,但是我們並不希望刪除原來的檔案,所以需要添加參數 -enableRule:DoNotDeleteRule。

自動排除Web.config和App.config

1,windows服務項目

通過給msdeploy添加參數-skip,命令如下:

-skip:objectName=filePath,absolutePath=Web\.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命令:

1 echo **********begin restore nuget package**********2 C:\mcgrady\tools\nuget.exe restore "%WORKSPACE%\DEV\Ubtrip\SSharing.Ubtrip.sln" -source https://www.nuget.org/api/v2/3 echo **********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參數:

 

msbuild之後cmd命令:

 

2,Web項目

msbuild之前命令:

 

msbuild參數:

 

參考資料

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

使用Jenkins自動發布Windows服務項目

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.