stages:
  - test
  - build
  - deploy
  - after_test

variables:
    PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"

docs:
  stage: test
  image: it4innovations/docker-mdcheck:latest
  allow_failure: true
  script:
  - mdl -r ~MD013,~MD010,~MD014,~MD024,~MD026,~MD029,~MD033,~MD036,~MD037,~MD046 *.md docs.it4i # BUGS

pylint:
  stage: test
  image: it4innovations/docker-pycheck:latest
  before_script:
  - source /opt/.venv3/bin/activate
  script:
  - pylint $(find . -name "*.py" -not -name "feslicescript.py")

pysafety:
  stage: test
  image: it4innovations/docker-pycheck:latest
  allow_failure: true
  before_script:
  - source /opt/.venv3/bin/activate
  script:
  - cat requirements.txt | safety check --stdin --full-report

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

ext_links:
  stage: after_test
  image: it4innovations/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 --allow-dupe --allow-redirect
  only:
  - master

404s:
  stage: after_test
  image: it4innovations/docker-mkdocscheck:latest
  before_script:
  - echo "192.168.101.10 docs.it4i.cz" >> /etc/hosts
  - wget -V
  - echo https://docs.it4i.cz/devel/$CI_BUILD_REF_NAME/
  - wget --spider -e robots=off -o wget.log -r -p https://docs.it4i.cz/devel/$CI_BUILD_REF_NAME/ || true
  script:
  - cat wget.log | awk '/^Found [0-9]+ broken link[s]?.$/,/FINISHED/ { rc=-1; print $0 }; END { exit rc }'

mkdocs:
  stage: build
  image: it4innovations/docker-mkdocscheck:latest
  before_script:
  - source /opt/.venv3/bin/activate
  - python -V # debug
  - pip freeze # debug
  - mkdocs -V # debug
  script:
    # add version to footer
  - bash scripts/add_version.sh
    # get modules list from clusters
  - bash scripts/get_modules.sh
    # generate site_url
  - (if [ "${CI_BUILD_REF_NAME}" != 'master' ]; then sed -i "s/\(site_url.*$\)/\1devel\/$CI_BUILD_REF_NAME\//" mkdocs.yml;fi);
    # generate ULT for code link
  - sed -i "s/master/$CI_BUILD_REF_NAME/g" material/partials/toc.html
    # regenerate modules matrix
  - python scripts/modules_matrix.py > docs.it4i/modules-matrix.md
  - python scripts/modules_matrix.py --json > docs.it4i/modules-matrix.json
  - curl -f0 https://code.it4i.cz/sccs/scs-api-public/raw/master/scs_api.server_public.md -o docs.it4i/apiv1.md
    # build pages
  - mkdocs build
    # 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

deploy to stage:
  environment: stage
  stage: deploy
  image: it4innovations/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
  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: it4innovations/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
  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