---

variables:
    MY_PROJECT_ID: "13001358"


include:
  - project: nofusscomputing/projects/gitlab-ci
    ref: development
    file:
      - .gitlab-ci_common.yaml
      - template/automagic.gitlab-ci.yaml

Documentation.Lint:
  rules:
    - when: never

Documentation.Build:
  rules:
    - when: never

Website.Lint:
    extends: .Lint_Markdown_Docs
    variables:
      MDLINT_PATHS: "pages/*.md pages/**/*.md pages/**/**/*.md pages/**/**/**/*.md pages/**/**/**/**/**/*.md #CHANGELOG.md !gitlab-ci !website-template"


Website.Build:
  extends: .MKDocs_Build
  needs: [ 'Website.Lint' ]
  resource_group: build


Assemble.Website.Prepare:
#  extends: .MKDocs_Build
  stage: prepare
#  image: python:3.7.5-buster
# turn mkdocs build template script section to a command template so that customizations can be added.
  script:
    - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/source"
#    - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build"
    - echo "fetch artifacts from child repo's"
    - echo "copy static pages source to" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/source"
    - echo "copy sub-repo source to (merge)" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/source"
    - echo "mkdocs build source dir"
#    - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/full-site"
    - mv "$CI_PROJECT_DIR/artifacts/build/Website.Build/build" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/"
    #- ls -laR $CI_PROJECT_DIR
    # remove ops placeholder index.html
    
# Project: Operations
    - echo "[DEBUG] fetch operations docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/32419575/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output operations-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/32419575/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip operations-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/operations/index.html";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/operations" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: Gitlab-CI
    - echo "[DEBUG] fetch gitlab-ci project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/28543717/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output gitlab-ci-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/28543717/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip gitlab-ci-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/gitlab-ci";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/gitlab-ci" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/gitlab-ci/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: ansible role, git_configuration
    - echo "[DEBUG] fetch git_configuration project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/45705596/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output git_configuration-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/45705596/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip git_configuration-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/git_configuration";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/git_configuration" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/git_configuration/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: Docker Mail Server
    - echo "[DEBUG] fetch docker-mail project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/33611657/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output docker-mail-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/33611657/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip docker-mail-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/docker-mail";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/docker-mail" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/docker-mail/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: Ansible Execution Environment
    - echo "[DEBUG] fetch execution_environment project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/45741845/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output execution_environment-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/45741845/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip execution_environment-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/execution_environment";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/execution_environment" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/execution_environment/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: NodeRED LDAP Self Service
    - echo "[DEBUG] fetch nodered_ldap_self_service project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/48321671/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output nodered_ldap_self_service-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/48321671/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip nodered_ldap_self_service-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ldap_self_service";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ldap_self_service" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ldap_self_service/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi

# Project: docker GLPI
    - echo "[DEBUG] fetch docker-glpi project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/12928828/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output docker-glpi-artifacts.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/12928828/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip docker-glpi-artifacts.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/glpi";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/glpi" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/glpi/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: kubernetes monitoring helm chart
    - echo "[DEBUG] fetch kubernetes_monitoring project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/50510268/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output kubernetes_monitoring.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/50510268/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip kubernetes_monitoring.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/kubernetes_monitoring";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/kubernetes_monitoring" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/kubernetes_monitoring/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: Ansible Public Playbooks
    - echo "[DEBUG] fetch Ansible Public Playbooks project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/46364551/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output ansible_playbooks.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/46364551/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip ansible_playbooks.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/playbooks";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ansible/playbooks" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/playbooks/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: Ansible role common
    - echo "[DEBUG] fetch Ansible common project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/52226103/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output nfc_common.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/52226103/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip nfc_common.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/common";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ansible/roles/common" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/common/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: Ansible role firewall
    - echo "[DEBUG] fetch Ansible firewall project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51640016/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output nfc_firewall.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51640016/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip nfc_firewall.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/firewall";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ansible/roles/firewall" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/firewall/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: Ansible role homeassistant
    - echo "[DEBUG] fetch Ansible Public homeassistant project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51020674/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output nfc_homeassistant.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51020674/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip nfc_homeassistant.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/homeassistant";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ansible/roles/homeassistant" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/homeassistant/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi


# Project: Ansible role kubernetes
    - echo "[DEBUG] fetch Ansible kubernetes project docs"
    - 'HTTP_STATUS_FILE=$(curl --location -o /dev/null --silent --head --write-out "%{http_code}" --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51640029/jobs/artifacts/development/download?job=Documentation%2EBuild")'
    - echo "[DEBUG] HTTP_STATUS_FILE=$HTTP_STATUS_FILE"
    - |
      if [ "0$HTTP_STATUS_FILE" != "0200" ]; then 
        echo "[ERROR] Unable to fetch Job Artifacts due to HTTP status of $HTTP_STATUS_FILE"; 
        # exit 1; 
      else
        curl --location --output nfc_kubernetes.zip --header "PRIVATE-TOKEN: ${GIT_COMMIT_TOKEN}" "https://gitlab.com/api/v4/projects/51640029/jobs/artifacts/development/download?job=Documentation%2EBuild";
        unzip nfc_kubernetes.zip;
        rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/kubernetes";
        cp -rvf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build/build/projects/ansible/roles/kubernetes" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/projects/ansible/roles/kubernetes/";
        rm -Rf "$CI_PROJECT_DIR/artifacts/build/Documentation.Build";
      fi




    # # below 2 lines commented out as need to ffigure out how to download artifacts.
    # - rm -Rf "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/operations/index.html"
    # - echo "cp -rvn" "$CI_PROJECT_DIR/artifacts/build/Website.Build/build/operations" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/"
    
    
    # # copy ops pages into main site, not overwriting
    # #- cp -rvn "$CI_PROJECT_DIR/artifacts/build/Static Pages/build/"* "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/"
    

    # # below line commented out as need to ffigure out how to download artifacts.
    # - cp -rvf "$CI_PROJECT_DIR/artifacts/build/Website.Build/build/operations" "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/build/"
    
    - echo "copy prepare files (sitemap, search file) to fullsite (overwrite)"
    - echo "copy each sub-repo build to fullsite (merge)"
#    - echo mv "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/full-site" public
  needs: 
    - 'Website.Build'
    # only available in gitlab premium
    # use: - "curl -O --header 'PRIVATE-TOKEN: ${GITLAB_API_TOKEN}' https://gitlab.example.com/api/v4/projects/${PROJECT_A_ID}/jobs/${REMOTE_JOB_ID}/artifacts/${REMOTE_FILENAME}"
    # - project: nofusscomputing/ops
    #   job: Static Pages
    #   ref: development
    #   artifacts: true
  artifacts:
    expire_in: 24 hrs
    when: always
    paths:
    - "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/*"
  resource_group: build
  rules:
    # - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master" && $CI_PIPELINE_SOURCE == "push"'
    #   when: always

      # Build docs on tag so they can be downloaded from the tag job and are always available.
    - if:  # condition_git_tag
        $CI_COMMIT_TAG != null &&
        $CI_COMMIT_BRANCH == null
      exists:
        - '{docs/**,pages/**}/*.md'
      when: always

    - if:  # condition_master_branch_push
        $CI_COMMIT_BRANCH == "master" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      when: always

    - if:  # condition_dev_branch_push
        $CI_COMMIT_BRANCH == "development" && 
        ( 
          $CI_PIPELINE_SOURCE == "pipeline"
            ||
          $CI_PIPELINE_SOURCE == "push"
            ||
          $CI_PIPELINE_SOURCE == "schedule"
        )
        # See nofusscomputing/projects/gitlab-ci#34 for extra $CI_PIPELINE_SOURCE
      exists:
        - '{docs/**,pages/**}/*.md'
      # No changes check # See nofusscomputing/projects/gitlab-ci#34
      # changes:
      #   paths:
      #     - '{docs/**,pages/**}/*.md'
      #   compare_to: 'master'
      when: always

    - if:  # condition_not_master_or_dev_push
        $CI_COMMIT_BRANCH != "master" && 
        $CI_COMMIT_BRANCH != "development" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      changes:
        paths:
          - '{docs/**,pages/**}/*.md'
        compare_to: 'development'
      when: always

    - when: never


pages:
  stage: deploy
  variables:
    GIT_STRATEGY: none
  script:
    - mv "$CI_PROJECT_DIR/artifacts/prepare/Assemble.Website.Prepare/build" public
  needs: [ 'Assemble.Website.Prepare' ]
  environment: 
    name: staging
    url: $CI_PAGES_URL
  artifacts:
    paths:
    - public
  rules:
    # - if: '$CI_COMMIT_BRANCH && $CI_COMMIT_BRANCH != "master" && $CI_PIPELINE_SOURCE == "push"'
    #   when: on_success

      # Build docs on tag so they can be downloaded from the tag job and are always available.
    - if:  # condition_git_tag
        $CI_COMMIT_TAG != null &&
        $CI_COMMIT_BRANCH == null
      exists:
        - '{docs/**,pages/**}/*.md'
      when: always

    - if:  # condition_master_branch_push
        $CI_COMMIT_BRANCH == "master" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      when: always

    - if:  # condition_dev_branch_push
        $CI_COMMIT_BRANCH == "development" && 
        ( 
          $CI_PIPELINE_SOURCE == "pipeline"
            ||
          $CI_PIPELINE_SOURCE == "push"
            ||
          $CI_PIPELINE_SOURCE == "schedule"
        )
        # See nofusscomputing/projects/gitlab-ci#34 for extra $CI_PIPELINE_SOURCE
      exists:
        - '{docs/**,pages/**}/*.md'
      # No changes check # See nofusscomputing/projects/gitlab-ci#34
      # changes:
      #   paths:
      #     - '{docs/**,pages/**}/*.md'
      #   compare_to: 'master'
      when: always

    - if:  # condition_not_master_or_dev_push
        $CI_COMMIT_BRANCH != "master" && 
        $CI_COMMIT_BRANCH != "development" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      changes:
        paths:
          - '{docs/**,pages/**}/*.md'
        compare_to: 'development'
      when: always

    - when: never


.Pytest_template:
  stage: test
  image: ubuntu:18.04
  before_script:
    - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME"
    - mkdir -p "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/tests"
    - apt update
    - apt install -y python3 python3-pip ca-certificates
    - apt install --no-install-recommends -y chromium-chromedriver
    - pip3 install --upgrade pip
    - pip3 install -r test/requirements.txt
  artifacts:
    expire_in: 24 hrs
    when: always
    paths:
      - "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/*"
    reports:
      junit:
        - "*.junit.xml"
  rules:
    # - if: '$CI_COMMIT_BRANCH && $CI_PIPELINE_SOURCE == "push"'
    #   when: on_success

      # Build docs on tag so they can be downloaded from the tag job and are always available.
    - if:  # condition_git_tag
        $CI_COMMIT_TAG != null &&
        $CI_COMMIT_BRANCH == null
      exists:
        - '{docs/**,pages/**}/*.md'
      when: on_success

    - if:  # condition_master_branch_push
        $CI_COMMIT_BRANCH == "master" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      when: on_success

    - if:  # condition_dev_branch_push
        $CI_COMMIT_BRANCH == "development" && 
        ( 
          $CI_PIPELINE_SOURCE == "pipeline"
            ||
          $CI_PIPELINE_SOURCE == "push"
            ||
          $CI_PIPELINE_SOURCE == "schedule"
        )
        # See nofusscomputing/projects/gitlab-ci#34 for extra $CI_PIPELINE_SOURCE
      exists:
        - '{docs/**,pages/**}/*.md'
      
      # No changes check # See nofusscomputing/projects/gitlab-ci#34
      # changes:
      #   paths:
      #     - '{docs/**,pages/**}/*.md'
      #   compare_to: 'master'
      when: on_success

    - if:  # condition_not_master_or_dev_push
        $CI_COMMIT_BRANCH != "master" && 
        $CI_COMMIT_BRANCH != "development" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      changes:
        paths:
          - '{docs/**,pages/**}/*.md'
        compare_to: 'development'
      when: on_success

    - when: never


Unit Tests:
  extends: .Pytest_template
  needs: [ 'Website.Build' ]
  script:
    - mv "$CI_PROJECT_DIR/artifacts/build/Website.Build/build" build
    - pytest --verbose --junitxml=unit_test.junit.xml --tb=line test/unit
    - cp *.junit.xml "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/"
    - echo "[DEBUG] python_exit[$python_exit]"


#Integration Tests:
#  extends: .Pytest_template
#  needs: 
#    - pages
#    - 'Unit Tests'
#  script:
#   - echo "placeholder job for integration tests" > "$CI_PROJECT_DIR/artifacts/$CI_JOB_STAGE/$CI_JOB_NAME/DETEMEME.txt"


public_website:
  stage: publish
  image: alpine
  variables:
    GIT_STRATEGY: none
  before_script:
    - ls -la /html
  script:
    - rm -rf /html/*
    - cp -r "$CI_PROJECT_DIR/artifacts/prepare/Assemble.Website.Prepare/build"/* /html/
    - ls -laR /html/
  needs: [ 'Assemble.Website.Prepare', 'Unit Tests']
  resource_group: production
  environment: 
    name: production
    url: https://nofusscomputing.com
  artifacts:
    paths:
    - public
  rules:
    - if:  # condition_master_branch_push
        $CI_COMMIT_BRANCH == "master" && 
        $CI_PIPELINE_SOURCE == "push"
      exists:
        - '{docs/**,pages/**}/*.md'
      when: on_success

    - if:  # condition_dev_branch_push
        $CI_COMMIT_BRANCH == "development" &&  
        ( 
          $CI_PIPELINE_SOURCE == "pipeline"
            ||
          $CI_PIPELINE_SOURCE == "push"
            ||
          $CI_PIPELINE_SOURCE == "schedule"
        )
        # See nofusscomputing/projects/gitlab-ci#34 for extra $CI_PIPELINE_SOURCE
      exists:
        - '{docs/**,pages/**}/*.md'
      # No changes check # See nofusscomputing/projects/gitlab-ci#34
      # changes:
      #   paths:
      #     - '{docs/**,pages/**}/*.md'
      #   compare_to: 'master'
      allow_failure: true
      when: manual
    - when: never
  tags:
    - production
    - website
