thiagoarrais/grafana-matomo-tracking-panel

View on GitHub
.circleci/config.yml

Summary

Maintainability
Test Coverage
version: 2

aliases:
  # Workflow filters
  - &filter-only-master
    branches:
      only: master

defaults: &defaults
  working_directory: ~/plugin
  docker:
    - image: circleci/node:14

jobs:
  build_plugin:
    <<: *defaults
    steps:
    - checkout
    - restore_cache:
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
    - run:
        name: Install yarn
        command: yarn install --pure-lockfile
    - run:
        name: Run Toolkit Build
        command: npx grafana-toolkit plugin:ci-build && cp INSTALL.md dist/README.md
    - run:
        name: Sign Plugin
        command: npx grafana-toolkit plugin:sign
    - save_cache:
        paths:
        - node_modules
        key: yarn-packages-{{ checksum "yarn.lock" }}
    - persist_to_workspace:
        root: .
        paths:
        - dist
        - coverage
  build_docs:
    <<: *defaults
    steps:
    - checkout
    - restore_cache:
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
    - run:
        name: Install yarn
        command: |
          yarn install --pure-lockfile
          mkdir ci # Avoid error if not exists
    - run:
        name: Build Docs
        command: npx grafana-toolkit plugin:ci-docs
    - save_cache:
        paths:
        - node_modules
        key: yarn-packages-{{ checksum "yarn.lock" }}
    - persist_to_workspace:
        root: .
        paths:
        - ci
  code_coverage:
    <<: *defaults
    steps:
    - checkout
    - attach_workspace:
        at: .
    - restore_cache:
        keys:
          - yarn-packages-{{ checksum "yarn.lock" }}
    - run:
        name:  Download cc-test-reporter
        command: |
          mkdir -p tmp/
          curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > ./tmp/cc-test-reporter
          chmod +x ./tmp/cc-test-reporter
    - run:
        name: Run coverage report
        command: |
          ls -l coverage
          ./tmp/cc-test-reporter format-coverage -t lcov -o tmp/codeclimate.frontend.json coverage/lcov.info
    - run:
        name: Install jest and jest-junit
        command: sudo yarn global add jest jest-junit
    - run:
        name: Run tests with JUnit as reporter
        command: jest --ci --runInBand --reporters=default --reporters=jest-junit
        environment:
          JEST_JUNIT_OUTPUT_DIR: "test-results/jest/results.xml"
    - persist_to_workspace:
        root: .
        paths:
        - tmp
    - store_test_results:
        path: test-results
  upload_coverage:
    docker:
    - image:  circleci/circleci-cli:alpine
    #  environment:
    #    CC_TEST_REPORTER_ID: add_this_to_circleci_config
    working_directory: ~/plugin
    steps:
      - attach_workspace:
          at: .
      - run:
          name: Upload coverage results to Code Climate
          command: |
            ./tmp/cc-test-reporter sum-coverage tmp/codeclimate.frontend.json -d -p 1 -o tmp/codeclimate.total.json
            ./tmp/cc-test-reporter upload-coverage -i tmp/codeclimate.total.json
  package:
    <<: *defaults
    steps:
    - checkout
    - attach_workspace:
        at: .
    - restore_cache:
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
    - run:
        name: Package Distribution
        command: npx grafana-toolkit plugin:ci-package
    - persist_to_workspace:
        root: .
        paths:
        - ci/jobs/package
        - ci/packages
        - ci/dist
        - ci/grafana-test-env
  test_integration:
    <<: *defaults
    docker:
    - image: circleci/node:12-browsers
    steps:
    - checkout
    - attach_workspace:
        at: .
    - restore_cache:
        keys:
        - yarn-packages-{{ checksum "yarn.lock" }}
    - run:
        name: Setup Grafana (local install)
        command: |
          wget https://dl.grafana.com/oss/release/grafana_6.6.1_amd64.deb
          sudo apt-get install -y adduser libfontconfig1
          sudo dpkg -i grafana_6.6.1_amd64.deb
          sudo apt-get install locate
          sudo updatedb
          sudo locate grafana
          sudo cat /etc/grafana/grafana.ini
          sudo echo ------------------------
          sudo cp ci/grafana-test-env/custom.ini /usr/share/grafana/conf/custom.ini
          sudo cp ci/grafana-test-env/custom.ini /etc/grafana/grafana.ini
          sudo service grafana-server start
          sudo grafana-cli --version
    - run:
        name: Run e2e tests
        command: |
          npx grafana-toolkit plugin:test
    - persist_to_workspace:
        root: .
        paths:
        - coverage
    - store_test_results:
        path: coverage
    - store_artifacts:
        path: coverage

  publish_github_release:
    working_directory: ~/simple-react-panel
    docker:
      - image: cibuilds/github:0.13
    steps:
      - checkout
      - add_ssh_keys:
          fingerprints:
            - "98:1f:d9:20:f8:bc:c0:54:1f:40:e8:95:ed:bb:8a:cb"
      - attach_workspace:
          at: .
      - run:
          name: "Publish Release on GitHub"
          command: |
            # copy ci artifacts
            mkdir -p artifacts
            cp -r ci/packages/* artifacts
            apk add --update --no-cache jq
            PLUGIN_NAME=`cat dist/plugin.json|jq '.id'| sed s/\"//g`
            RELEASE_NOTES=`awk 'BEGIN {FS="##"; RS=""} FNR==4 {print; exit}' CHANGELOG.md`
            VERSION=`cat dist/plugin.json|jq '.info.version'| sed s/\"//g`
            git config user.email "eng@grafana.com"
            git config user.name "CircleCI Automation"
            git checkout -b release-${VERSION}
            git add --force dist/
            git add artifacts/
            git commit -m "automated release $VERSION [skip ci]"
            git push -f origin release-${VERSION}
            git tag -f v${VERSION}
            git push -f origin v${VERSION}
            ghr -t ${GITHUB_TOKEN} -u ${CIRCLE_PROJECT_USERNAME} -r ${CIRCLE_PROJECT_REPONAME} -c ${CIRCLE_SHA1} -n "${PLUGIN_NAME} v${VERSION}" -b "${RELEASE_NOTES}" -delete v${VERSION} ./artifacts/

workflows:
  version: 2
  plugin_workflow:
    jobs:
    - build_plugin
    - build_docs
    - code_coverage:
        requires:
        - build_plugin
    - upload_coverage:
        requires:
        - code_coverage
    - package:
        requires:
        - build_plugin
        - build_docs
        - upload_coverage
    - test_integration:
        requires:
        - package
    - approve_release:
        type: approval
        requires:
          - test_integration
        filters: *filter-only-master
    - publish_github_release:
        requires:
          - approve_release
        filters: *filter-only-master