---
# Publishes a hugo repo as GitLab pages.
# The job name must be pages for it to work.

.pages:
  image:
    name: registry.gitlab.com/just-ci/images/hugo:latest
    entrypoint: [""]
  variables:
    MINIFY: "false"
    EXTRA_ARGS: ""
  # Add a before script to add an external theme, for example:
  # .pages:
  #   before_script:
  #     - git clone https://github.com/matcornic/hugo-theme-learn.git themes/hugo-theme-learn
  script:
    - |
      if [ ${MINIFY} = "true" ]; then
        hugo --minify ${EXTRA_ARGS}
        /minify.sh .
      else
        hugo ${EXTRA_ARGS}
      fi
    - gzip -kr public
    - |
      if [ ! "${CI_PROJECT_DIR}" = "${PWD}" ]; then
        mv public "${CI_PROJECT_DIR}/public"
      fi
    - |
      if [ "${CI_COMMIT_BRANCH}" = "${CI_DEFAULT_BRANCH}" ]; then
        echo "[!] When browing the test job artifacts you may not see the full final result when opening an HTML file, as the built site may reference absolute or canonified URLs."
      fi
  artifacts:
    paths:
      - public
    when: always

pages:
  extends: .pages
  stage: build
  rules:
    - if: $CI_COMMIT_REF_NAME == $CI_DEFAULT_BRANCH

pages:dev:
  extends: .pages
  stage: test
  environment:
    name: Development
    url: "https://$CI_SERVER_URL/-/$CI_PROJECT_PATH/-/jobs/$CI_JOB_ID/artifacts/file/public/index.html"
  rules:
    - if: $CI_COMMIT_REF_NAME != $CI_DEFAULT_BRANCH
