4

I wrote the following mix.exs to release my Phoenix application as a tar ball, referring to the “Steps” section of the Mix.Tasks.Release documentation.

defmodule MyApp.MixProject do
  use Mix.Project

  def project do
    [
      apps_path: "apps",
      apps: [:shared, :admin, :shop],
      version: "0.1.0",
      start_permanent: Mix.env() in [:qa, :prod],
      deps: deps(),
      releases: [
        my_app: [
          applications: [
            admin: :permanent,
            shop: :permanent
          ],
          steps: [:assemble, &copy_extra_files/1, :tar]
        ]
      ],
      default_release: :my_app
    ]
  end

  defp copy_extra_files(release) do
    File.cp_r("apps/shared/priv/repo/seeds", release.path <> "/seeds")
    release
  end

  defp deps do
    []
  end
end

When I run MIX_ENV=qa mix release my_app, it created a seeds directory under _build/qa/rel/my_app, but when I extract the generated tar ball, it does not contain the seeds directory.

How can I rewrite the mix.exs in order to insert this directory into the tar ball?

Elixir version is 1.11.3.

Note: The same question has been posted on the Elixir Forum.

Tsutomu
  • 4,848
  • 1
  • 46
  • 68

2 Answers2

5

You don't have to copy anything manually since elixir does this for you automatically, if you take a look in your my_app/lib/my_app-0.1.0/priv, there you have all the files you had in your priv folder.

Now in order to access those resources painless without hardcoding the path you can use priv_dir\1:

:code.priv_dir(:my_app)

This is applicable not only for your application, for example if you want your phoenix static resources, you can find them in phoenix priv folder.

Daniel
  • 2,320
  • 1
  • 14
  • 27
  • 1
    Thanks a lot! One note. To use it in Elixir code, we need to write something like `List.to_string(:code.priv_dir(:shared))`. – Tsutomu Jan 18 '21 at 10:57
1

Looking at the source for mix.release, it appears to only select from specific directories when compiling the release.

If you copy seeds into #{release.path}/releases/#{release.version}, it will be included.

defp copy_extra_files(release) do
  File.cp_r(
    "apps/shared/priv/repo/seeds",
    "#{release.path}/releases/#{release.version}/seeds"
  )

  release
end
Adam Millerchip
  • 20,844
  • 5
  • 51
  • 74
  • It's obvious from Daniel's answer that this is the wrong approach. But it could possibly be useful for copying files that are not in the `priv/` directory, so I'll leave it for now. – Adam Millerchip Jan 18 '21 at 12:59