setting up elixir based phoenix framework pipelines for gitlab to run tests. This guide is based on the gitlab documentation and a blog , I wrote this as I had to make a few adjustments to actually make the pipelines run the tasks successfully
Add .gitlab-ci.yml
to your project root
Your .gitlab-ci.yml
should contain the following content
image: elixir:1.5
services:
- postgres:9.6
variables:
MIX_ENV: "test"
before_script:
# Setup phoenix dependencies
- apt-get update
- apt-get install -y postgresql-client
- mix local.hex --force
- mix local.rebar --force
- mix deps.get --only test
- mix ci.test
test:
script:
- mix test
The image
tells gitlab to use that particular docker file, feel free to use your elixir version here
The variables
section sets the mix environment to the “test” environment for running the tests
The before_script
contains commands to be run before the script tasks are executed. In our case we are updating the OS, installing postgres-client then setting up hex and rebar. After that we fetch the dependencies.
If you follow the official doc by gitlab then rebar
is not added but this throws out an error
CI.TEST
I introduced this section by defining it in the mix aliases in the mix file. This was because we need to setup the database in the test environment
.....defp aliases do
[
"ecto.setup": ["ecto.create", "ecto.migrate", "run priv/repo/seeds.exs"],
"ecto.reset": ["ecto.drop", "ecto.setup"],
"test": ["ecto.create --quiet", "ecto.migrate", "test"],
"ci.test": ["ecto.drop", "ecto.create --quiet", "ecto.migrate", "test"]
]
end
Previously I used mix ci.reset
which worked fine only that it would run seed data and add it to the database which caused the tests to fail
The test
now defines a task that is supposed to be run by the pipeline. It must contain the word script
which tells gitlab what should be run
Update config
In config/test.exs
update the hostname to use “postgres” if we are running in “CI” environment
config :my_app, MyApp.Repo,
hostname: if(System.get_env("CI"), do: "postgres", else: "localhost"),
No previous migration setting
Incase you do not have any active migrations, the tests will not pass as it requires a directory called migrations
. Create the directory priv/repo/migrations
with a file called .gitkeep
Add, Commit and push
Commit, push then watch your tasks run on gitlab
References
Getting started with GitLab CI/CD
https://medium.com/@nahtnam/using-phoenix-on-gitlab-ci-5a51eec81142