defaults_go: &DEFAULTS_GO
  language: go
  go: "1.12.2"
  cache:
    directories:
      # https://restic.net/blog/2018-09-02/travis-build-cache
      - $HOME/.cache/go-build
      - $HOME/gopath/pkg/mod

defaults_js: &DEFAULTS_JS
  language: node_js
  # we run make to install everything
  install: []
  cache:
    yarn: true

jobs:
  include:
    - stage: Test
      <<: *DEFAULTS_GO
      env:
        - DESC="Test Go code"
        - GO111MODULE=on
      before_script:
        - travis_retry make mock-assets
      script: make test-go
      after_success:
        - travis_retry bash <(curl -s https://codecov.io/bash) -F backend

    - stage: Test
      <<: *DEFAULTS_JS
      env:
        - DESC="Test JavaScript code"
        - NODE_ENV=test
      script: make test-js
      after_success:
        - travis_retry bash <(curl -s https://codecov.io/bash) -F ui -s ui

    # duplicate js test but with a different time zone, to ensure that tests/code work with non-UTC time zone
    - stage: Test
      <<: *DEFAULTS_JS
      env:
        - DESC="Test JavaScript code with Pacific/Easter time zone"
        - NODE_ENV=test
        - TZ=Pacific/Easter
      script: make test-js
      after_success:
        - travis_retry bash <(curl -s https://codecov.io/bash) -F ui -s ui

    - stage: Lint
      <<: *DEFAULTS_JS
      env:
        - DESC="Lint git commit"
      script: make lint-git-ci

    - stage: Lint
      <<: *DEFAULTS_GO
      env:
        - DESC="Lint Go code"
        - GO111MODULE=on
      before_script:
        - travis_retry make mock-assets
      script: make lint-go

    - stage: Lint
      <<: *DEFAULTS_JS
      env:
        - DESC="Lint JavaScript code"
      script: make lint-js

    - stage: Lint
      <<: *DEFAULTS_JS
      env:
        - DESC="Lint documentation"
      script: make lint-docs

    - state: Lint
      <<: *DEFAULTS_GO
      env:
        - DESC="Verify go.sum"
        - GO111MODULE=on
      script:
        - travis_retry make mock-assets
        - travis_retry go get -d -v
        - travis_retry go mod tidy
        - git diff --exit-code

    - stage: Build and Deploy
      # we build and push docker image on merge and tag, so this should only
      # run for pull requests, to test that they don't break docker builds
      if: repo = prymitive/karma AND (branch != master OR type = pull_request) AND tag IS NOT present
      language: generic
      addons:
        apt:
          packages:
            - docker-ce
      env:
        - DESC="Build docker image"
      script: travis_retry make docker-image

    - stage: Build and Deploy
      <<: *DEFAULTS_GO
      env:
        - DESC="Cross compile binaries"
        - GO111MODULE=on
      # disable cache so we always build from scratch
      cache: false
      before_script:
        # this stage needs to build everything including assets file and that
        # requires running webpack, so we need nodejs here
        - travis_retry nvm install $(< .nvmrc)
      script:
        # compile assets via webpack and build those into bindata_assetfs.go file
        - travis_retry make bindata_assetfs.go
        # and now compile using bakelite for all target archs
        - travis_retry go install github.com/terinjokes/bakelite
        - export SOURCE_DATE_EPOCH=$(git show -s --format=%ci ${TRAVIS_TAG:-${TRAVIS_COMMIT}}^{commit})
        - bakelite -platforms="-plan9" -ldflags="-X main.version=\"$(make show-version)\"" github.com/prymitive/karma
        - for i in karma-*; do tar --mtime="${SOURCE_DATE_EPOCH}" --owner=0 --group=0 --numeric-owner -c $i | gzip -n - > $i.tar.gz; done
        - shasum -a 512 karma-*.tar.gz | tee sha512sum.txt
        # verify that there are no uncommited changes
        - git diff --exit-code
      deploy:
        provider: releases
        api_key:
          secure: Wmix2qr1q6BakTWI4Wc6hQEw27OZMb04OeAfPhM84ZUMd30t9BNAdfERsNkuY2VylnJ9KhbBdHmd0XWqqAvI55/lZTLkUW1gD8ZEjly23w735HNm/9vHESjmUUZdZK/bLFLr1cQq3+jgGdO0ZNcAEGJhKZNA1TcdA7t0uNhg0xSejVYRMxOLx3wzVF9oGSq36m8nhLIWG9mlRkZBfE+0Q4pHJK+diqICoTUIZNhZO1Lu8uO6jNpAmgRfhF7q1LGcoa0ZeHV8OsB5OhpLXDExyNw4znxPzWBu5y9+85fjeRKpmGd0KFT2PRnwB9XkSo20Mmk94x+t07+0VR6NtyKXPG5g8i/PxLQr4f1ZwPXNW0xKq5t9KLycnj3Gf/xlaXyk4d5V64EKUTbDP37emodCwPyxaozoRfDvbT7Oyj1jtHoj+6ijYrPBpjc4fnz7xBoydD5R+we/dzcnM/1ZlFnpGckWsCL7QyNuGQQJ9vk2Xo3H5wylL1Ol2BB/aWZYAuBbwXaXZnOg79b/2SQkxHrEveAbDlVovSvqsOtq6DI6oY4VyWsQzjQHvmFMfTp1LPWwh/EL1q2+feDM4vEsx9OMLegFCoAzKPy9xm0teZbYKdMfJammBjvRYZh9h+Hw43/atXktG3zSuR4C/HmXLoswq+c2bjJWlcM8jvB29oo5uA4=
        skip_cleanup: true
        file_glob: true
        file:
          - karma-*.tar.gz
          - sha512sum.txt
        on:
          repo: prymitive/karma
          tags: true

    - stage: Build and Deploy
      if: repo = prymitive/karma AND ((type = push AND branch = master) OR tag IS present)
      language: generic
      addons:
        apt:
          packages:
            - docker-ce
      env:
        - DESC="Build and push docker image"
        # DOCKER_PASSWORD
        - secure: "XULRlOHN17A1KUSzST1TKo5UQtSS0/T6y2Rp88gJ/jDOraFKW3IuGHBM3uE3uALBUfrLnB6DGrrtS1x3hvWSvpnBmNmCK94qGYpEppAU2b8+DfUGQqI1D7sa/FXAaxqkA6LDKxcdgbagd8kE/3gRmayjHeisCDPvy3BhVZVEfzSIaKilzXnHrCywEpB2DWRUsYkdZdw2tG674626ZcMwJUajkeW4fxmvWYrskE92+7MSuz9DExxYWLLah12+Ug1URD1UWyFeIdMK0z3S0bN4Bsd+RT0ScDelRMhad5sTTqGWbLC3nr10aMRYzXYhp2pUBPtK2abby4KebY8hToxf6Dr8f7Nz4BRA3nqwp/7Q79hloU7IYdtnvSdvMp3Pc3YDsHiYqAO+G/jfX2j4t8CGIk3hHDUZ+4/PnYddV+OFR8CujH4Wq85FzHPyfRmZuB6MdPQQEtW4B/Mmf3qsTYXZIwDSKU2fnJM6eEnDW7vZIb55Y8nFC4x8b4fVyY7/wSQgDVtd933BmYDUxYo9AjjAfigLBE30zalr/TFibIRWUewoYqvJJ803AJU/M54vzQQ9l9ypoNadGgHa0PuPrnza9WufHGNkm7jRfhKFDUi/RvdFX8u7NtTpB9bAk/Mk+9qcLIkVnm+9SCcA80KR9LLwp8SZChBMHNnNTs47T0cDzNI="
      script:
        - export VERSION=$(make show-version)
        - export LOCAL_IMAGE="karma:${VERSION}"
        - export DOCKER_USERNAME=lmierzwa
        - export DOCKER_IMAGE=karma
        - export IMAGE_NAME="${DOCKER_USERNAME}/${DOCKER_IMAGE}"
        - travis_retry make docker-image
        - travis_retry docker login -u "${DOCKER_USERNAME}" -p "${DOCKER_PASSWORD}"
        - docker tag "${LOCAL_IMAGE}" "${IMAGE_NAME}:latest"
        # tag with the version only if we have a git tag (new release was pushed)
        - if [ -n "$TRAVIS_TAG" ]; then docker tag "${LOCAL_IMAGE}" "${IMAGE_NAME}:${VERSION}" ; fi
        - travis_retry docker push "${IMAGE_NAME}"

    - stage: Build and Deploy
      # deploy on every job that isn't a PR, this way we:
      # * don't deploy on PRs send from forks, only internal
      # * deploy only once per PR - since we will deploy on branch job
      if: repo = prymitive/karma AND type != pull_request
      env:
        - DESC="Deploy demo app to Heroku"
        # HEROKU_TOKEN, valid forever, needed to push docker image and release
        # it on heroku app
        - secure: "zr1fHhSIZQgA7wT8PALNyAhilCZBpvziL2zuC7LJvYy9PSHatV1B+/Tl5Ao1MGlqiD9wHdRXhw/Z7Ol7vR84LlEXIQv/PZvpYtdGrwP/dmwEzRi59puNHW/sDa5fU27U5bgGW9VPYKzQFGBIknRz9yEpGAsDqzWSRwEQofgnuF1Cv0JJXN/tcZs/fcXz4AhFSXRb8Rde2geHRVGlz3UnuECQ9LnzTI/xxIP/+YORvMpTcwJtQwq/NhucYXzms19XM94xz5IE/cwf8yV9YZalm867aR2yQJvkMmOaufSYoFgRrghqnpzEe1wyuZvAXkwwZErw5swBY3Zo1YkGUeU761g3v+Nh+dlVKFaBVYgDt9W9bb1QsK1Lbgix4UYSx8Tz06X83xz2f6hWXS1Yvju7yE7M1VmjAhevWW+ZpTf3vwOH2UeUHyAMOddggMSIRfaxC9W74Trt8zxKlM+8sQiaEE3c6Ea+ZJxq1baDJvHQPdfuj2844uKaAL7qNVuRNRPAa0bp0qkzLyl3f5P3XK54mM4vayBRCQ+qflq+XGXY5G8+LukUNnKMq/KuPZZ1A6pOr3kTj4qKaxAcxOJQq4/xc+zJaiQFkzfMj1//LKMyvrRtqMnPV+P3qtgMGzA4Z3JlHUOgPHgbZ9WTlpV5yi066Onro+j2NFehjY+FV6R2gOI="
      language: generic
      addons:
        apt:
          packages:
            - docker-ce
      before_script:
        # a safe and boring version of cat > .netrc < EOF
        - echo "machine api.heroku.com" >> $HOME/.netrc
        - echo "  login l.mierzwa@gmail.com" >> $HOME/.netrc
        - echo "  password $HEROKU_TOKEN" >> $HOME/.netrc
      script:
        - travis_retry docker build --build-arg VERSION=$(make show-version) -t registry.heroku.com/karma-demo/web -f demo/Dockerfile .
        - travis_retry docker login -u _ -p "$HEROKU_TOKEN" registry.heroku.com
        - travis_retry docker push registry.heroku.com/karma-demo/web
        # bundled heroku cli doesn't know anything about containers, update it
        - travis_retry curl https://cli-assets.heroku.com/install.sh | sh
        - travis_retry /usr/local/bin/heroku container:release web --app karma-demo
