stages:
  - update
  - build
  - deploy

image: "ruby:2.6-stretch"
variables:
  NOKOGIRI_USE_SYSTEM_LIBRARIES: "true"

build:
  stage: build
  artifacts:
    paths:
      - _site/
    expire_in: 1 day
  cache:
    paths:
      - vendor/bundle/
  except:
    - triggers
  tags:
    - docker
  before_script:
    - bundle install --deployment --without=development --with=test
    - bundle exec rake init
  script:
    - bundle exec rake -s --suppress-backtrace . test

deadlinks:
  stage: deploy
  allow_failure: true
  cache:
    paths:
      - vendor/bundle/
  except:
    - triggers
  tags:
    - docker
  before_script:
    - bundle install --deployment --without=development --with=test
  script:
    - bundle exec rake -s --suppress-backtrace . testlinks

deploy:
  stage: deploy
  environment: production
  image: resourcereasoning/website-deploy
  tags:
    - docker
  only:
    - master
  except:
    - triggers
  # DOC_DEPLOY_USER and DOC_DEPLOY_KEY should be defined as secret variables in the GitLab CI Settings panel
  before_script:
    - eval `ssh-agent`
    - echo "$DOC_DEPLOY_KEY" | ssh-add -
  script:
    - ./.deploy.sh

# Special target triggered when publications repository is updated:
update_publications:
  stage: update
  variables:
    GIT_AUTHOR_NAME: Resource Reasoning Group Backup Bot
    GIT_COMMITTER_NAME: Resource Reasoning Group Backup Bot
    GIT_AUTHOR_EMAIL: rr-gitlab-bot@example.com
    EMAIL: rr-gitlab-bot@example.com
    # PUSH_KEY should be defined as a secret variable in the GitLab UI
    SSH_KEY: $PUSH_KEY
  image: ignoredambience/github-gitlab-sync
  tags:
    - docker
  only:
    - triggers
  before_script:
    - eval `ssh-agent`
    - echo "$SSH_KEY" | ssh-add -
  script:
    - git checkout -B ${CI_COMMIT_REF_NAME}
    - git submodule update --init --remote
    - git add publications
    - git commit -m "[AUTO] Updating publications submodule" || exit 0
    - git remote rm rw || true
    - git remote add rw git@gitlab.doc.ic.ac.uk:${CI_PROJECT_PATH}.git
    - git push rw ${CI_COMMIT_REF_NAME}