version: 2.1

latest: &latest
  pattern: "^1.17.*-erlang-27.*$"

tags: &tags
  [
    1.17.2-erlang-27.0.1-alpine-3.20.1,
    1.16.3-erlang-26.2.5-alpine-3.19.1,
    1.15.7-erlang-26.2.4-alpine-3.18.6,
    1.14.5-erlang-25.3.2.11-alpine-3.17.7,
    1.13.4-erlang-24.3.4-alpine-3.15.3
  ]

jobs:
  build-test:
    parameters:
      tag:
        type: string
    docker:
      - image: hexpm/elixir:<< parameters.tag >>
    working_directory: ~/repo
    environment:
      LC_ALL: C.UTF-8
    steps:
      - run:
          name: Install system dependencies
          command: apk add --no-cache build-base libmnl-dev
      - checkout
      - run:
          name: Install hex and rebar
          command: |
            mix local.hex --force
            mix local.rebar --force
      - restore_cache:
          keys:
            - v1-mix-cache-<< parameters.tag >>-{{ checksum "mix.lock" }}
      - run: mix deps.get
      - run: mix compile --warnings-as-errors
      - run: mix test --exclude has_ipv6 --exclude weather --exclude icmp || mix test --failed
      - when:
          condition:
            matches: { <<: *latest, value: << parameters.tag >> }
          steps:
            - run: mix format --check-formatted
            - run: mix deps.unlock --check-unused
            - run: mix docs
            - run: mix hex.build
            - run: mix credo -a --strict
            - run: mix dialyzer
      - save_cache:
          key: v1-mix-cache-<< parameters.tag >>-{{ checksum "mix.lock" }}
          paths:
            - _build
            - deps

workflows:
  checks:
    jobs:
      - build-test:
          name: << matrix.tag >>
          matrix:
            parameters:
              tag: *tags
