image: registry.gitlab.com/soapbox-pub/rebased/ci

variables: &global_variables
  POSTGRES_DB: pleroma_test
  POSTGRES_USER: postgres
  POSTGRES_PASSWORD: postgres
  DB_HOST: postgres
  DB_PORT: 5432
  MIX_ENV: test
  # Needed for Dokku deployment.
  # https://github.com/dokku/dokku/issues/2514#issuecomment-616775470
  GIT_DEPTH: 0

cache: &cache
  key:
    files:
      - mix.lock
  paths:
    - deps
    - _build
  policy: pull

stages:
  - deps
  - test
  - deploy

deps:
  stage: deps
  script:
    - mix deps.get
    - mix deps.compile
  cache:
    <<: *cache
    policy: pull-push
  only:
    changes:
      - mix.lock

openapi:
  stage: test
  only:
    changes:
      - ".gitlab-ci.yml"
      - "lib/pleroma/web/api_spec/**/*.ex"
      - "lib/pleroma/web/api_spec.ex"
  artifacts:
    paths:
      - spec.json
  script:
    - mix pleroma.openapi_spec spec.json

test:
  stage: test
  only:
    changes: &elixir-changes
      - ".gitlab-ci.yml"
      - "**/*.ex"
      - "**/*.exs"
      - "mix.lock"
  services: &db-services
    - name: postgres:13-alpine
      alias: postgres
      command: ["postgres", "-c", "fsync=off", "-c", "synchronous_commit=off", "-c", "full_page_writes=off"]
  before_script: &db-setup
    - mix ecto.create
    - mix ecto.migrate
  script:
    - mix test --cover --preload-modules
  coverage: '/^Line total: ([^ ]*%)$/'
  artifacts:
    reports:
      coverage_report:
        coverage_format: cobertura
        path: coverage.xml

test-erratic:
  stage: test
  allow_failure: true
  only:
    changes: *elixir-changes
  services: *db-services
  before_script: *db-setup
  script:
    - mix test --only=erratic

lint:
  stage: test
  only:
    changes: *elixir-changes
  script:
    - mix format --check-formatted

# analysis:
#   stage: test
#   only:
#     changes: *elixir-changes
#   script:
#     - mix credo --strict --only=warnings,todo,fixme,consistency,readability

cycles:
  stage: test
  only:
    changes: *elixir-changes
  script:
    - mix xref graph --format cycles --label compile | awk '{print $0} END{exit ($0 != "No cycles found")}'

# Deploy with Dokku
# https://github.com/dokku/gitlab-ci
# https://github.com/dokku/ci-docker-image
review:
  image: dokku/ci-docker-image
  stage: test
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://rebased-$CI_COMMIT_REF_SLUG.dokku.soapbox.pub
  only:
    - branches
  except:
    - main
  variables:
    GIT_REMOTE_URL: ssh://dokku@$DOKKU_HOST/rebased-$CI_COMMIT_REF_SLUG
    GIT_PUSH_FLAGS: --force
  script: dokku-deploy
  allow_failure: true

release:
  stage: deploy
  variables:
    MIX_ENV: prod
    PLEROMA_BUILD_BRANCH: $CI_COMMIT_REF_NAME
  script:
    - mix deps.get
    - mkdir release
    - mix release --path release
  artifacts:
    name: "rebased-$CI_COMMIT_REF_NAME-$CI_COMMIT_SHORT_SHA-$CI_JOB_NAME"
    paths:
      - release/*
  cache: {}
  only:
    refs:
      - main
    changes: *elixir-changes

docker:
  stage: deploy
  image: docker:20.10.17
  cache: {}
  services:
    - docker:20.10.17-dind
  tags:
    - dind
  # https://medium.com/devops-with-valentine/how-to-build-a-docker-image-and-push-it-to-the-gitlab-container-registry-from-a-gitlab-ci-pipeline-acac0d1f26df
  script:
    - echo $CI_REGISTRY_PASSWORD | docker login -u $CI_REGISTRY_USER $CI_REGISTRY --password-stdin
    - docker build -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE
  only:
    refs:
      - main
