stages:
  - test
  - build
  - deploy

# GitLab provides a template to ensure pipelines run only for branches and tags, not for merge requests
# This prevents duplicate pipelines in merge requests.
# See https://docs.gitlab.com/ee/ci/troubleshooting.html#job-may-allow-multiple-pipelines-to-run-for-a-single-action
include:
  - template: 'Workflows/Branch-Pipelines.gitlab-ci.yml'

# For jobs that run backend scripts directly
.backend-setup:
  image: registry.gitlab.teklia.com/arkindex/backend/base:gitlab-teklia

  cache:
    paths:
      - .cache/pip

  before_script:
    # 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.teklia.com/arkindex/transkribus#egg=transkribus-client"
    - "pip install -e git+https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.teklia.com/arkindex/license#egg=teklia-license"
    - pip install -r tests-requirements.txt
    - "echo 'database: {host: postgres, port: 5432}\npublic_hostname: http://ci.arkindex.localhost' > $CONFIG_PATH"

  # Those jobs require the base image; they might fail if the image is not up to date.
  # Allow them to fail when building a new base image, to prevent them from blocking a new base image build
  # Never run those jobs on scheduled pipelines
  rules:
    - if: '$CI_COMMIT_TAG && $CI_COMMIT_TAG =~ /^base-.*/'
      allow_failure: true
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - when: on_success

  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:
    - name: postgis/postgis:14-3.2
      alias: postgres

  artifacts:
    when: always
    reports:
      junit:
        - test-report.xml

  script:
    - python3 setup.py test

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

  except:
    - schedules

  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:
    - name: postgis/postgis:12-3.1
      alias: postgres

  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 and schedules
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - 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 and schedules
  rules:
    - if: '$CI_PIPELINE_SOURCE == "schedule"'
      when: never
    - 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
  image: python:3-slim

  # 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
  image: python:3-slim

  # Run on master updates only
  only:
    - master

  except:
    - schedules

  # 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:
  stage: deploy
  image: getsentry/sentry-cli

  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.teklia.com/infra/devops:latest

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

  script:
    - devops release-notes

bump-python-deps:
  stage: deploy
  image: registry.gitlab.teklia.com/infra/devops:latest

  only:
    - schedules

  script:
    - devops python-deps requirements.txt base/requirements.txt tests-requirements.txt
