kind: pipeline
type: docker
name: cannery

steps:
- name: restore-cache
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    restore: true
    mount:
      - _build
      - deps
      - .npm
      - .mix

- name: test
  image: elixir:1.18.3-otp-27-alpine
  environment:
    TEST_DATABASE_URL: ecto://postgres:postgres@database/cannery_test
    HOST: testing.example.tld
    MIX_HOME: /drone/src/.mix
    MIX_ARCHIVES: /drone/src/.mix/archives
    MIX_ENV: test
  commands:
  - apk add --no-cache build-base npm git
  - mix local.rebar --force
  - mix local.hex --force
  - mix deps.get
  - npm set cache .npm
  - npm --prefix ./assets ci --no-audit --prefer-offline
  - mix do phx.digest, gettext.extract, assets.deploy
  - mix test.all

- name: build and publish stable
  image: plugins/docker
  privileged: true
  settings:
    repo: shibaobun/cannery
    purge: true
    compress: true
    platforms:
      - linux/amd64
      - linux/arm64
      - linux/arm/v7
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    tags: latest
  when:
    branch:
    - stable

- name: build and publish tagged version
  image: plugins/docker
  privileged: true
  settings:
    repo: shibaobun/cannery
    purge: true
    compress: true
    platforms:
      - linux/amd64
      - linux/arm64
      - linux/arm/v7
    username:
      from_secret: docker_username
    password:
      from_secret: docker_password
    tags:
      - ${DRONE_TAG}
  when:
    event:
    - tag

- name: rebuild-cache
  image: drillster/drone-volume-cache
  volumes:
  - name: cache
    path: /cache
  settings:
    rebuild: true
    mount:
      - _build
      - deps
      - .npm
      - .mix

services:
- name: database
  image: postgres:13
  environment:
    POSTGRES_USER: postgres
    POSTGRES_PASSWORD: postgres
    POSTGRES_DB: postgres

volumes:
  - name: cache
    host:
      path: /run/media/default/ssdsrv/gitea/drone-cache
  - name: docker_sock
    host:
      path: /var/run/docker.sock