Elixir 1.9 releases with edeliver

You can still use edeliver for deployment, but in combination with distillery. Instead of “mix release.init”, you can write “mix distillery.init” and then use edeliver.

I don’t know if it is of use for you, but here is my complete step-by-step list for deployment of an elixir-1.9-project with edeliver (on a server that has MariaDB). Not everything is optimal (for example, I do not use ssl), but the deployment worked:

Setting up MariaDB on server

→ some useful commands to create databases, grant privileges, etc.:


  mysql -u root -p 
  mysql -u phoenix -p 

  SHOW DATABASES;
  select User, Host from mysql.user;
  SHOW GRANTS FOR 'phoenix'@'localhost';

  CREATE DATABASE project_name_prod;
  GRANT ALL PRIVILEGES ON project_name_prod.* TO 'phoenix'@'localhost';
  FLUSH PRIVILEGES;

  DROP DATABASE project_name_prod;
  DROP USER 'phoenix'@'localhost';
  REVOKE ALL PRIVILEGES ON project_name_prod.* FROM 'phoenix'@'localhost';

  SHOW TABLES FROM project_name_prod;
  SELECT * FROM project_name_prod.users;

setup ssh public key

Set up SSH public key authentication to connect to a remote system

bitbucket

project is on bitbucket.

prod.secret.exs

→ copy prod.secret.exs to server (/home/my_user_name/a_folder/project_name/app_config)

		use Mix.Config

		config :project_name, ProjectName.Repo,
			# ssl: true,
			url: "ecto://phoenix:password@localhost/project_name_prod",
			pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10")

		config :project_name, ProjectNameWeb.Endpoint,
			secret_key_base: "this_is_a_very_long_string"

		config :sendgrid,
			api_key: "this_is_a_very_long_string"

Edeliver

→ in mix.exs:

defp deps do
  [
    ...
  {:edeliver, ">= 1.6.0"},
  {:distillery, "~> 2.0", warn_missing: false}
  ]
end

→ get dependencies:

mix deps.get
mix compile

→ config/prod.exs:

	config :project_name, ProjectNameWeb.Endpoint,
		http: [port: 4005],
		url: [host: "my_project.com", port: 4005, scheme: "https"],
		cache_static_manifest: "priv/static/cache_manifest.json",
		server: true,
		code_reloader: false,
		root: ".",
		check_origin: false,
		version: Application.spec(:project_name, :vsn)

→ distillery:

mix distillery.init

→ create the file .deliver/config (without file ending!):

APP="project_name"

BUILD_HOST="ip address"
BUILD_USER="my_user_name"
BUILD_AT="/home/my_user_name/a_folder/project_name/app_build"

PRODUCTION_HOSTS="ip address"
PRODUCTION_USER="my_user_name"
DELIVER_TO="/home/my_user_name/a_folder/project_name/app_release"

pre_erlang_get_and_update_deps() {
  local _prod_secret_path="/home/my_user_name/a_folder/project_name/app_config/prod.secret.exs"
  if [ "$TARGET_MIX_ENV" = "prod" ]; then
    __sync_remote "
      ln -sfn '$_prod_secret_path' '$BUILD_AT/config/prod.secret.exs'
    "
  fi
}

pre_erlang_clean_compile() {
  status "Installing NPM dependencies"
  __sync_remote "
    [ -f ~/.profile ] && source ~/.profile
    set -e

    cd '$BUILD_AT/assets'
    npm install $SILENCE
  "

  status "Building static files"
    __sync_remote "
      [ -f ~/.profile ] && source ~/.profile
      set -e

      cd '$BUILD_AT'
      mkdir -p priv/static
      cd '$BUILD_AT/assets'
      npm run deploy $SILENCE
   "

  status "Running phx.digest"
  __sync_remote "
    [ -f ~/.profile ] && source ~/.profile
    set -e

    cd '$BUILD_AT'
    APP='$APP' MIX_ENV='$TARGET_MIX_ENV' $MIX_CMD phx.digest $SILENCE
  "
}

→ gitignore:

echo “.deliver/releases/” >> .gitignore

→ git:

  git add .
  git commit -m "Setting up edeliver"
  git push origin master

→ now deploy:

  mix edeliver build release --debug
  mix edeliver deploy release to production
  mix edeliver start production
  mix edeliver migrate production

  mix edeliver stop production

→ seeds:
https://www.reddit.com/r/elixir/comments/5j6k2y/how_to_populate_a_database_in_phoenixelixir_on_a/

connect to server with ssh, then run these lines:

cd ~/a_folder/project_name/app_release/project_name/bin
./project_name remote_console
:code.priv_dir(:project_name) |> Path.join("repo/seeds.exs") |> Code.require_file()

→ hot upgrades
→ change version in mix.exs, push with git, and then:

 mix edeliver build upgrade --with="0.1.0"
 mix edeliver deploy upgrade to production --version="0.1.1"

https://hexdocs.pm/distillery/guides/phoenix_walkthrough.html

3 Likes