2011年6月2日 星期四

TFS 的 Web 自動部署

繼上次的 TFS 建置:Coded UI 自動測試 問題後,好久好久我都沒再碰 TFS 的 Build 設定了。

這次,目的在建立 Web 的自動部署。

環境

這次的環境較為複雜。有開發機,TFS Server,Build Server,Test WebServer.

希望可以達成的需求:在開發機上要求一個 Build後,TFS Server 向 Build Server 要求啟動一個 Build。Build Server 完成 Compile 後,將 Web Application 自動部署到 Test WebServer。

過程還有些漫長,最大的原因還是「太久遠的記憶」是不可靠的。下面是我試出來的結果。

Build Definition

在 MSBuild Arguments 我設定如下

/p:DeployOnBuild=True
/p:DeployTarget=MsDeployPublish
/p:MSDeployPublishMethod=RemoteAgent
/p:CreatePackageOnPublish=True
/p:DeployIisAppPath="Default Web Site/YourApp"
/p:MsDeployServiceUrl=http://172.199.199.199/MsDeployAgentService
/p:username=administrator
/p:password=adminpassword

image

 

Build Server

Build Server 上,必須安裝了 Visual Studio 2010。我試的結果,不裝,就不會自動部署。我不知道到底我錯過了什麼環結,雖然設定了上述的 MSBuild Arguments,但就是不跑 Deploy。

安裝時,我選了 c# 與Visual Web Developer。理論上只需安裝 Visual Web Developer即可,但我不想再浪費時間試了。

image

另外,在 Visual Studio 2010 Web Application 的專案屬性上,也有一些玄機。如果有勾選「Include IIS settings as configured in IIS」或「Include application pool settings used by this Web project」的話,在 Build Server 就要安裝 IIS 並且有相同的設定。因為當 Build Server 要打包 package 時,需要讀IIS的設定。讀不到設定就會出現下面的錯誤訊息。

C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v10.0\Web\Microsoft.Web.Publishing.targets (1657): "MapUriToIisWebServer" 工作發生未預期的失敗。 System.Runtime.InteropServices.COMException (0x80005000): 未知的錯誤 (0x80005000)  

image

此時的我,顧不得完美了,先不要勾選這兩個選項了。

Test Web Server

1 在待部署的 Web Server 上,必須安裝 Web Deployment Tool。安裝時,必須注意要選「完整」安裝。

2 安裝完畢後,需要在服務中啟動 Web Development Agent Service

image

3  Build 執行後出現下面的錯誤

Web deployment task failed.((2011/6/2 下午 05:14:42) An error occurred when the request was processed on the remote computer.)  (2011/6/2 下午 05:14:42) An error occurred when the request was processed on the remote computer. The application pool that you are trying to use has the 'managedRuntimeVersion' property set to 'v2.0'. This application requires 'v4.0'.

由於我沒有選擇要打包 IIS 的設定,部署後的應用程式是下 Default Web Site 下。Default Web Site 預設是.net framework v2.0, MSDeploy 在建立 application 時會預設同上一層的 application pool。

我的解法是直接將上一層的 Default Web Site 改用 v4.0 的 application pool。

結論

上述設定,一定還有調整優化的空間。之後有空再寫 blog好了。

雖然很麻煩,但想到之後不必再手動部署測試環境時,心裡也就舒坦了許多。

參考

沒有留言:

Share with Facebook