Github Actions enables you to build, test, and deploy your Phoenix App from GitHub. Actions use the idea of workflows and in this tutorial, we're going to configure a basic build-and-test workflow using a new Phoenix Application.
Let's get started.
Create Phoenix App
Phoenix comes out of the box with a few tests which should be enough to get us started with Github Actions.
Ensure your tests run successfully before proceeding:
$ mix test
Compiling 16 files (.ex)
Generated status app
18:04:55.437 [info] Already up
Finished in 0.04 seconds
3 tests, 0 failures
We'll need to configure the test Repo to connect to the postgres hostname, so open config/test.exs:
(replace my_app and MyApp)
We're targeting Github Actions using an environment variable that will only be set in CI.
This will configure the Repo to use the correct configuration.
Github Actions uses the concept of workflows to perform different steps.
Create a new file: .github/workflows/ci.yml:
Breaking it Down
This may look like a lot of code, but it's a basic yaml file that instructs Actions on which commands to run for each step.
Each step in the workflow has a name property that describes the current action.
Here are the steps:
- Use Ubuntu as a Docker container.
- Set some environment variables on the container for our app to read.
- Use Postgres Docker container, and set default user and DB.
- Checkout the code.
- Elixir setup action.
- Check for cached version of mix dependencies.
- NodeJS action.
- Optional chromedriver action (feel free to remove this if you're not using integration tests).
- Install mix dependencies.
- Compile frontend assets.
- Run tests.
Now you can commit your code to the Github repo and under the "Actions" tab you can watch the progress.
$ git add .
$ git commit -m "Add github actions"
$ git push origin
As you can see from the output, the cache action we added runs the Post actions/cache@v1 step that will cache our mix deps.
This cache will be used in the Run actions/cache@v1 step to save some amount of time per workflow run.
There are many more actions and steps that you can customize for your own workflows but this has just covered the most basic use-case.
Hopefully you found this useful! We'll likely expound on this and cover more CI systems if there's enough interest.
Thanks for reading!