2016年1月7日 星期四

[Ruby On Rails] 如何在Heroku 上佈署應用程式

在 Heroku 上開發 步驟 



1. Introduction


申請 Heroku 免費帳號,由於目前的開發環境是Windows,所以按照 Heroku 官網上面的 Getting Started with Ruby on Heroku (Microsoft Windows) 步驟安裝必要的程式。

JDK : JDK 我是安裝 Java SE Development Kit 8u66 (SE 65的程式無法執行,不明原因)。
因為所使用的主機windiws版本是64位元,所以下載x64的JDK檔案

Windows x64186.65 MB  jdk-8u66-windows-x64.exe

JRuby : 另外,因為JRuby的安裝程式無法偵測到Java Runtime Environment(jre),所以先安裝了Java (Version 8 Update 66),安裝完成後便可以執行JRuby的安裝程序,跑出如下圖的畫面。



Bundler : 在cmd 的小黑窗裡面,直接輸入 jruby -S gem install bundler 這個指令。
如果安裝完成,會顯示如下的畫面。
(像我這種用慣windows的一般安裝介面流程的人,應該會覺得只要在command window輸入一段指令,檔案就安裝完了,實在頗為神奇)



安裝步驟的部分就不一一詳細列出。

2. Set up


安裝 Heroku 在 local的 Heroku Toolbelt for Windows


安裝了之後,它可以幫助我們連結到 git 還有 Heroku Command Line Interface (CLI)。關鍵的是toolbelt是Heroku的local command,可以在local端上面執行自己打造的應用程式(app)。

3.Prepare the app 

在cmd視窗裡面輸入以下的文字,按下滑鼠右鍵,選擇複製,並在cmd視窗裡按下滑鼠右鍵,選擇貼上,便能夠直接貼上複製的文字到cmd視窗裡面

> git clone https://github.com/heroku/jruby-getting-started.git
> cd jruby-getting-started

這時便複製了Github裡 https://github.com/heroku/jruby-getting-started.git 這項條目的檔案到終端這邊。
接著,使用"更換目錄指令(cd,change directory的縮寫)"進入jruby-getting-started這個資料夾


4.Deploy the app



接下來便要練習佈署一個應用程式到Heroku上,並準備讓Heroku接受我們的程式碼。

> heroku create
Creating polar-inlet-4930... done, stack is cedar-14
http://polar-inlet-4930.herokuapp.com/ | https://git.heroku.com/polar-inlet-4930.git
Git remote heroku added

當創了一個應用程式,我們同時也創了一個 Git remote (叫作 heroku) 聯結到自己local端的資料庫(repository)。目前,Heroku會幫我們的應用程式產生一個隨機的名字。

接著,利用push指令將程式碼進行佈署的動作。執行結果的範例如下:

> git push heroku master
Counting objects: 176, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (113/113), done.
Writing objects: 100% (176/176), 31.81 KiB | 0 bytes/s, done.
Total 176 (delta 51), reused 170 (delta 48)
remote: Compressing source files... done.
remote: Building source:
remote:
remote: -----> Ruby app detected
remote: -----> Compiling Ruby/Rails
remote: -----> Using Ruby version: ruby-2.2.2-jruby-9.0.0.0
remote: -----> Installing JVM: openjdk1.8-latest
remote: Picked up JAVA_TOOL_OPTIONS: -Xmx768m -Djava.rmi.server.useCodebaseOnly=true
remote: -----> Installing dependencies using 1.7.12
remote:        Running: bundle install --without development:test --path vendor/bundle --binstubs vendor/bundle/bin -j4 --deployment
remote:        Picked up JAVA_TOOL_OPTIONS: -Xmx768m -Djava.rmi.server.useCodebaseOnly=true
remote:        Fetching gem metadata from https://rubygems.org/...........
remote:        Installing json 1.8.1
...
remote:        Installing sass-rails 4.0.5
remote:        Your bundle is complete!
remote:        Gems in the groups development and test were not installed.
remote:        It was installed into ./vendor/bundle
remote:        Bundle completed (162.29s)
remote:        Cleaning up the bundler cache.
remote: -----> Preparing app for Rails asset pipeline
remote:        Running: rake assets:precompile
...
remote:        Asset precompilation completed (107.72s)
remote:        Cleaning assets
remote:        Running: rake assets:clean
remote:        Picked up JAVA_TOOL_OPTIONS: -Xmx768m -Djava.rmi.server.useCodebaseOnly=true
remote:
remote: -----> Discovering process types
remote:        Procfile declares types -> web
remote:        Default types for Ruby  -> console, rake, worker
remote:
remote: -----> Compressing... done, 97.9MB
remote: -----> Launching... done, v6
remote:        https://polar-inlet-4930.herokuapp.com/ deployed to Heroku
remote:
remote: Verifying deploy... done.
To https://git.heroku.com/polar-inlet-4930.git
 * [new branch]      master -> master

最後會顯示成功佈署到主程式(master)。

OK,現在應用程式已經佈署上去了,接著輸入下面的指令確保程式正確的在local運行

> heroku ps:scale web=1

最後,輸入下面的指令,或是輸入正確的網址,就能夠在瀏覽器上面執行開發者自行打造的應用程式

> heroku open

執行結果
OK,很順利的在Heroku上面佈署了一個Hello World等級的網站。

如果去Heroku自己的個人帳戶中,找一下database裡面,剛剛所創造的網頁的資料,會發現,原來真正存放的資料庫是Amazon的AWS 雲端平台上。 Heroku則是一個中介商,幫助大家能夠更快速的佈署所開發的網頁到雲端主機上。

5. View Logs

Heroku 把過去的更動紀錄等,都按照時間編排。可以使用 heroku logs --tail 指令觀看過去的任何紀錄。




沒有留言:

張貼留言

/* 載入prettify的autoloader */ /* 載入JQuery */