---
stages:
  - build
  - deploy

build:
  stage: build
  image: 
    entrypoint: ['']
    name: klakegg/hugo:pandoc
  script:
    - hugo
  artifacts:
    paths:
      - public
    expire_in: 1 hour
  only:
    - master

deploy:
  stage: deploy
  image: 
    entrypoint: ['']
    name: alpine/git:latest
  dependencies:
    - build
  before_script:
    - git config --global user.email "${GITLAB_USER_EMAIL}"
    - git config --global user.name "${GITLAB_USER_NAME}"
    - git remote show origin
  script:
    - git add -f public
    - CHANGES=$(git status --porcelain | wc -l)
    # following command comes from https://dev.to/ranb2002/how-to-push-to-a-git-repository-from-a-gitlab-ci-pipeline-4ikp
    - if [ "$CHANGES" -gt "0" ]; then 
        git status;
        git commit -m "Update public from ${CI_COMMIT_SHORT_SHA}";
        git push "https://${CI_USER_NAME}:${CI_ACCESS_TOKEN}@${CI_REPOSITORY_URL#*@}" "HEAD:${CI_COMMIT_REF_NAME}" -o skip-ci;
      fi
  only:
  - master