codeforthailand/election-live

View on GitHub
.circleci/config.yml

Summary

Maintainability
Test Coverage
version: 2
jobs:
  build:
    working_directory: ~/election-live
    docker:
      - image: circleci/node:10.15.3
    steps:
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run: yarn
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
            - ./node_modules
      - run: yarn build
      - persist_to_workspace:
          root: .
          paths:
            - public
  test:
    working_directory: ~/election-live
    docker:
      - image: circleci/node:10.15.3
    steps:
      - run:
          name: Install Code Climate
          command: |
            curl -L https://codeclimate.com/downloads/test-reporter/test-reporter-latest-linux-amd64 > /tmp/cc-test-reporter
            chmod +x /tmp/cc-test-reporter
            /tmp/cc-test-reporter before-build
      - checkout
      - restore_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
      - run: yarn
      - save_cache:
          key: dependency-cache-{{ checksum "yarn.lock" }}
          paths:
            - ./node_modules
      - run:
          name: test
          command: |
            yarn test --ci --coverage --reporters=default --reporters=jest-junit
      - store_artifacts:
          path: junit.xml
          prefix: tests
      - store_artifacts:
          path: coverage
          prefix: coverage
      - store_test_results:
          path: junit.xml
      - run:
          name: Code Climate
          command: |
            # Exported here to allow forked pull requests to report.
            # https://docs.codeclimate.com/docs/finding-your-test-coverage-token#section-should-i-keep-my-test-reporter-id-secret
            export CC_TEST_REPORTER_ID=288aa84aed5e2836e6e216cbdc7270562db0578317d2e08a2a8cf02d53be6857

            /tmp/cc-test-reporter after-build --coverage-input-type lcov
      # @todo #1 Setup CircleCI to obtain lighthouse report.
      #  This can be done by these steps:
      #  (1) Run `yarn build` to generate a built bundle
      #  (2) Start a temporary web server.
      #  (3) Run lighthouse CLI to obtain report. (See: https://github.com/GoogleChrome/lighthouse)
      #  (4) Store artifact in CircleCI using `store_artifacts` command.
      #
  deploy:
    working_directory: ~/election-live
    docker:
      - image: circleci/node:10.15.3
    steps:
      - add_ssh_keys:
          fingerprints:
            - "03:72:bd:d6:22:85:a5:6a:56:0d:a6:fa:d0:2f:40:a1"
      - attach_workspace:
          at: .
      - run:
          name: Update .htaccess Cache-Control max-age to 30 seconds instead of 0
          command: |
            sed -i -e 's/max-age=0/max-age=30, stale-while-revalidate=30, stale-if-error=300/g' public/.htaccess
            sed -i -e 's/max-age=31536000/max-age=31536000, stale-while-revalidate=30, stale-if-error=300/g' public/.htaccess
      - deploy:
          name: Prepare files for uploading
          command: |
            sudo apt update && sudo apt install rsync
            echo '206.189.85.88 ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBOOlv2IqgK2lE+esErYHNO9tDXjTd8/AUcYZlmFeM0awGcLkTPv0Eg308QoZVmaIWXaYfynGjOEAv4u9NsP275g=' >> ~/.ssh/known_hosts

      # Perform a 2-step deployment to make sure that all assets
      # a HTML file may need is already there by the time we sync HTML files.
      - deploy:
          name: Deploy non-HTML files
          command: |
            rsync -rvz --exclude="*.html" public/ "$DEPLOYMENT_TARGET_HOST:$DEPLOYMENT_TARGET_PATH/"
      - deploy:
          name: Deploy HTML files
          command: |
            rsync -rvz public/ "$DEPLOYMENT_TARGET_HOST:$DEPLOYMENT_TARGET_PATH/"
workflows:
  version: 2
  build-test-deploy:
    jobs:
      - build
      - test
      - deploy:
          requires:
            - build
          filters:
            branches:
              only: master