version: 2.1

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

executors:
  e2e_exec:
    docker:
      - image: srclosson/grafana-plugin-ci-e2e:latest

jobs:
  build:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/golang:1.14-node
    environment:
      YARN_NO_PROGRESS: "true"
    steps:
      - checkout
      - restore_cache:
          keys:
            - dependency-cache-npm-{{ checksum "yarn.lock" }}
      - restore_cache:
          keys:
            - dependency-cache-go-{{ checksum "go.sum" }}
      - run:
          name: Install Dependencies
          command: 'make install'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-npm-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - save_cache:
          key: dependency-cache-go-{{ checksum "go.sum" }}
          paths:
          - ./vendor
          - /go/pkg/mod
      - run: make build

  lint:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/golang:1.14-node
    steps:
      - checkout
      - restore_cache:
          keys:
            - dependency-cache-npm-{{ checksum "yarn.lock" }}
      - restore_cache:
          keys:
            - dependency-cache-go-{{ checksum "go.sum" }}
      - run:
          name: Install Dependencies
          command: 'make install'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-npm-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - save_cache:
          key: dependency-cache-go-{{ checksum "go.sum" }}
          paths:
          - ./vendor
          - /go/pkg/mod
      - run: make lint

  test:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    environment:
      CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
      CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
    docker:
      - image: circleci/golang:1.14-node
    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-npm-{{ checksum "yarn.lock" }}
      - restore_cache:
          keys:
            - dependency-cache-go-{{ checksum "go.sum" }}
      - run:
          name: Install Dependencies
          command: 'make install'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-npm-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - save_cache:
          key: dependency-cache-go-{{ checksum "go.sum" }}
          paths:
          - ./vendor
          - /go/pkg/mod
      - run: make test-ci
      - run: bash <(curl -s https://codecov.io/bash) || echo "Codecov did not collect coverage reports"
      - store_test_results:
          path: /tmp/circleci-test-results
      - store_artifacts:
          path: /tmp/circleci-artifacts
      - store_artifacts:
          path: /tmp/circleci-test-results

  get-plugin:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/golang:1.14-node
    environment:
      YARN_NO_PROGRESS: "true"
    steps:
      - checkout
      - restore_cache:
          keys:
            - dependency-cache-npm-{{ checksum "yarn.lock" }}
      - restore_cache:
          keys:
            - dependency-cache-go-{{ checksum "go.sum" }}
      - persist_to_workspace:
          root: ~/alexanderzobnin/
          paths:
            - grafana-zabbix

  package-plugin:
    executor: e2e_exec
    working_directory: ~/alexanderzobnin/grafana-zabbix
    steps:
      - attach_workspace:
          at: ~/alexanderzobnin
      - run:
          name: Install Dependencies
          command: 'make install'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-npm-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - save_cache:
          key: dependency-cache-go-{{ checksum "go.sum" }}
          paths:
          - ./vendor
          - /go/pkg/mod
      - run:
          name: Build plugin for all platforms
          command: 'make dist'
          no_output_timeout: 15m
      - run:
          name: Prepare plugin for packaging
          command: |
            mkdir -p ci/jobs/build_plugin
            mv dist/ ci/jobs/build_plugin
            cp CHANGELOG.md ci/jobs/build_plugin/dist
      - run:
          name: Package and sign plugin
          command: yarn grafana-toolkit plugin:ci-package
          no_output_timeout: 15m
      - persist_to_workspace:
          root: ~/alexanderzobnin/
          paths:
            - grafana-zabbix
      - store_artifacts:
          path: ci/packages

  make-github-release:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/golang:1.14-node
    environment:
    steps:
      - attach_workspace:
          at: ~/alexanderzobnin
      - run:
          name: Prepare assets
          command: mv ci/packages/* ./
      - run:
          name: Publish release
          command: 'node ./.circleci/github/publishRelease.js'
          no_output_timeout: 15m

  make-release-commit:
    working_directory: ~/alexanderzobnin/grafana-zabbix
    docker:
      - image: circleci/golang:1.13-node
    environment:
      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
      - restore_cache:
          keys:
            - dependency-cache-npm-{{ checksum "yarn.lock" }}
      - restore_cache:
          keys:
            - dependency-cache-go-{{ checksum "go.sum" }}
      - run:
          name: Install Dependencies
          command: 'make install'
          no_output_timeout: 15m
      - save_cache:
          key: dependency-cache-npm-{{ checksum "yarn.lock" }}
          paths:
          - ./node_modules
      - save_cache:
          key: dependency-cache-go-{{ checksum "go.sum" }}
          paths:
          - ./vendor
          - /go/pkg/mod
      - run: ./.circleci/make-release.sh

  build-docs:
    working_directory: ~/grafana-zabbix
    docker:
      - image: circleci/python:2.7
    steps:
      - run: sudo pip install --upgrade pip
      - run: sudo pip install mkdocs
      - checkout
      - run: cd docs && mkdocs build --clean
      - persist_to_workspace:
          root: .
          paths:
            - docs/site

  deploy-docs:
    working_directory: ~/grafana-zabbix
    docker:
      - image: circleci/golang:1.14-node
    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

  codespell:
    docker:
    - image: circleci/python

    steps:
    - checkout
    - run: sudo pip install codespell
    - run: codespell -S './.git*,./src/img*,./go.sum,./yarn.lock' -L que --ignore-words=./.codespell_ignore

workflows:
  version: 2
  build-master:
    jobs:
      - build:
          filters: *filter-only-master
      - codespell:
          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
      - codespell:
          filters: *filter-not-release-or-master
      - lint:
          filters: *filter-not-release-or-master
      - test:
          filters: *filter-not-release-or-master

  build-release:
    jobs:
      - build:
          filters: *filter-only-release
      - codespell:
          filters: *filter-only-release
      - lint:
          filters: *filter-only-release
      - test:
          filters: *filter-only-release
      - get-plugin:
          requires:
            - build
            - codespell
            - lint
            - test
          filters: *filter-only-release
      - package-plugin:
          requires:
            - get-plugin
          filters: *filter-only-release
      - make-github-release:
          requires:
            - package-plugin
          filters: *filter-only-release

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