Try   HackMD

如何把專案部署到Railway上 - 筆記

tags: deployment Node.js database MySQL

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
本站筆記已同步更新到我的個人網站囉! 歡迎參觀與閱讀,體驗不同的視覺感受!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
2023/08 更新:Railway自2023年8月1日起取消免費方案,最低收費Hobby Plan每個月5美元,各方案詳細收費標準可參考官網

專案簡介

用Node.js完成的互動式社交網站Simple Twitter,搭配關聯式資料庫MySQL(v8.0),採前後端分離開發,這次要部署的是後端repo。

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

專案前台登入操作示範

Web Service部署

  1. 先創一個Railway帳號
  2. 完成後到Dashboard中按 + New Project
  3. 選擇要建立的project,這邊先介紹部署專案repo,所以先選 Deploy from GitHub repo > Configure GitHub App,這邊會詢問GitHub的授權和要部署的repo,依個人需求選擇即可
  4. GitHub授權完成後在 New Project 的區塊可以找到要部署的repo (如果沒看到可以重複3的步驟,就會看到剛剛選的repo),點進去可以看到 Deploy NowAdd variables (設置環境變數),這裡可以先把 .env 檔案中的環境變數填進去(這個步驟很重要,error常常是環境變數沒設定好發生的
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    )
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  5. 環境變數設定完,按Deploy Now就會開始部署
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  6. 部署完成點進 View Logs 看看是否有錯誤,如下圖Deploy Logs沒有error就成功啦!
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  7. 如果有錯誤可以檢查:
  • 環境變數是否正確:Variables中的環境變數和.env檔案中的環境變數是否一致
  • Settings 中的設置,例如Start command是不是和啟動專案的command一樣,特別要注意檔案名稱,例如是node app.js 還是 node index.js 等等
  • Settings 中也可以設置Automatic Deployments,如果GitHub上的檔案有更新會自動重新部署;另外也可以自動產生Domain或設定客製化Domain
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

MySQL 資料庫部署

Railway提供了4種資料庫的部署,PostgreSQL、Redis、MongoDB、MySQL,這個專案使用的是MySQL:

  • 點進Dashboard,找到剛剛部署好的專案點進去(注意是點進剛剛的專案再新增資料庫,不是直接在工作區新增一個資料庫,可以想成專案repo和資料庫是同捆包),按右上角的 +New > Database > Add MySQL
  • 很快的Railway幫我們新增了一個MySQL資料庫,點進資料庫找到Connect,有資料庫的連線資訊
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
  • 打開MySQL WorkBench,新增一個連線,把這些資訊填進去,然後test connection,無誤的話可以順利進到資料庫工作區
  • 這時候突然想到,還沒有把專案跟這個雲端資料庫連線,所以打開VScode來修改一下:
# config.json "production": { "use_env_variable": "DB_URL" },
# .env DB_URL=your_db_url
  • 修改完再push到GitHub,注意Railway是否有自動重新部署,若沒有記得手動更新
  • 資料庫現在是空的,我們要建立Schema並且把種子資料寫進去,這裡希望也可以用sequelize的指令操作,因此先下載railway cli,詳細安裝方式請看railway的文件,注意如果是以npm安裝要先升級Node.js的版本到16,不然無法安裝!!
  • 以下簡單寫一下CLI操作方式,詳細都請參閱官方文件
    1. 安裝完Railway CLI,終端機輸入 railway loginrailway login --browserless依照指示登入Railway
    2. 和剛剛建立的專案連結,輸入後可以選擇專案和環境(預設是production):
# Link to a project
railway link

          3. Railway提供了可以在本地使用和Railway專案相同環境變數進行開發的指令:

# Run <cmd> locally with the same environment variables as your Railway project railway run <cmd> # Run your Node.js project with your remote environment variables railway run npm start
  • 現在要來進行資料的migration,在終端機使用熟悉的sequelize指令(先確認已切換到production模式):
    railway run npx sequelize db:migrate
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    出現了錯誤,呼叫Google大神,找到了這篇文章
    在MySQL Workbench或MySQL CLI執行以下SQL query:
    ALTER USER 'root'@'%' IDENTIFIED WITH mysql_native_password BY 'password'

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
注意!這裡的rootpassword要改成Railway雲端資料庫的user和password,才有權限操作資料庫,不然會出現另一個錯誤ERROR: Access denied for user '<wrong_username>'@'<your_ip>' (using password: YES) (密碼或使用者名稱不對都無法操作資料庫,可以參考這篇文)
Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →

  • 再執行一次資料migration,這次就成功了;同理寫入種子資料:railway run npx sequelize db:seed:all
  • 回到Railway確認資料是否正確寫入:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    點進table看看:
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    都沒問題囉,到這裡就完成了!
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →
    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

其他功能

這裡補充幾個Railway很方便的功能:

  • 在專案中可以直接用reference的方式引入環境變數(包含資料庫變數),連複製貼上都不用,真貼心

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 設置shared variables:如果有多個專案要共用的環境變數,可以設置成shared variables,在其他專案也可以用reference的方式引入,相當方便!

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

  • 可以邀請協作者編輯專案,但協作者無法看到環境變數,如果要分享讀取環境變數的權限可以使用token,協作者用CLI的指令讀取,詳細參考文件 (此功能請小心使用):
    RAILWAY_TOKEN=XXXX railway run

    Image Not Showing Possible Reasons
    • The image file may be corrupted
    • The server hosting the image is unavailable
    • The image path is incorrect
    • The image format is not supported
    Learn More →

小結

沒有了Heroku這個方便免費的服務只好尋找替代品,本來試著部署在Render,雖然Render操作起來也非常之簡單易懂,無奈目前免費服務不提供MySQL資料庫部署(PostgreSQL是可以的,但90天到期後自動刪除資料庫),所以選擇了Railway,雖然功能或許沒有GCP或AWS強大,但對於個人小專案已經足夠,目前免費方案提供每月500小時或5美元的額度。(2023/08 更新:Railway自2023年8月1日起取消免費方案,最低收費Hobby Plan每個月5美元,各方案詳細收費標準可參考官網。)
Railway的服務相對新,部署的時候遇到問題參考資料也不是很多,只能靠官方文件和自己摸索,希望這篇文能幫助到需要使用Railway服務的人!

Image Not Showing Possible Reasons
  • The image file may be corrupted
  • The server hosting the image is unavailable
  • The image path is incorrect
  • The image format is not supported
Learn More →
本站內容僅為個人學習記錄,如有錯誤歡迎留言告知、交流討論!