---
.deploy: &deploy
  tags:
    - esphomedocs
  before_script:
    - mkdir -p /root/.ssh
    - echo "$PRIVATE_KEY" | tr -d '\r' >/root/.ssh/id_rsa
    - chmod 600 /root/.ssh/id_rsa
    - ssh-keyscan -t rsa github.com >>/root/.ssh/known_hosts
    - git config --global user.email "$GITHUB_EMAIL"
    - git config --global user.name "$GITHUB_NAME"
    - git worktree prune
    - rm -rf ../esphomelib _build/html/
  script:
    - mkdir -p _build/html
    - git fetch --force git@github.com:${TARGET_REPO}.git gh-pages:gh-pages
    - git worktree add _build/html gh-pages
    - make ../esphomelib
    - make html
    - make convertimages
    - git -C _build/html add --all
    - git -C _build/html commit -m "Deploy to gh-pages"
    - git -C _build/html push -f git@github.com:${TARGET_REPO}.git gh-pages
  cache:
    paths:
    - _build/doctrees

deploy-release:
  <<: *deploy
  variables:
    TARGET_REPO: OttoWinter/esphomedocs
    PRIVATE_KEY: ${GITHUB_PRIVATE_KEY}
    CNAME: esphomelib.com
    BASE_URL: https://esphomelib.com
  only:
    - current

deploy-beta:
  <<: *deploy
  variables:
    TARGET_REPO: OttoWinter/esphomedocs-beta
    PRIVATE_KEY: ${GITHUB_BETA_PRIVATE_KEY}
    CNAME: beta.esphomelib.com
    BASE_URL: https://beta.esphomelib.com
  only:
    - rc