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:python3.12

  cache:
    paths:
      - .cache/pip

  before_script:
    - "echo database: {host: postgres, port: 5432} > $CONFIG_PATH"
    - pip install -e .[test]

  # 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:17-3.5
      alias: postgres

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

  script:
    - arkindex test

backend-lint:
  image: python:3.12
  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:17-3.5
      alias: postgres

  script:
    - arkindex 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

# Make sure arkindex is always compatible with Nuitka
backend-build-binary:
  stage: build

  image: python:3.12

  before_script:
    - pip install nuitka

  script:
    - python -m nuitka --nofollow-imports --include-package=arkindex --nofollow-import-to=*.tests arkindex/manage.py

  except:
    - schedules

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