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

stages:
  - preprocessor-test
  - build
  - review
  - deploy

# always test the preprocessor and pandoc build with unit tests
testing:
  stage: preprocessor-test
  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:
    - bin
    - testing

# build 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

# deploy static webpage on issue-branch to web-server hosted asteroids
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 static/public/*.html .public-asteroid
    - cp -r img .public-asteroid
    - cp -r css .public-asteroid
    - mv .public-asteroid public-asteroid
    # deploy webpage on asteroid
    - scp -v -r public-asteroid "$ASTEROID_USER@$ASTEROID_HOST:$ASTEROID_DEPLOY/$CI_COMMIT_REF_NAME"
  artifacts:
    paths:
      - public-asteroid
  rules:
    - if: $CI_COMMIT_BRANCH == 'dev'
    - if: $CI_MERGE_REQUEST_ID

# deploy static webpage on dev-branch to tuhh
tuhh-deploy:
  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 )'
    - 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 .public-tuhh
    - cp -r static/public/*.html .public-tuhh
    - cp -r img .public-tuhh
    - cp -r css .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_COMMIT_BRANCH == 'dev'

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

deploy_review:
  stage: deploy
  script:
    - echo "Deploy a review app"
  environment:
    name: review/$CI_COMMIT_REF_NAME
    url: https://review.fabian-gabel.de/$CI_COMMIT_REF_NAME
  #  on_stop: stop_review_app
  #  auto_stop_in: 1 week
  rules:
    - if: $CI_MERGE_REQUEST_ID

# 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
