我們做網絡程式開發的,有時會想找一些免費的網絡空間放上自己的作品做展示或測試,大部份人都只得找一些Web Hosting去解決。但實在免費的Web Hosting諸多限制,有時帳戶/檔案無故消失亦很常見。
特別是可運行ASP.NET程式的Free Hosting,更是少之有少。
但AppHarbor的出現,令以上事情出現新希望。
AppHarbor可說是早前網絡上經常談到的Cloud Computing(雲端運算)其中的PaaS模式(Platform as a Service)。
AppHarbor以很特別的方式運作:
- 不用FTP上傳檔案,是以GIT或SVN Add-on去Commit至伺服器,同樣有Revision,可隨時決定返回任何版本。
- 預設沒有MSSQL / MySQL,但可以以On Demand方式自行免費安裝各種Add-on。
- Collaboration
- 可上傳Unit Test Project,AppHarbor會自動在Server-Side先運行Unit Test,成功後才會Deploy。
雖然不能與Free Hosting相提並論,因為大家所擁有的能力都很不同,但若和我一樣,只是想向外界展示一下ASP.NET程式的話,AppHarbor是一個很好的選擇。
最令人卻步的,或者是需要使用GIT,若然有SVN的經驗的話,可以說是沒有難度,沒有經驗的話,若單純做一個"上傳"動作都是很簡單的事,接下來就由零開始示範一次。
1. 下載GIT Client - TortoiseGit
因為我們需要配合AppHarbor,所以本機都要建立一個GIT的Repository做Commit (至本機)和Push (至AppHarbor)的動作。
事前需要花一點功夫去建立GIT的基地。
- 先下載GIT的核心檔案 - Git for Windows,可以選擇下載Portable版本的PortableGit。
- 然後下載簡單易用的GIT Client - TortoiseGit。
把PortableGit Unzip至C:\PortableGit。(路徑不能有空格或中文)
再安裝TortoiseGit。
在桌面空白的地方Right Click > TortoiseGit > Settings,先在左邊選單選GIT,User Info處填入Name和Email,因為GIT的Commit動作是必須要有以上資料。
之後去確認一下Git.exe的位置是否如"C:\PortableGit\bin"。
完成以上的話,GIT部份已大功告成。


2. 新建ASP.NET MVC3 Project
本範例以Visual Studio 2010的MVC3 Project Template示範,開啟Visual Studio 2010後,新增專案,選"ASP.NET MVC 3 Web Application",大家應該都很熟悉了。
Project名稱我修改做"AppHarborTest"。
Project Template選"Internet Application",不用選取"create a unit test project"。
如下圖 :

3. 建立本機GIT Repository
以Visual Studio預設路徑為例,
前往"C:\Users\[USER_NAME]\Documents\Visual Studio 2010\Projects\AppHarborTest\AppHarborTest",
進入Project資料夾,然後在空白地方,Right Click > GIT create new repository後,
TortoiseGit會問你是否Make it Bare,直接按OK先跳過,
再出現一個告訴你資料夾不是空的對話,按Proceed就可以。
因為GIT跟SVN一樣,要建立的Repository資料夾,預設是空的。
最後在AppHarborTest資料夾應該會見到一個隱藏資料夾名為.git。



暫時不用修改任何檔案,直接按Right Click > Git Commit -> "Master"。
這個動作用途是把新增的檔案Commit至GIT資料庫並視為為HEAD版本(最新的),用以之後的Push指令(上傳至AppHarbor)。
等待完成後,先按Close,那本機要做的就差不多完成了。



4. AppHarbor 申請帳號&設定
終於到了AppHarbor部份,前往AppHarbor按Sign Up,申請Account過程很簡單快捷,完成後應該會看到這個畫面。
在欄位上填上"AppHarborTest",地區選"United States"或"Europe"都沒所謂,估計這個是Server的地理位置吧,最後按Create New。


5. 加入Add-on
我們暫不需要MSSQL,但想嘗試的話,我們可以按左邊的Menu > Add-ons , Install Free版本就可以了。

若你需要使用SQL Server Management Studio連接,可以按Configuration variables連結取得Connection String。


AppHarbor最基本的設定已經完成。
6. GIT Push至AppHarbor
回到本機的AppHarborTest Project資料夾,我們現在要進行一個Push的"上傳"的動作,這類似SVN的Commit。
但GIT架構是自己是Repository,Commit動作是在本機進行(即本文第3步),要送至另一個Bone Repository (你可以暫時看待為中央版本庫),我們需要用Push。

在Destination > Remote處,還沒有任何選項,所以我們要先按Manage加入AppHarbor,
Repository URL 可如下圖的地方找到,按Add New/Save後,再按OK,會自動選擇AppHarborTest,最後再按OK,便會進行Push的動作。
詢問Password時,打入你申請帳號的密碼便可以。




7. Build Project
回到AppHarbor頁面,系統應該會找到剛Push的Project,確認Status是Succeeded。

最後按右上方的"Go to your application",大功告成!!

後記:
大家可以會發現網頁可以隨意瀏覽,但不能Register Account,原因是這個Project Template用的是SqlMembershipProvider,需要attach aspnetdb.mdf至SQL Server,但AppHarbor暫時只接受由Addon所Create的Database,所以才會出現以上情況。
其實一直試用AppHarbor,發現還有幾個要注意的地方:
- 一個Application只能配搭一個MSSQL Database。
- File Write Permission只在APP_DATA資料夾有效。
- 暫時不支援Entity Framework 4.1或以上。
- 暫時不支援CodeFirst的Deployment。
本文只介紹Deployment的動作,AppHarbor還有很多值得去探索的地方,但無論如何AppHarbor的出現的確是ASP.NET開發人員一大喜訊。
沒有留言:
發佈留言