image: registry.gitlab.com/arkindex/backend/base:latest
stages:
  - test
  - build
  - deploy

# For jobs that run backend scripts directly
.backend-setup:
  cache:
    paths:
      - .cache/pip

  before_script:
    - apk --update add build-base
    # Custom line to install our own deps from Git using GitLab CI credentials
    - "pip install -e git+https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/arkindex/common#egg=arkindex-common"
    - "pip install -e git+https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/arkindex/ponos#egg=ponos-server"
    - "pip install -e git+https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/arkindex/transkribus#egg=transkribus-client"
    - pip install -r tests-requirements.txt codecov
    - "echo 'database: {host: postgres, port: 5432}' > $CONFIG_PATH"

  variables:
    # For the postgres image
    POSTGRES_DB: arkindex_dev
    POSTGRES_USER: devuser
    POSTGRES_PASSWORD: devdata

    # For the backend
    CONFIG_PATH: "$CI_PROJECT_DIR/config.yml"

    # Pip cache
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

backend-tests:
  extends: .backend-setup
  stage: test

  services:
    - postgres:12.0

  script:
    - python setup.py test
    - codecov

backend-lint:
  image: python:3
  stage: test

  cache:
    paths:
      - .cache/pip
      - .cache/pre-commit

  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
    PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit"

  before_script:
    - pip install pre-commit

  script:
    - pre-commit run -a

backend-migrations:
  extends: .backend-setup
  stage: test

  services:
    - postgres:12.0

  script:
    - pip install -e .
    - arkindex/manage.py makemigrations --check --noinput --dry-run -v 3

backend-openapi:
  extends: .backend-setup
  stage: build

  script:
    - ci/openapi.sh

  artifacts:
    paths:
      - output/

backend-static:
  extends: .backend-setup
  stage: build

  script:
    - ci/static-collect.sh

  artifacts:
    paths:
      - static

backend-build-base:
  stage: build
  image: docker:19.03.1
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375/

  # Run this only on base tags
  rules:
    - if: '$CI_COMMIT_TAG =~ /^base-.*/'
      when: on_success
    - when: never

  script:
    - ci/build-base.sh

backend-build:
  stage: build
  image: docker:19.03.1
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375/

  # Run this on master and tags except base tags
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: on_success
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
      when: on_success
    - when: never

  script:
    - ci/build.sh Dockerfile

backend-build-binary:
  stage: build
  image: docker:19.03.1
  services:
    - docker:dind
  variables:
    DOCKER_DRIVER: overlay2
    DOCKER_HOST: tcp://docker:2375/

  # Run this on master and tags except base tags
  rules:
    - if: '$CI_COMMIT_BRANCH == "master"'
      when: on_success
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
      when: on_success
    - when: never

  script:
    - ci/build.sh Dockerfile.binary "-binary"

backend-static-deploy:
  stage: deploy

  # Run this on any version tag except base images
  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
      when: on_success
    - when: never

  # Run immediately once backend-static ends without waiting for others
  needs:
    - backend-static

  # Ensure artifacts are available
  dependencies:
    - backend-static

  script:
    - ci/static-deploy.sh

backend-openapi-deploy:
  stage: deploy

  # Run on master updates only
  only:
   - master

  # Ensure artifacts are available
  dependencies:
    - backend-openapi

  before_script:
    - pip install awscli

  script:
    - aws s3 cp output/schema.yml s3://teklia-assets-release/arkindex/openapi.yml

sentry-release:
  image: getsentry/sentry-cli
  stage: deploy

  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
      when: on_success
    - when: never

  variables:
    RELEASE: "arkindex-backend@$CI_COMMIT_TAG"
    SENTRY_ORG: teklia
    SENTRY_PROJECT: arkindex-backend

  dependencies:
    - backend-build

  script:
    - sentry-cli releases new --finalize $RELEASE

release-notes:
  stage: deploy
  image: registry.gitlab.com/teklia/devops:latest

  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG !~ /^base-.*/'
      when: on_success
    - when: never

  script:
    - devops release-notes
