pages:
  image: registry.gitlab.com/rxap/gitlab-ci/apindex:latest
  stage: .post
  needs:
    - job: run
      optional: true
    - job: coverage-report
      optional: true
    - job: writerside
      optional: true
  script: |
    if [ ! -d public ]; then
      echo "No public directory found, create a new one"
    fi

    output="public/${CI_COMMIT_REF_SLUG}"
    mkdir -p "$output"

    echo "Folder structure of the public directory:"
    ls -l public

    if [ -d coverage ]; then
      echo "Copy coverage report to $output"
      # it is not required to create the folder coverage. With the cp command the folder coverage will be created
      cp -r coverage "$output/"
    fi

    if [ -d dist/writerside ]; then
      echo "Copy Writerside help to $output"
      mkdir -p "$output/docs"
      cp -r dist/writerside/* "$output/docs/"
    fi

    if [ -d dist/compodoc ]; then
      echo "Copy Compodoc to $output"
      mkdir -p "$output/compodoc"
      cp -r dist/compodoc/* "$output/compodoc/"
    fi

    if [ -d dist/storybook ]; then
      echo "Copy storybook to $output"
      mkdir -p "$output/storybook"
      cp -r dist/storybook/* "$output/storybook/"
    fi

    echo "generate index.html"
    apindex public
  environment:
    name: "pages/$CI_COMMIT_REF_NAME"
    deployment_tier: other
    url: $CI_PAGES_URL/$CI_COMMIT_REF_SLUG
  allow_failure: true
  cache:
    # storage all page contents into one global cache
    # only one page can exist per project
    key: pages_$CI_PROJECT_ID
    paths:
      - public
  artifacts:
    expire_in: 1 week
    paths:
      - public
  rules:
    - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
      when: on_success
    - if: '$CI_COMMIT_TAG =~ /^v[0-9]+\.[0-9]+\.[0-9]+/'
      when: on_success
    - if: '$CI_COMMIT_BRANCH =~ /^([0-9]+\.[0-9]+\.x|[0-9]+\.x)$/'
      when: on_success
    - when: manual
