# This file is a template, and might need editing before it works on your project.
# Full project: https://gitlab.com/pages/plain-html
#

stages:
  - testing
  - build
  - review
  - deploy

# always test the preprocessor with unit tests
test-preprocessor:
  stage: testing
  allow_failure: false
  image: 
    name: alpine:latest
  script:
    - apk add --no-cache --upgrade bash
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    # run shell script with testss
    - $RESEARCH_ROOT/bin/run-tests.sh
  artifacts:
    paths:
      - testing/*.log
  only:
    refs:
      - branches
      - merge_requests
    changes:
      - .gitlab-ci.yml
      - bin
      - testing

# always test the pandoc integration and pandoc build with unit tests
pandoc-test:
  stage: testing
  allow_failure: false
  image: 
    name: alpine:latest
  script:
    - apk add --no-cache --upgrade bash
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    # run shell script with testss
    - $RESEARCH_ROOT/bin/test_pandoc.sh
  artifacts:
    paths:
      - testing/test_pandoc/*.log

# test that markdown is translated correctly 
test-md-to-html:
  stage: testing
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - mkdir -p log
    - rm -rf log/test_create_html.log
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    - $RESEARCH_ROOT/bin/test_create_html.sh | tee log/test_create_html.log
  artifacts:
    paths:
      - log/test_create_html.log
  only:
    refs:
      - branches
      - merge_requests
    changes:
      - .gitlab-ci.yml
      - bin
      - testing

# test that html is included correctly 
test-replace_marker:
  stage: testing
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - mkdir -p log
    - rm -rf log/test_replace_marker.log
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    - $RESEARCH_ROOT/bin/test_replace_marker.sh | tee log/test_replace_marker.log
  artifacts:
    paths:
      - log/test_replace_marker.log
  only:
    refs:
      - branches
      - merge_requests
    changes:
      - .gitlab-ci.yml
      - bin
      - testing

# test building standalone static webpage
test-build-standalone:
  stage: testing
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - mkdir -p log
    - rm -rf log/test_build_standalone.log
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    - $RESEARCH_ROOT/bin/test_build_standalone.sh | tee log/test_build_standalone.log
  artifacts:
    paths:
      - log/test_build_standalone.log
  only:
    refs:
      - branches
      - merge_requests
    changes:
      - .gitlab-ci.yml
      - bin
      - testing

# build standalone static webpage
build-standalone-static:
  stage: build
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    # build static webpage
    - $RESEARCH_ROOT/bin/build_standalone.sh
  artifacts:
    paths:
      - public 
  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_COMMIT_BRANCH

# build standalone static webpage
build-static:
  stage: build
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    # build static webpage
    - $RESEARCH_ROOT/bin/build_static.sh
  artifacts:
    paths:
      - static
  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_COMMIT_BRANCH

# build tuhh-snippets
build-tuhh:
  stage: build
  allow_failure: false
  image: 
    name: eltenedor/ubuntu-pandoc:latest
  script:
    - RESEARCH_ROOT=`pwd`; export RESEARCH_ROOT;
    - $RESEARCH_ROOT/bin/preproc_topics.sh $RESEARCH_ROOT/build-tuhh $RESEARCH_ROOT/topics
    - $RESEARCH_ROOT/bin/create_html.sh $RESEARCH_ROOT/build-tuhh $RESEARCH_ROOT/build-tuhh
  artifacts:
    paths:
      - build-tuhh
  rules:
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_COMMIT_BRANCH

# deploy static webpage on issue-branch to web-server hosted asteroids
deploy-on-asteroid:
  stage: review
  image: 
    name: alpine:latest
  before_script:
    ## install dependencies
    #- apk update && apk add openssh-client bash 
    ## see https://docs.gitlab.com/ee/ci/ssh_keys/ for comments
    #- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' #this command is for debian based systems
    - apk update && apk add openssh-client bash  #this command is for alpine
    - eval $(ssh-agent -s)
    - echo "$ASTEROID_SSH_KEY" | tr -d '\r' | ssh-add -
    # See https://stackoverflow.com/a/57332478  about known hosts
    - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
    - echo "$ASTEROID_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 700 ~/.ssh
  script:
    - mkdir .public-asteroid
    - cp -r public/*.html .public-asteroid
    - cp -r img .public-asteroid
    - cp -r css .public-asteroid
    - mv .public-asteroid public-asteroid
    # deploy webpage on asteroid
    #- ssh $ASTEROID_USER@$ASTEROID_HOST "rm -rf $ASTEROID_DEPLOY/$CI_COMMIT_REF_NAME"
    - ssh $ASTEROID_USER@$ASTEROID_HOST "mkdir -p $ASTEROID_DEPLOY/$CI_COMMIT_REF_NAME"
    - scp -v -r public-asteroid "$ASTEROID_USER@$ASTEROID_HOST:$ASTEROID_DEPLOY/$CI_COMMIT_REF_NAME/$CI_COMMIT_SHORT_SHA"
  artifacts:
    paths:
      - public-asteroid
  rules:
    - if: $CI_PROJECT_URL != 'https://collaborating.tuhh.de/cfg0846/research-topics-mat-tuhh'
      when: never
    - when: always

# deploy static webpage on dev-branch to tuhh
deploy-on-tuhh:
  stage: deploy
  image: 
    name: alpine:latest
  before_script:
    ## install dependencies
    - apk update && apk add openssh-client bash 
    ## see https://docs.gitlab.com/ee/ci/ssh_keys/ for comments
    #- 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
    - eval $(ssh-agent -s)
    - echo "$TUHH_SSH_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
    - echo "$TUHH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
    - chmod 700 ~/.ssh
  script:
    - mkdir -p .public-tuhh
    - cp -r build-tuhh/*.html .public-tuhh
    - mv .public-tuhh public-tuhh
    # deploy webpage on tuhh
    - echo 'scp -v public-tuhh' "$TUHH_USER@$TUHH_HOST:$TUHH_DEPLOY"
    - scp -v -r public-tuhh "$TUHH_USER@$TUHH_HOST:$TUHH_DEPLOY"
  artifacts:
    paths:
      - public-tuhh
  rules:
    - if: $CI_PROJECT_URL != 'https://collaborating.tuhh.de/cfg0846/research-topics-mat-tuhh'
      when: never
    - if: $CI_COMMIT_BRANCH == 'dev'
      when: always

# deploy static webpage to web-server via GitLab pages
pages:
  stage: deploy
  script:
    - mkdir .public-pages
    - cp -r public .public-pages
    - mv .public-pages public-pages
  artifacts:
    paths:
      - public-pages
  rules:
    - if: $CI_COMMIT_BRANCH == 'master'

deploy_review:
  stage: deploy
  script:
    - echo "Deploy a review app on an asteroid"
  environment:
    name: asteroid/$CI_COMMIT_REF_NAME
    url: https://review.fabian-gabel.de/$CI_COMMIT_REF_NAME/$CI_COMMIT_SHORT_SHA
  #  on_stop: stop_review_app
  #  auto_stop_in: 1 week
  rules:
    - if: $CI_PROJECT_URL != 'https://collaborating.tuhh.de/cfg0846/research-topics-mat-tuhh'
      when: never
    - if: $CI_MERGE_REQUEST_ID
    - if: $CI_COMMIT_BRANCH == 'dev'

review_tuhh:
  stage: review
  image: 
    name: alpine:latest
  before_script:
    - apk update && apk add openssh-client bash  
    - eval $(ssh-agent -s)
    - echo "$REVIEW_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
    - echo "$REVIEW_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 700 ~/.ssh/known_hosts
  script:
    - mkdir .public-review
    - cp -r public/*.html .public-review
    - cp -r img .public-review
    - cp -r css .public-review
    - mv .public-review public-review
    - ssh -v -p "${REVIEW_SSH_PORT}" "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}" "mkdir -p /usr/local/apache2/htdocs/~${CI_COMMIT_REF_NAME}/$CI_COMMIT_SHORT_SHA"
    - scp -v -P "${REVIEW_SSH_PORT}" -r public-review/* "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}":"/usr/local/apache2/htdocs/~${CI_COMMIT_REF_NAME}/$CI_COMMIT_SHORT_SHA"
  environment:
    name: tuhh/$CI_COMMIT_REF_NAME
    url: https://${REVIEW_PROXY_HOST_NAME}${REVIEW_BASE_DIR}~${CI_COMMIT_REF_NAME}/$CI_COMMIT_SHORT_SHA/
    on_stop: review_tuhh:stop
  rules:
    - if: $CI_PROJECT_URL != 'https://collaborating.tuhh.de/cfg0846/research-topics-mat-tuhh'
      when: never
    - if: '$CI_COMMIT_REF_NAME != "master"'
  
review_tuhh:stop:
  stage: review
  image: 
    name: alpine:latest
  before_script:
    - apk update && apk add openssh-client bash  
    - eval $(ssh-agent -s)
    - echo "$REVIEW_SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
    - echo "$REVIEW_SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 700 ~/.ssh/known_hosts
  script:
    - ssh -p "${REVIEW_SSH_PORT}" "${REVIEW_SSH_USER}"@"${REVIEW_HOST_NAME}" "rm -rf /usr/local/apache2/htdocs/~${CI_COMMIT_REF_NAME}"
  rules:
    - if: '$CI_COMMIT_REF_NAME != "master"'
      when: manual
  allow_failure: true
  environment:
    name: tuhh/$CI_COMMIT_REF_NAME
    action: stop

master-deploy:
  stage: deploy
  image: 
    name: alpine:latest
  before_script:
    - apk update && apk add openssh-client bash  
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add -
    - mkdir -p ~/.ssh && touch ~/.ssh/known_hosts
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 700 ~/.ssh/known_hosts
  script:
    - echo "$SSH_KNOWN_HOSTS" > ~/.ssh/known_hosts
    - chmod 644 ~/.ssh/known_hosts
    - eval $(ssh-agent -s)
    - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
    - ssh -p "${SSH_PORT}" "${SSH_USER}"@"${HOST_NAME}" "mkdir -p /usr/local/apache2/htdocs"
    - scp -P "${SSH_PORT}" -r public/* "${SSH_USER}"@"${HOST_NAME}":"/usr/local/apache2/htdocs"
  cache:
    key: "$CI_COMMIT_REF_SLUG"
  environment:
    name: $CI_COMMIT_REF_NAME
    url: https://$PROXY_HOST_NAME$BASE_DIR
  rules:
    - if: '$CI_COMMIT_REF_NAME == "master"'


# uncomment for manual stopping
#stop_review_app:
#  stage: deploy
#  script: stop-review-app
#  environment:
#    name: review/$CI_COMMIT_REF_NAME
#    action: stop
#  rules:
#    - if: $CI_MERGE_REQUEST_ID
#      when: manual
