image: ubuntu:xenial

cache:
  paths:
    - output/ # Node modules and dependencies

before_script:
  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
  - eval $(ssh-agent -s)
  - ssh-add <(echo "$SSH_PRIVATE_KEY")
  - mkdir -p ~/.ssh
  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'

build:
  stage: build
  script:
    - apt-get install -y --force-yes build-essential python-dev python-pip git nodejs nodejs-legacy npm
    - pip install --upgrade pip==9.0.3
    - pip install -r requirements.txt
    - cd content && npm install && cd ..
    - sed -i "s/RELATIVE_URLS = True/RELATIVE_URLS = False/" pelicanconf.py
    - apt-get install -y --force-yes locales
    - locale-gen fr_FR
    - locale-gen fr_FR.UTF-8
    - update-locale LANG=fr_FR.UTF-8
    - . /etc/default/locale
    - pelican content
    # krb5 auth
    - apt-get install -y --force-yes krb5-user krb5-config
    - echo "#!/bin/sh" > hex2bin
    - echo "sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' \"$1\" | xargs printf" > hex2bin
    - echo "#!/bin/sh" > bin2hex
    - echo "hexdump -v -e '1/1 \"%02x\"' \"$1\"" > bin2hex
    - echo "$KEYTAB" > my.keytab.hex
    - ./hex2bin my.keytab.hex > my.keytab
    - kinit -k -t my.keytab vimazeno@LOCAL.ISIMA.FR
    - ssh -K vimazeno@perso.isima.fr "mkdir -p ~/public_html/"

  artifacts:
    untracked: true

deploy:
  stage: deploy
  dependencies:
    - build
  script:
    - apt-get install -y --force-yes rsync
    - rsync -az --exclude '_' -e ssh output/ vimazeno@perso.isima.fr:~/public_html/
  when: manual
