image: node:16.14.2-slim

include:
  - template: 'Workflows/MergeRequest-Pipelines.gitlab-ci.yml'

stages:
  - build
  - 🧪 test
  - 📝 publish
  - 🧽 clean

variables:
  SUB_GROUP:          "www"

.cache:
  tags:
    - cache
  cache:
    paths:
      - node_modules/

.cache-push:
  extends: .cache
  cache:
    policy: push

.cache-pull:
  extends: .cache
  cache:
    policy: pull

install_dependencies:
  extends: .cache-push
  stage: build
  script:
    - npm ci

lint:
  extends: .cache-pull
  stage: 🧪 test
  script:
    - npm run lint

pages:
  extends: .cache-pull
  stage: 📝 publish
  script:
    - npm run build
    - mv build public
  artifacts:
    paths:
      - public
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  environment:
    name: production
    url: "${DOCUSAURUS_URL}"

pages:preview:
  extends: pages
  artifacts:
    expire_in: 2 week
  rules:
    - if: $CI_MERGE_REQUEST_IID
      variables:
          DOCUSAURUS_URL:     "https://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}"
          DOCUSAURUS_BASEURL: "/-/${SUB_GROUP}/${CI_PROJECT_NAME}/-/jobs/${CI_JOB_ID}/artifacts/public/"
  environment:
    name: preview/${CI_COMMIT_REF_NAME}
    url: "https://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-/${SUB_GROUP}/${CI_PROJECT_NAME}/-/jobs/${CI_JOB_ID}/artifacts/public/index.html"
    on_stop: pages:preview:stop

pages:preview:stop:
  stage: 🧽 clean
  rules:
    - if: $CI_MERGE_REQUEST_IID
      when: manual
  allow_failure: true
  environment:
    name: preview/${CI_COMMIT_REF_NAME}
    action: stop
  script:
    - echo "🧽 clean MR environment [preview/${CI_COMMIT_REF_NAME}]"
