stages:
  - test
  - build
  - deploy

docs:
  stage: test
  image: davidhrbac/docker-mdcheck:latest
  script:
  - mdl -r ~MD013,~MD033,~MD014,~MD026,~MD037 *.md docs.it4i

two spaces:
  stage: test
  image: davidhrbac/docker-mdcheck:latest
  allow_failure: true
  before_script:
  - echo "== Files having more than one space betwee two characters =="
  - find docs.it4i/ -name '*.md' -exec grep "[[:alpha:]]  [[:alpha:]]" {} +
  script:
  - find docs.it4i/ -name '*.md' -exec grep "[[:alpha:]]  [[:alpha:]]" -l {} + | grep docs.it4i/ -v

capitalize:
  stage: test
  image: davidhrbac/docker-mkdocscheck:latest
  # allow_failure: true
  script:
  - find mkdocs.yml docs.it4i/ \( -name '*.md' -o -name '*.yml' \) -print0 | xargs -0 -n1 scripts/titlemd_test.py

spell check:
  stage: test
  image: davidhrbac/docker-npmcheck:latest
  allow_failure: true
  script:
  #- npm i markdown-spellcheck -g
  - mdspell '**/*.md' '!docs.it4i/module*.md' '!docs.it4i/anselm-cluster-documentation/software/omics-master/overview.md' '!docs.it4i/downtimes_history.md' -rns --en-us

ext_links:
  stage: test
  image: davidhrbac/docker-mdcheck:latest
  allow_failure: true
  after_script:
  # remove JSON results
  - rm *.json
  script:
  #- find docs.it4i/ -name '*.md' -exec grep --color -l http {} + | xargs awesome_bot -t 10
  - find docs.it4i/ -name '*.md' -exec grep --color -l http {} + | xargs awesome_bot -t 10 --allow-dupe --allow-redirect
  #- |
  #  COMMIT_MESSAGE=$(git log -1 --pretty=%B | grep "Merge branch 'hot_fix' into 'master'" )
  #  if [[ ${COMMIT_MESSAGE} == *hot_fix* ]]; then
  #    echo "Skipping ci build"
  #    exit 0
  #  else
  #    find docs.it4i/ -name '*.md' ! -name 'modules-*.md' -exec grep --color -l http {} + | xargs awesome_bot -t 10 --allow-dupe --allow-redirect
  #  fi
  only:
  - master

mkdocs:
  stage: build
  image: davidhrbac/docker-mkdocscheck:latest
  script:
  - mkdocs -V
  #- apt-get update
  #- apt-get -y install git
    # add version to footer
  - bash scripts/add_version.sh
    # get modules list from clusters
  - bash scripts/get_modules.sh
    # regenerate modules matrix
  - python scripts/modules-matrix.py > docs.it4i/modules-matrix.md
  - python scripts/modules-json.py > docs.it4i/modules-matrix.json
    # build pages
  - mkdocs build
    # compress search_index.json
    #- bash scripts/clean_json.sh site/mkdocs/search_index.json
    # replace broken links in 404.html
  - sed -i 's,href="" title=",href="/" title=",g' site/404.html
  - cp site/404.html site/403.html
  - sed -i 's/404 - Not found/403 - Forbidden/g' site/403.html
    # compress sitemap
  - gzip < site/sitemap.xml > site/sitemap.xml.gz
  artifacts:
    paths:
    - site
    expire_in: 1 week

shellcheck:
  stage: test
  image: davidhrbac/docker-shellcheck:latest
  allow_failure: true
  script:
  - which shellcheck || apt-get update && apt-get install -y shellcheck
  - find . -name *.sh -not -path "./docs.it4i/*" -not -path "./site/*" -exec shellcheck {} +

deploy to stage:
  environment: stage
  stage: deploy
  image: davidhrbac/docker-mkdocscheck:latest
  before_script:
  # install ssh-agent
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - 'which rsync || ( apt-get update -y && apt-get install rsync -y )'
  # run ssh-agent
  - eval $(ssh-agent -s)
  # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  # disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
  # WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
  - mkdir -p ~/.ssh
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
  - useradd -lM nginx
  script:
  - chown nginx:nginx site -R
  - rsync -a --delete site/ root@"$SSH_HOST_STAGE":/srv/docs.it4i.cz/devel/$CI_BUILD_REF_NAME/
  only:
  - branches@sccs/docs.it4i.cz

deploy to production:
  environment: production
  stage: deploy
  image: davidhrbac/docker-mkdocscheck:latest
  before_script:
  # install ssh-agent
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - 'which rsync || ( apt-get update -y && apt-get install rsync -y )'
  # run ssh-agent
  - eval $(ssh-agent -s)
  # add ssh key stored in SSH_PRIVATE_KEY variable to the agent store
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  # disable host key checking (NOTE: makes you susceptible to man-in-the-middle attacks)
  # WARNING: use only in docker container, if you use it with shell you will overwrite your user's ssh config
  - mkdir -p ~/.ssh
  - echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config
  - useradd -lM nginx
  script:
  - chown nginx:nginx site -R
  - rsync -a --delete site/ root@"$SSH_HOST_STAGE":/srv/docs.it4i.cz/site/
  only:
  - master@sccs/docs.it4i.cz
  when: manual
