Elixir application runtime
Overview
Elixir is a functional language that runs on the Erlang virtual machine, built for concurrent and fault-tolerant applications. Clever Cloud builds your Mix project. It starts Phoenix web applications by default, and any other project, such as a Plug server, with the task you set.
Create your Elixir application
To create a new Elixir application, use the Clever Cloud Console or Clever Tools:
clever create --type elixirConfigure your Elixir application
Mandatory needs
An Elixir application needs:
- A
mix.exsfile and itsmix.lockfile, at the root of the project or of the folder set inCC_APP_FOLDER - A web server listening on
0.0.0.0, on the port inPORT
Mix is the build tool of Elixir. mix.exs describes your project and its dependencies. mix deps.get records their exact versions in mix.lock: commit it, so that the build fetches the versions you tested. A Clever Task doesn’t need a web server.
A Phoenix application generated by mix phx.new reads PORT in config/runtime.exs, and listens on every interface in production. It also needs, in the environment of your application:
MIX_ENV=prod, which builds and runs the production configurationSECRET_KEY_BASE, a secret generated withmix phx.gen.secretPHX_HOST, the domain name of your application, which Phoenix uses to build its URLs
clever env set MIX_ENV prod
clever env set SECRET_KEY_BASE "$(mix phx.gen.secret)"
clever env set PHX_HOST myapp.cleverapps.ioYour application reads its environment variables with System.get_env, in config/runtime.exs or in its code, such as add-on credentials.
Phoenix reads the URL of its database in DATABASE_URL, through Ecto, but a linked PostgreSQL add-on provides POSTGRESQL_ADDON_URI. In config/runtime.exs, replace the database_url assignment with this one, and keep the rest:
database_url =
System.get_env("DATABASE_URL") || System.get_env("POSTGRESQL_ADDON_URI") ||
raise "environment variable DATABASE_URL is missing"Build phase
On a new build, Clever Cloud selects the Elixir and Erlang/OTP versions and runs the pre-build hook. It then installs Hex, the package manager, and Rebar, the Erlang build tool. Finally, it fetches and compiles your dependencies, and builds your project:
mix local.hex --force
mix local.rebar --force
mix deps.get
mix deps.compile
npm install
mix compile
npm run deploynpm builds the web assets of projects that use it, from their package.json file, in the folder set in CC_PHOENIX_ASSETS_DIR, the root of the project by default. Without this file, npm logs an error, and the build goes on.
To run another Mix task instead of mix compile, set it in CC_MIX_BUILD_GOAL.
Build Phoenix assets
A Phoenix application created by mix phx.new builds its JavaScript and CSS with assets.deploy, an alias for several Mix tasks. To run it during the build, add a cc.build alias to the aliases list of mix.exs, and keep the others.
This alias compiles the project, then builds the assets:
defp aliases do
[
# Keep the aliases mix phx.new generated, and add:
"cc.build": ["compile", "assets.setup", "assets.deploy"]
]
endThen run it instead of mix compile:
clever env set CC_MIX_BUILD_GOAL cc.buildElixir and Erlang/OTP versions
Elixir runs on the virtual machine and libraries of Erlang/OTP. Clever Cloud uses CC_ELIXIR_VERSION, with the matching OTP version:
| Elixir | Erlang/OTP |
|---|---|
1.14 (EOL) | 25 |
1.15 | 26 |
1.16 | 26 |
1.17 | 27 |
1.18 | 27 |
1.19 (default) | 28 |
1.20 | 29 |
The end-of-life (EOL) versions are marked as such.
To use Elixir 1.18 and Erlang/OTP 27, for instance:
clever env set CC_ELIXIR_VERSION 1.18Start phase
Before it starts your application, Clever Cloud installs Hex and Rebar again, and prepares Phoenix and its database:
mix phx.digestadds a fingerprint to the names of the static files, so that browsers load their new versionsmix ecto.migrateruns the Ecto migrations, which update the structure of your databasemix phx.serverstarts Phoenix
Each of these steps has its own variable:
CC_PHOENIX_DIGEST_GOALreplacesmix phx.digestwith another Mix taskCC_PHOENIX_RUN_ECTO_MIGRATE=falseskipsmix ecto.migrateCC_PHOENIX_APP_DIRsets the folder of the migrations, the root of the project by defaultCC_PHOENIX_SERVER_GOALreplacesmix phx.serverwith another Mix taskCC_RUN_COMMANDreplaces it with a whole command, run from the root of the repository
The migrations run on every start of every instance. If the digest or the migrations fail, Mix logs an error, and the start goes on.
A Mix project without Phoenix, such as a Plug server, has no mix phx.server task: set its start task, otherwise the deployment fails. Without Phoenix and Ecto, also replace the digest with a task that does nothing more, such as compile, and skip the migrations:
clever env set CC_PHOENIX_SERVER_GOAL "run --no-halt"
clever env set CC_PHOENIX_DIGEST_GOAL compile
clever env set CC_PHOENIX_RUN_ECTO_MIGRATE falseCaches
After a successful build of an application, Clever Cloud keeps a build cache of the commit. Restarting the application without a code change reuses it and skips the build. To build again from scratch, restart without the cache (rebuild):
clever restart --without-cacheWith CC_CACHE_DEPENDENCIES=true, Clever Cloud also keeps the deps and _build folders at the root of the repository, Hex and Rebar, and the npm cache between commits, so the next builds fetch and compile fewer dependencies.
Elixir applications as Clever Tasks
An Elixir application can run as a Clever Task, such as a job or a migration: Clever Cloud builds it, then runs CC_RUN_COMMAND, without the digest and the migrations. The command runs from the root of the repository.
Configure Tasks from the Information panel in the Console or with Clever Tools, here for a priv/tasks/report.exs script:
clever create --type elixir --task "mix run priv/tasks/report.exs"
clever deploy # or clever restart if there is no code change