elixir phoenix 部署与热升级

2018/6/10

主要使用edeliver + distillery

distillery负责打包
edeliver负责部署与热升级

服务器配置

预设部署环境为ubuntu

wget https://packages.erlang-solutions.com/erlang-solutions_1.0_all.deb
sudo dpkg -i erlang-solutions_1.0_all.deb && \
    sudo apt-get update && \
    sudo apt-get install esl-erlang elixir -y && \
    sudo upgrade -y
    
mix local.hex

ssh config中添加

Host phoenix.demo 
    HostName phoenix.demo
    User tony
    IdentityFile ~/.ssh/id_rsa.pub

添加服务器配置到服务器

scp ~/phoenix.demo/config/prod.secret.exs phoenix.demo:/home/tony/app_config/prod.secret.exs

项目配置

项目根目录mix.exs中 deps方法中添加

    {:edeliver, "~> 1.4.3"},
    {:distillery, "~> 1.4"}

执行

mix deps.get

Edeliver & Distillery 配置

cd ~/phoenix.demo
mix release.init

.edeliver/config中添加

APP="myproject"

BUILD_HOST="phoenix.demo"
BUILD_USER="tony"
BUILD_AT="/home/tony/app_build"

PRODUCTION_HOSTS="phoenix.demo" 
PRODUCTION_USER="tony" 
DELIVER_TO="/home/tony/app_release" 

pre_erlang_get_and_update_deps() {
  local _prod_secret_path="/home/tony/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
}
git add .
git commit -m 'demo'
git push

部署

mix edeliver build release # 构建
mix edeliver deploy release to production # 部署到正式环境
mix edeliver start production # 启动升级环境

热升级

任意修改代码后升级mix.exs中的版本号

- version: "0.0.1",
+ version: "0.0.2",

git

git add . && git commit -m "upgrade" && git push

执行

mix edeliver upgrade production

参考

  1. how to automate elixir phoenix deployment with distillery and edeliver on ubuntu 16.04