image: alpine:latest

# Only run pipelines for pushes on branches
workflow:
  rules:
    - if: $CI_COMMIT_BRANCH

# We can't do variable substitution in a job and then also use that computed
# variable in the Environment URL of the same job, so we have to do the
# substitution in an earlier stage. All of this variable substitution is only
# required for projects in sub-groups because GitLab doesn't have a predefined
# variable that contains the project namespace relative to the root namespace.
#
# For example, if the project path is "/group/subgroup-1" then we need just the
# "/subgroup-1" portion because that is used in the Environment URL path, while
# the "/group" root namespace is used as the subdomain.
setup:
  stage: .pre
  tags:
    - docker
  script:
    - echo "ROOT_RELATIVE_NAMESPACE=${CI_PROJECT_NAMESPACE#$CI_PROJECT_ROOT_NAMESPACE}" >> build.env
  artifacts:
    reports:
      dotenv: build.env

# We can't deploy multiple different variants of GitLab Pages sites (e.g. one
# per branch), so we use this workaround instead. GitLab has a special browsable
# URL for job artifacts on public projects, and we can create an Environment
# that points to that URL so it's easy for devs to know what the current
# Environment URL for their branch is.
.deploy:
  stage: deploy
  tags:
    - docker
  before_script:
    - echo $CI_ENVIRONMENT_URL
    - echo $ROOT_RELATIVE_NAMESPACE
    - test "$ROOT_RELATIVE_NAMESPACE" == "${CI_PROJECT_NAMESPACE#$CI_PROJECT_ROOT_NAMESPACE}" || { echo "ROOT_RELATIVE_NAMESPACE is not set correctly" ; exit 1 ; }
  script:
    - find src/ -type f -name '*.html'
      -exec sed -i
        -e "s|%%BRANCH%%|${CI_COMMIT_BRANCH}|g"
        -e "s|%%COMMIT_SHA%%|${CI_COMMIT_SHORT_SHA}|g"
        -e "s|%%COMMIT_TIMESTAMP%%|${CI_COMMIT_TIMESTAMP}|g"
        -e "s|%%JOB_ID%%|${CI_JOB_ID}|g"
        -e "s|%%JOB_TIMESTAMP%%|${CI_JOB_STARTED_AT}|g"
        -e "s|%%JOB_URL%%|${CI_JOB_URL}|g"
        -e "s|%%PROJECT_PATH%%|${CI_PROJECT_PATH}|g"
        -e "s|%%PROJECT_TITLE%%|${CI_PROJECT_TITLE}|g"
        -e "s|%%PROJECT_URL%%|${CI_PROJECT_URL}|g"
      {} +
    - mv src public
  artifacts:
    paths:
      - public/

# Deploy a review environment for any push to a branch that is not the default
# branch
review:
  extends: .deploy
  rules:
    - if: $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH
  environment:
    name: "review $CI_COMMIT_BRANCH"
    url: "https://${CI_PROJECT_ROOT_NAMESPACE}.${CI_PAGES_DOMAIN}/-${ROOT_RELATIVE_NAMESPACE}/${CI_PROJECT_NAME}/-/jobs/$CI_JOB_ID/artifacts/public/index.html"

# Deploy to GitLab Pages for pushes on the default branch
pages:
  extends: .deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH

# Create a list of URLs that need to be purged from the cache after GitLab Pages
# deployment. Each real file/directory has multiple URLs because the router for
# GitLab Pages fudges paths for better user experience. The fudging rules are:
#  - Directories can be accessed with or without a trailing slash
#  - Files can be accessed with or without a trailing slash
#  - Files with the '.html' extension can be accessed with or without .html
create-purge-list:
  stage: deploy
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  needs: ['pages']
  tags:
    - docker
  script:
    # Add the index-less homepage
    - echo "$CI_PAGES_URL" > purge-list.txt
    # Add all the HTML files and HTML symlinks, with and without .html extension
    - |-
      for f in $(find public/* \( -type f -or -type l \) -iname '*.html'); do
        f="${f#public/}"
        echo "$CI_PAGES_URL/$f" >> purge-list.txt
        echo "$CI_PAGES_URL/${f%.html}" >> purge-list.txt
      done
    # Add everything else
    - |-
      for f in $(find public/* \( -type d -or -type f -or -type l \) -not -iname '*.html'); do
        echo "$CI_PAGES_URL/${f#public/}" >> purge-list.txt
      done
    # Remove any duplicate URLs
    - sort -u -o purge-list.txt purge-list.txt
    # Duplicate each line, adding a trailing slash to the duplicates
    - sed -i 'p;s|$|/|' purge-list.txt
    - cat purge-list.txt
  artifacts:
    paths:
      - purge-list.txt

trigger-cache-purge:
  stage: .post
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  trigger:
    include: purge-cache.gitlab-ci.yml
  variables:
    PARENT_PIPELINE_ID: $CI_PIPELINE_ID
    CF_PURGE_CACHE_CHUNK_SIZE: 20


# vi: set ts=2 sw=2 et ft=yaml: