AlexMaslakoff.icu

Things related to software development, security, marketing, e-commerce.

Deploying Elixir/Phoenix applications

published: 22 Feb 2019

This is twisted. Once I made it work, I haven’t touched it since, out of fear that something would break. Look at the github page of edeliver - https://github.com/edeliver/edeliver. It looks anything but simple and straighforward. One would have to go university to make sense of it.

How to

First, we need these 2 libraries in the mix.exs file:

{:edeliver, "~> 1.6"},
{:distillery, "~> 2.0"},

Then we need a file .deliver/config:

#!/usr/bin/env bash

APP="<your_app_name_from_mix_file>"
HOST="<your_server_ip>"
DEPLOY_USER="<your_user_name>"

BUILD_HOST="$HOST"
BUILD_USER="$DEPLOY_USER"
BUILD_AT="/tmp/edeliver/$APP/builds"

STAGING_HOSTS="$HOST"
STAGING_USER="$DEPLOY_USER"
PRODUCTION_HOSTS="$HOST"
PRODUCTION_USER="$DEPLOY_USER"

DELIVER_TO="/home/$DEPLOY_USER/apps/$APP"
RELEASE_DIR="/home/$DEPLOY_USER/apps/$APP"

USING_DISTILLERY=true

pre_erlang_get_and_update_deps() {
  local _prod_secret_path="config/prod.secret.exs"

  if [ "$TARGET_MIX_ENV" = "prod" ]; then
    status "copying '$_prod_secret_path' file to build host"
    scp "$_prod_secret_path" "$BUILD_USER@$BUILD_HOST:$BUILD_AT/config/prod.secret.exs"
  fi
}

pre_erlang_clean_compile() {
  status "Copying assets"
  __sync_remote "
    [ -f ~/.profile ] && source ~/.profile
    set -e
    cd '$BUILD_AT'
    cp -r priv/assets/ /var/apps/$APP/
  "
}

pre_erlang_generate_release() {
    status "removing all the releases"
  __sync_remote "
      echo ${RELEASE_DIR}/releases/[0-9]*/
      rm -rf ${RELEASE_DIR}/releases/[0-9]*/
  "
}

A directory for scripts:

mkdir scripts

The file scripts/deploy.sh:

vim scripts/deploy.sh

#!/bin/bash

# this can be refactored:
set -e
echo "[*] deploying app"
source ".deliver/config"

echo "[*] stopping server"

scp lib/${APP}_web/templates/shared/maintenance_on.html $BUILD_USER@$HOST:/var/apps/$APP/maintenance_on.html
ssh $BUILD_USER@$HOST "sudo systemctl stop $APP"
mix edeliver build release --verbose --force

echo "[*] starting server"

ssh $BUILD_USER@$HOST "sudo systemctl start $APP"
ssh $BUILD_USER@$HOST "sudo mv /var/apps/$APP/maintenance_on.html /var/apps/$APP/maintenance_off.html"

echo "[OK] done"

Make it executable:

sudo u+x scripts/deploy.sh

systemd service

We create it in scripts/my_app123.service

[Unit]
Description=your_app123
After=syslog.target
Requires=postgresql.service

[Service]
ExecStart=/home/your_user123/apps/your_app123/bin/your_app123 foreground
ExecStop=/home/your_user123/apps/your_app123/bin/your_app123 stop
Restart=on-abort
WorkingDirectory=/home/your_user123/apps/your_app123/bin
Environment=MIX_ENV=prod
SyslogIdentifier=your_app123
User=your_user123

[Install]
WantedBy=multi-user.target

[to be continued…]

Subscribe to Newsletters

Be up to date with new material and freebies