Skip to content

Instantly share code, notes, and snippets.

@dberget
Last active May 22, 2018 15:22
Show Gist options
  • Save dberget/f5bac406f4cba841b070a7c15826e554 to your computer and use it in GitHub Desktop.
Save dberget/f5bac406f4cba841b070a7c15826e554 to your computer and use it in GitHub Desktop.
Elixir Release Files

Makefile:

build-release:
	echo "Make sure you bumped the version in mix.exs"
	$(eval VERSION := $(shell sed -n '/version:/s/ *version: "\(.*\)",/\1/p' mix.exs))
	sleep 3
	mix deps.get
	cd assets && ./node_modules/brunch/bin/brunch b -p
	MIX_ENV=prod mix phx.digest
	docker run -it -v `pwd`:/src --rm --workdir /src -e MIX_ENV=prod erlang-builder mix do deps.clean comeonin, deps.get, compile, release --env=prod

release:
	$(eval VERSION := $(shell sed -n '/version:/s/ *version: "\(.*\)",/\1/p' mix.exs))
	ssh root@45.55.254.134 "mkdir /your_app_new"
	scp _build/prod/rel/your_app/releases/$(VERSION)/your_app.tar.gz root@45.55.254.134:/your_app_new/
	ssh root@45.55.254.134 "cd /var/www/your_app && ./bin/your_app stop"
	ssh root@45.55.254.134 "mv /var/www/your_app /your_app_old"
	ssh root@45.55.254.134 "mv /your_app_new /var/www/your_app"
	ssh root@45.55.254.134 "cd /var/www/your_app && tar xzf your_app.tar.gz"
	ssh root@45.55.254.134 "cd /var/www/your_app && PORT=4000 ./bin/your_app start"
	ssh root@45.55.254.134 "rm -rf /your_app_old"

builder:
	cd docker && docker build -t erlang-builder .

Dockerfile:

# Builds an image with erlang and elixir installed
# that matches our production system so the right
# libraries are in place when building releases
FROM ubuntu:16.04

ENV CACHE_BUST 20171114_1053

RUN apt-get update && apt-get install -y apt-transport-https
RUN apt-get install locales && locale-gen en_US.UTF-8
RUN apt-get install -y build-essential git-all wget

RUN wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb 
RUN dpkg -i erlang-solutions_1.0_all.deb
RUN apt-get update && apt-get install -y erlang elixir
 

ENV LANG en_US.UTF-8  
ENV LC_ALL en_US.UTF-8  
ENV LANGUAGE en_US:en 

RUN mix local.hex --force
RUN mix local.rebar --force

Release.Tasks

defmodule Release.Tasks do
  alias Hearthdecks.Tasks.CardUpload

  def migrate do
    {:ok, _path} = Application.ensure_all_started(:hearthdecks)

    path = Application.app_dir(:hearthdecks, "priv/repo/migrations")

    Ecto.Migrator.run(Hearthdecks.Repo, path, :up, all: true)
  end

  def update do
    {:ok, _path} = Application.ensure_all_started(:hearthdecks)

    path = Application.app_dir(:hearthdecks, "priv/repo/migrations")

    Ecto.Migrator.run(Hearthdecks.Repo, path, :up, all: true)

    CardUpload.run()
  end
end

post_start hook:

set +e

echo "post_start hook"

while true; do  
  nodetool ping
  EXIT_CODE=$?
  if [ $EXIT_CODE -eq 0 ]; then
    echo "Application is up!"
    break
  fi
done

set -e

echo "Running migrations"  
/var/www/hearthdecks/bin/hearthdecks rpc Elixir.Release.Tasks update


echo "migrations complete"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment