dist: xenial
language: node_js
node_js: lts/*
cache:
  yarn: false
  npm: false

addons:
  apt:
    packages:
      - libgconf-2-4
  snaps:
    - docker
  firefox: "latest-esr"

install:
  - yarn global add wait-on
  # Install Codecov
  - yarn install --frozen-lockfile
  - cp backend/.env.template backend/.env

before_script:
  - docker-compose -f docker-compose.yml build --parallel
  - docker-compose -f docker-compose.yml -f docker-compose.build-and-test.yml build # just tagging, just be quite fast
  - docker-compose -f docker-compose.yml -f docker-compose.build-and-test.yml up -d
  - wait-on http://localhost:7474
  - docker-compose  -f docker-compose.yml -f docker-compose.build-and-test.yml exec backend yarn run db:migrate init

script:
  - export CYPRESS_RETRIES=1
  - export BRANCH=$(if [ "$TRAVIS_PULL_REQUEST" == "false" ]; then echo $TRAVIS_BRANCH; else echo $TRAVIS_PULL_REQUEST_BRANCH; fi)
  - echo "TRAVIS_BRANCH=$TRAVIS_BRANCH, PR=$PR, BRANCH=$BRANCH"
  # Miscellaneous
  - ./scripts/translations/sort.sh
  - ./scripts/translations/missing-keys.sh
  # Backend
  - docker-compose exec backend yarn run lint
  - docker-compose exec backend yarn run test --ci --verbose=false --coverage
  - docker-compose exec backend yarn run db:seed
  - docker-compose exec backend yarn run db:reset
  # Frontend
  - docker-compose exec webapp yarn run lint
  - docker-compose exec webapp yarn run test --ci --verbose=false --coverage
  # Fullstack
  - docker-compose down
  - docker-compose -f docker-compose.yml up -d
  - wait-on http://localhost:7474
  # disable for last deploy, because of flakiness!
  # - yarn run cypress:run --record
  # - yarn run cucumber
  # Coverage
  # disable this uneffective thing for last deploy, because of easyness!
  # - yarn run codecov

after_success:
  - wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh
  - chmod +x send.sh
  - ./send.sh success $WEBHOOK_URL
  - if [ $TRAVIS_BRANCH == "master" ] && [ $TRAVIS_EVENT_TYPE == "push" ]; then
    wget https://raw.githubusercontent.com/Human-Connection/Discord-Bot/develop/tester.sh &&
    chmod +x tester.sh &&
    ./tester.sh staging $WEBHOOK_URL;
    fi

after_failure:
  - wget https://raw.githubusercontent.com/DiscordHooks/travis-ci-discord-webhook/master/send.sh
  - chmod +x send.sh
  - ./send.sh failure $WEBHOOK_URL

before_deploy:
  - go get -u github.com/tcnksm/ghr
  # stop deployment to kubernetes until we have set it up
  # - ./scripts/setup_kubernetes.sh

deploy:
  - provider: script
    script: bash scripts/docker_push.sh
    on:
      branch: master
  # stop deployment to kubernetes until we have set it up
  # - provider: script
  #   script: bash scripts/deploy.sh
  #   on:
  #     branch: master
