image: node:14.17.1-slim

stages:
  - 🧪 test
  - 📝 publish
  - 🧽 clean

variables:
  SUB_GROUP:          "www"

lint:
  stage: 🧪 test
  tags:
    - cache
  script:
    - rm -Rf node_modules
    - npm ci
    - npm run lint
  cache:
    policy: push
    key:
      files:
        - package.json
    paths:
      - node_modules

pages:
  stage: 📝 publish
  tags:
    - cache
  script:
    - npm run build
    - mv build public
  artifacts:
    paths:
      - public
  cache:
    key:
      files:
        - package.json
    paths:
      - node_modules
  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}]"

