Skip to content

Instantly share code, notes, and snippets.

@Maikon
Forked from staknine/live_saas_kit_deploy.md
Created June 9, 2022 14:37
Show Gist options
  • Save Maikon/8e62db9c85676abd5263c11c67741bca to your computer and use it in GitHub Desktop.
Save Maikon/8e62db9c85676abd5263c11c67741bca to your computer and use it in GitHub Desktop.
Steps to deploy LiveSaaS Kit on Fly with Phoenix 1.6.9

Here are the steps to update Phoenix Live SaaS Kit for Fly and Phoenix 1.6.9:

Initial Deployment

  1. git init, git add ., git commit -m "init repo"
  2. Remove generated docker & deploy script files
  • build.sh
  • docker-entrypoint.sh
  • .dockerignore
  • Dockerfile
  • docker-compose.yml
  1. Update .tool-versions:
elixir 1.13.4-otp-24 
erlang 24.3.4
  1. Open mix.exs and update phoenix to 1.6.9 & phoenix_html to 3.2.0
  2. Run mix deps.get
  3. Run cd assets then rm yarn.lock. Will use npm to match Dockerfile.
  4. Run npm install
  5. Update runtime.exs database config:
  maybe_ipv6 = if System.get_env("ECTO_IPV6"), do: [:inet6], else: []

  config :demo, Demo.Repo,
    # ssl: true,
    url: database_url,
    pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
    socket_options: maybe_ipv6

See: https://community.fly.io/t/how-to-run-ecto-create-for-an-elixir-app/5001/2

  1. Update runtime.ex endpoint config.
  host = System.get_env("PHX_HOST") || "example.com"
  port = String.to_integer(System.get_env("PORT") || "4000")

  config :demo, DemoWeb.Endpoint,
    url: [host: host, port: 443, scheme: "https"],
    http: [
      # Enable IPv6 and bind on all interfaces.
      # Set it to  {0, 0, 0, 0, 0, 0, 0, 1} for local network only access.
      # See the documentation on https://hexdocs.pm/plug_cowboy/Plug.Cowboy.html
      # for details about using IPv6 vs IPv4 and loopback vs public addresses.
      ip: {0, 0, 0, 0, 0, 0, 0, 0},
      port: port
    ],
    secret_key_base: secret_key_base

Source: [error] Could not check origin for Phoenix.Socket transport. Origin of the request: https://live-saas-kit-demo.fly.dev. This happens when you are attempting a socket connection to a different host than the one configured in your config/ files.

  1. Run fly launch, but do NOT deploy app because we need to update the Dockerfile
  2. Add nodejs && npm and add RUN cd assets && npm install to Dockerfile

Steps 1 & 3 from:

https://community.fly.io/t/elixir-getting-started-guide/1356/59

NOTE: Step 2 is already complete in the generated Dockerfile.

  1. Run fly deploy --remote-only to finish deployment

Admin Config

Additional setup steps from Fullstack Phoenix

  1. Add Guardian secrets using mix guardian.gen.secret
$ fly secrets set GUARDIAN_SECRET_KEY=secret GUARDIAN_SECRET_KEY_ADMINS=secret
  1. Create an admin using instructions from Fullstack Phoenix Admin Area.

GitHub Actions

  1. Delete .github/wokflows/ci.yml
  2. Create new repo on GitHub
  3. Generate Fly API token with fly auth token
  4. Store value as FLY_API_TOKEN in GitHub repository secrets
  5. Add .github/workflows/main.yml from hello_fly project.
  6. Merge changes and push to GitHub

Email Config

  1. Sign up for a Mailgun account
  2. Add hackney package to mix.exs and configure the Mailgun adapter (can uncomment runtime.config file).
    config :demo, Demo.Mailer,
       adapter: Swoosh.Adapters.Mailgun,
       api_key: System.get_env("MAILGUN_API_KEY"),
       domain: System.get_env("MAILGUN_DOMAIN")
       
     config :swoosh, :api_client, Swoosh.ApiClient.Hackney
  1. Set API Key and Domain as secrets in Fly
    fly secrets set MAILGUN_API_KEY=my-api-key MAILGUN_DOMAIN=example.com
  1. Add authorized recipients (Mailgun sandbox mode only)
  2. Update config.exs with from_email. Also update other company info...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment