目錄

Deply Project to Fly Replace Heroku

Deply Project to Fly Replace Heroku

Heroku 將在 11/28, 2022 中止免費方案. /zh-tw/deply-project-to-flyio/heroku.png

替代方案

更詳細的內容請參考: https://dev.to/meshvpatel18/top-heroku-alternatives-for-free-46g1

  1. Netlify - Host serverless apps
  2. Cloudflare - Best for Static Site.
  3. Cyclic - Deploy full stack Node JS apps in seconds.
  4. Deta – The Cloud for Developers
  5. Firebase - Fast and secure web hosting
  6. Railway - Made for any language.
  7. Render - The fastest
  8. Devtron
  9. Fly - Run your full stack apps all over the world.
  10. Fathym - Develop and deploy micro frontends in a headless, open world.
  11. Back4app - Low-code backend to build modern apps
  12. Dom Cloud - Classic Web Hosting Made Easy
  13. Adaptable - The easiest way to deploy your FullStack App
  14. Microtica
  15. Coherence - The First Developer Experience Platform

原先在 Heroku 上架了 Line Bot, 使用上面的free PostgreSQLRedis. fly 剛好符合目前想要的使用情境, 所以這篇文章紀錄一下 如何使用~

fly 免費方案的RAM約250MB, 想比Heroku的 500 MB 小了很多, 但 fly 不像 Heroku 會30分鐘沒使用就讓你的 server 停機

Install Flyctl

MAC

1
brew install flyctl

其他的 OS安裝方式可以參考 : https://fly.io/docs/hands-on/install-flyctl/

Singup and Login

首先要先註冊再來是登入, 指令如下

1
2
flyctl auth signup
flyctl auth login

Launch and Deploy

Launch

可以直接到你的project下 執行 flyctl launch 此時就他會詢問一些問題, 像是你的app name , 跟 server 要放在哪個國家區域等

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
Creating app in /Users/kimi/Desktop/kimi
Scanning source code
Could not find a Dockerfile, nor detect a runtime or framework from source code. Continuing with a blank app.
? App Name (leave blank to use an auto-generated name):
Automatically selected personal organization: xxxx
? Select region:  [Use arrows to move, type to filter]
  hkg (Hong Kong, Hong Kong)
  iad (Ashburn, Virginia (US))
  lax (Los Angeles, California (US))
  lhr (London, United Kingdom)
  maa (Chennai (Madras), India)
  mad (Madrid, Spain)
  mia (Miami, Florida (US))
> nrt (Tokyo, Japan)
  ord (Chicago, Illinois (US))
  scl (Santiago, Chile)
  sea (Seattle, Washington (US))
  sin (Singapore)
  sjc (San Jose, California (US))
  syd (Sydney, Australia)
  yul (Montreal, Canada)

最後會自動產生出fly.tomlDockerfile. 這邊是我的Django

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
ARG PYTHON_VERSION=3.7

FROM python:${PYTHON_VERSION}

RUN apt-get update && apt-get install -y \
    python3-pip \
    python3-venv \
    python3-dev \
    python3-setuptools \
    python3-wheel

RUN mkdir -p /app
WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

RUN python manage.py makemigrations
RUN python manage.py collectstatic --noinput


EXPOSE 8000

# replace APP_NAME with module name
CMD ["gunicorn", "--bind", ":8000", "--workers", "2", "gogopowerkimibot.wsgi"]

Deploy

flyctl deploy , 就可以在 fly的 dashboard 看到程式碼正在部署了~ 如果有程式異動, 也可以直接打此指令

Create Postgre

跟上一步建立flyctl launch 差不多. 執行此指令flyctl postgres create, 最後他產生出一組連線資訊跟密碼. 記得要把它存起來, 要不然之後沒地方可以查到這些資訊

Create Redis

執行此指令flyctl redis create 記得要把它存起來, 要不然之後沒地方可以查到這些資訊

https://fly.io/docs/reference/redis/

其他指令:

1
2
3
4
flyctl redis list
flyctl redis status gogopowerkimi-redis
fly redis connect
fly redis delete {ID}

Secrets

PostgreRedis 都建立好時, 你可以把連線資訊或者你的環境變數寫入 .env 檔. 藉由 flyctl secrets import 去匯入 ex:

1
flyctl secrets import < .env.flyio

Fly Status

flyctl ssh console : 可以 ssh 進去 fly status --all : 看目前此 project 狀態

最後可以在你的 Dashborad上看到 /zh-tw/deply-project-to-flyio/fly.png

Github Action

https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/

1. 建立 Fly API Token

flyctl auth token 產生出一組 token, 並把它寫到你的 repositiry > settings > Secrets > Actions 取名為 FLY_API_TOKEN 如下圖 /zh-tw/deply-project-to-flyio/github_secret.png

2. 建立 yml 檔

最後可以使用 Github Action, 當push時, 自動 deploy 在你的目錄 .github/workflows 資料夾下建立一個yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
# https://fly.io/docs/app-guides/continuous-deployment-with-github-actions/
name: Fly Deploy
on:   
  workflow_dispatch:
  push:
    branches:
      - flyio

env:
  FLY_API_TOKEN: ${{ secrets.FLY_API_TOKEN }}
jobs:
  deploy:
      name: Deploy app
      runs-on: ubuntu-latest
      steps:
        - uses: actions/checkout@v2
        - uses: superfly/flyctl-actions/setup-flyctl@master
        - run: flyctl deploy

之後只要push到 flyio branch 就會自動跑 action了 /zh-tw/deply-project-to-flyio/github_action.png

Reference