aliases:
  # Workflow filters
  - &filter-not-release-or-master
    tags:
      ignore: /^v[0-9]+(\.[0-9]+){2}(-.+|[^-.]*)$/
    branches:
      ignore:
        - master
        - docs
        - gh-pages
  - &filter-only-master
    branches:
      only: master
  - &filter-docs
    branches:
      only: docs

version: 2
jobs:
  build:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache:
          keys:
            - dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: yarn install
          command: 'yarn install --pure-lockfile --no-progress'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - run: npm run build

  lint:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      - restore_cache:
          keys:
            - dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: yarn install
          command: 'yarn install --pure-lockfile --no-progress'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - run: npm run lint

  test:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
    docker:
      - image: circleci/node:8
    steps:
      - checkout
      # Prepare for artifact and test results collection equivalent to how it was done on 1.0.
      # In many cases you can simplify this from what is generated here.
      # 'See docs on artifact collection here https://circleci.com/docs/2.0/artifacts/'
      - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
      - restore_cache:
          keys:
            - dependency-cache-{{ checksum "yarn.lock" }}
      - run:
          name: yarn install
          command: 'yarn install --pure-lockfile --no-progress'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - run: npm run ci-test
      - store_test_results:
          path: /tmp/circleci-test-results
      - store_artifacts:
          path: /tmp/circleci-artifacts
      - store_artifacts:
          path: /tmp/circleci-test-results

  build-docs:
    working_directory: ~/grafana-zabbix
    docker:
      - image: circleci/python:2.7
    steps:
      - run:
          name: install git lfs
          command: 'curl -s https://packagecloud.io/install/repositories/github/git-lfs/script.deb.sh | sudo bash'
      - run: sudo apt-get install git-lfs
      - run: git lfs install
      - run: sudo pip install --upgrade pip
      - run: sudo pip install mkdocs
      - checkout
      - run: git lfs fetch --all
      - run: cd docs && mkdocs build --clean
      - persist_to_workspace:
          root: .
          paths:
            - docs/site

  deploy-docs:
    working_directory: ~/grafana-zabbix
    docker:
      - image: circleci/node:8
    environment:
      GH_PAGES_BRANCH: gh-pages
      CI_GIT_USER: CircleCI
      CI_GIT_EMAIL: ci@grafana.com
    steps:
      - add_ssh_keys:
          fingerprints:
            - "dc:7e:54:e0:aa:56:4d:e5:60:7b:f3:51:24:2d:d3:29"
      - checkout
      - attach_workspace:
          at: ../gh-pages
      - run: ./.circleci/deploy-docs.sh

workflows:
  version: 2
  build-master:
    jobs:
      - build:
          filters: *filter-only-master
      - lint:
          filters: *filter-only-master
      - test:
          filters: *filter-only-master

  build-branches-and-prs:
    jobs:
      - build:
          filters: *filter-not-release-or-master
      - lint:
          filters: *filter-not-release-or-master
      - test:
          filters: *filter-not-release-or-master

  build-docs:
    jobs:
      - build-docs:
          filters: *filter-docs
      - deploy-docs:
          requires:
            - build-docs
          filters: *filter-docs
