stages:
  - test
  - build
  - deploy
  - production

docs:
  stage: test
  image: davidhrbac/docker-mdcheck:latest
  allow_failure: true
  script:
  - mdl -r ~MD013 *.md docs.it4i/

mkdocs:
  stage: build
  image: davidhrbac/docker-mkdocscheck:latest
  script:
  #- apt-get update
  #- apt-get -y install git
  - bash add_version.sh
  - bash get_modules.sh
  - mkdocs build
  - bash clean_json.sh site/mkdocs/search_index.json
  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
  - shellcheck *.sh

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/

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
  when: manual
