stages:
  - test
  - build
  - release

variables:
  # Submodule clone
  GIT_SUBMODULE_STRATEGY: recursive

test:
  image: python:3

  stage: test
  cache:
    paths:
      - .cache/pip

  variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

  before_script:
    - pip install tox

  except:
    - schedules

  script:
    - tox

lint:
  stage: test
  image: python:3

  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

  except:
    - schedules

  script:
    - pre-commit run -a

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

  only:
    - schedules

  script:
    - devops python-deps requirements.txt docs-requirements.txt

.docs:
  image: python:3
  artifacts:
    paths:
      - public

  before_script:
    - pip install -r docs-requirements.txt

  script:
    - mkdocs build --strict --verbose

docs-build:
  extends: .docs
  stage: build

  # Test job outside of tags to ensure the docs still can build before merging
  # Does not use the `pages` name, therefore will be ignored by GitLab Pages
  except:
    - tags
    - schedules

pages:
  extends: .docs
  stage: release

  only:
    - master
    - tags

  except:
    - schedules

docs-deploy:
  image: node:18
  stage: release

  dependencies:
    - docs-build

  before_script:
    - npm install -g surge

  except:
    - master
    - tags
    - schedules

  environment:
    name: ${CI_COMMIT_REF_SLUG}
    url: https://${CI_COMMIT_REF_SLUG}-atr-data-generator.surge.sh
    on_stop: docs-stop-surge

  script:
    - surge public ${CI_ENVIRONMENT_URL}

docs-stop-surge:
  image: node:18
  stage: release
  when: manual

  # Do not try to checkout the branch if it was deleted
  variables:
    GIT_STRATEGY: none

  except:
    - master
    - tags
    - schedules

  environment:
    name: ${CI_COMMIT_REF_SLUG}
    url: https://${CI_COMMIT_REF_SLUG}-atr-data-generator.surge.sh
    action: stop

  before_script:
    - npm install -g surge

  script:
    - surge teardown ${CI_ENVIRONMENT_URL}

