Skip to content

Instantly share code, notes, and snippets.

@myobie
Last active October 10, 2018 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save myobie/2dd66e9252704c14d96e34d480962047 to your computer and use it in GitHub Desktop.
Save myobie/2dd66e9252704c14d96e34d480962047 to your computer and use it in GitHub Desktop.
Automatically build and digest assets for a phoenix application when building an elixir release with distillery
# rel/config.exs
# ...
environment :prod do
# ...
plugin(Example.Phoenix)
end
# ...
# rel/plugins/phoenix.exs
defmodule Example.Phoenix do
use Mix.Releases.Plugin
def before_assembly(_release, _opts) do
info("Deploying assets")
with {_output, 0} <- npm_install(),
{_output, 0} <- npm_deploy(),
_ = phx_digest() do
nil
else
{_output, error_code} ->
throw({:error, :npm_deploy_failed, error_code})
end
end
def after_assembly(_release, _opts), do: nil
def before_package(_release, _opts), do: nil
def after_package(_release, _opts), do: nil
def after_cleanup(_release, _opts), do: nil
defp npm_install do
System.cmd(
"npm",
["install"],
cd: "assets",
into: IO.stream(:stdio, :line)
)
end
defp npm_deploy do
System.cmd(
"npm",
["run", "deploy"],
cd: "assets",
into: IO.stream(:stdio, :line)
)
end
defp phx_digest, do: Mix.Task.run("phx.digest")
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment