如何將Rails專案部署到Render上🧣

20 阅读6分钟

如何將Rails專案部署到Render上🧣

medium.com/@cagebooker…

這邊示範時專案版本為 Ruby 3.0.0 Rails 6.1.7.3

首先建立 rails 專案並使用 postgresql
railsnewdemodatabase=postgresql開啟專案建立資料表rails new demo --database=postgresql 開啟專案建立資料表 rails db:create
在 local 端打開 server $ rails s
確認沒問題後我們就繼續往下

接下來配合著 render 提供的官方 doc 來部署 rails 專案吧,打開後往下找到

Create Your First Page

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

首先要調整專案中的首頁,原本預設的 rails 首頁在部署時是不接受的,所以手動調整,就跟著指示來做,先新增一個 controller $ rails g controller Render index

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

接著設定路徑,打開 routes.rb,加入root 'render#index'end
最後,建立對應的view檔案 app/views/render/index.html.erb ,內容可以自己修改,不一定要照著 doc 上面提供的。

Doc 再往下拉會看到 Add Third-Party CSS 這個標題,因為跟部署無關可以先跳過。

Doc 往下,Update Your APP For Render

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

因為我們建立專案時就已經使用了 postgresql了,所以一二兩點不用動作。

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

在第三點中,這些圖片深色的部份是需要注意的,這次範例我只改動了以下

username: mysite

password:

我將註解打開,並設定要的名稱跟密碼,下方的 host 跟 port 就留著預設的資料不改變,註解也沒打開,這樣就可以了。

Doc 往下 Go Production-Ready

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

第一點不用動作。直接看到第二點,這邊要去修改 config/puma.rb ,框起來的這項,我們要將註解打開並將後面參數改成 4。

Press enter or click to view image in full size

再來,第三點要修改 config/enviroments/production.rb 的內容,找到對應的資料,然後加上 || ENV[‘RENDER’].present?

Create a Build Script

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

這邊要依照指示在 bin 下建立 render-build.sh 檔案,然後將範例代碼全部貼上即可。第二個要不要 check 不一定要執行,看你方便。

Deploy to Render

Render 的部署這邊提供兩種方式
1. Use render.yaml to Deploy , 利用 render.yaml 來部署
2. Deploy Manually,手動部署
我們這邊用手動部署的方式比較簡單,所以看到 Deploy Manually 這裡

Press enter or click to view image in full size

转存失败,建议直接上传图片文件

第一步,我們頁面右上角 dashboard 右鍵開新分頁,在 dashboard 的中建立一個 postgreSQL 的資料庫。只要填寫好名字就按 create 就完成了

Press enter or click to view image in full size

Dash board的頁面

Press enter or click to view image in full size

只要填寫好名字就按 create database 就可以了。

Get 顧凱翔’s stories in your inbox

Join Medium for free to get updates from this writer.

Subscribe

第二步,新增 web service,一樣到 dashboard 中,點擊新增 Web Service

Press enter or click to view image in full size

Press enter or click to view image in full size

Web Service 的頁面

點擊新增 web service後,可以看到如上頁面,這裡是需要連結 github的,如果你是第一次使用 render 畫面會顯示按鈕要你連結到 github,就照做就可以了。
這邊要把最一開始建立的 demo 專案 push 到 github 上,完成後再回到 web service 這個頁面。就可以看到 demo 的專案了,然後按下旁邊的 connect

Press enter or click to view image in full size

填寫名稱

點擊 connect 後,要填寫一些資料,依照上圖要填寫名稱

Press enter or click to view image in full size转存失败,建议直接上传图片文件

Press enter or click to view image in full size

第三步的環境設定

接著往下捲動,配合Doc文件(上圖左)的第三點,將Build Command 和 Start Command 的值修改,依照指示複製貼上過去。
Build Command → ./bin/render-build.sh
Start Command → bundle exec puma -C config/puma.rb

完成後,繼續往下,點擊 Advanced

Press enter or click to view image in full size

頁面會產生一個表格,點 ADD Enviroment Variable 增加兩個環境設定,對應到Doc 的第四點,這邊要加上兩個 KEY
DATABASE_URL
RAILS_MASTER_KEY

Press enter or click to view image in full size

Press enter or click to view image in full size

第一個 要填寫的 value 是 intermal database URL ,我們要去剛剛建立的 postgresql中找,一樣開個新分頁回到 dashboard找到剛剛建立的 postgresql

Press enter or click to view image in full size

點進來後,向下捲動就能看到 Internal Database URL了,直接點擊複製,就可以回到前面的頁面貼上

Press enter or click to view image in full size

Press enter or click to view image in full size

第二個 RAILS_MASTER_KEY,這個要到專案中 config 下找 master.key 這個檔案,然後把裡面的密碼填入剛剛的欄位中。

Press enter or click to view image in full size

Press enter or click to view image in full size

我的是有擋住密碼的

這樣就完成 web service 創建的環境設置了,拉到最下面按 Create Web Service,系統就開始動作,這時候畫面會跳轉,然後開始部署過程,會花一點時間要稍等一下。

然後這時候可能會得到 Failed 的訊息,因為專案跟伺服器有設定需要調整,不過系統的回覆會直接跟你講要怎麼做,如下底線。

Press enter or click to view image in full size

錯誤訊息中會出現 bundle lock — add-platform x86_64-linux,這時候回到 demo 專案中,執行這個指令

$ bundle lock --add-platform x86_64-linex

完成後再次 push 到 github 上

再次回到 web service 頁面,右上角有 Manual Deploy 按鈕,點 Deply latest commit,這樣就完成囉

Press enter or click to view image in full size

接著就等待,系統會再一次部署

Press enter or click to view image in full size转存失败,建议直接上传图片文件

Press enter or click to view image in full size

最後沒意外就會順利成功了,看到 live 顯示在意面上,你可以將頁面往上拉,就能看到部署完成的連結,點擊連過去就可以看到自己的網頁囉!

這次 Rails 部署到 Render 的教學結束。謝謝觀看