GitHub Actions CI Pipeline For An Elixir / Phoenix App

Here is a complete action that can be put into .github/workflows/ci.yml so that tests and other checks (formatter, Credo, Dialyzer) are ran automatically on each push and each pull request:

name: CI

on: [push, pull_request]

env:
  MIX_ENV: test

jobs:
  build-and-test:
    name: Build and Tests
    runs-on: ${{ matrix.os }}

    strategy:
      matrix:
        os: [ubuntu-20.04]
        elixir: [1.11.2]
        otp: [23.3.4]

    services:
      postgres:
        image: postgres:12.6
        env:
          POSTGRES_USER: postgres
          POSTGRES_PASSWORD: postgres
          POSTGRES_DB: kiz_test
        ports:
          - 5432:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5

    steps:
    - name: Check Out Repository
      uses: actions/checkout@v2

    - name: Prepare Application
      run: cp config/test.secret.ci.exs config/test.secret.exs

    - name: Setup Elixir
      uses: actions/setup-elixir@v1
      with:
        elixir-version: ${{ matrix.elixir }}
        otp-version: ${{ matrix.otp }}
        experimental-otp: true

    - name: Restore Dependencies, Build & Dialyzer PLTs Cache
      uses: actions/cache@v2
      with:
        path: | 
          deps
          _build
          priv/plts
        key: ${{ runner.os }}-mix-${{ matrix.elixir }}-${{ matrix.otp }}-${{ hashFiles('**/mix.lock') }}
        restore-keys: ${{ runner.os }}-mix-${{ matrix.elixir }}-${{ matrix.otp }}

    - name: Install Dependencies
      run: mix deps.get

    - name: Setup DB
      run: mix ecto.setup

    - name: Run Tests
      run: mix test

    - name: Check Formatting
      run: mix format --check-formatted

    - name: Review Code With Credo
      run: mix credo

    - name: Typecheck With Dialyzer
      run: mix dialyzer

Few things to note with that setup. Continue reading