version: 2
workflows:
  version: 2
  tests:
    jobs: &workflow_jobs
      - node6:
          filters: &all_commits
            tags:
              only: /.*/
      - node8:
          filters: *all_commits
      - node10:
          filters: *all_commits
      - lint:
          requires:
            - node6
            - node8
            - node10
          filters: *all_commits
      - docs:
          requires:
            - node6
            - node8
            - node10
          filters: *all_commits
      - system_tests:
          requires:
            - lint
            - docs
          filters: &master_and_releases
            branches:
              only: master
            tags: &releases
              only: '/^v[\d.]+$/'
      - sample_tests:
          requires:
            - lint
            - docs
          filters: *master_and_releases
      - publish_npm:
          requires:
            - system_tests
            - sample_tests
          filters:
            branches:
              ignore: /.*/
            tags: *releases
  nightly:
    triggers:
      - schedule:
          cron: 0 7 * * *
          filters:
            branches:
              only: master
    jobs: *workflow_jobs
jobs:
  node6:
    docker:
      - image: 'node:6'
        user: node
    steps: &unit_tests_steps
      - checkout
      - run: &npm_install_and_link
          name: Install and link the module
          command: |-
            mkdir -p /home/node/.npm-global
            npm install
          environment:
            NPM_CONFIG_PREFIX: /home/node/.npm-global
      - run: npm test
      - run: node_modules/.bin/codecov

  node8:
    docker:
      - image: 'node:8'
        user: node
    steps: *unit_tests_steps
  node10:
    docker:
      - image: 'node:10'
        user: node
    steps: *unit_tests_steps
  lint:
    docker:
      - image: 'node:8'
        user: node
    steps:
      - checkout
      - run: *npm_install_and_link
      - run: &samples_npm_install_and_link
          name: Link the module being tested to the samples.
          command: |
            cd samples/
            npm link ../
            npm install
          environment:
            NPM_CONFIG_PREFIX: /home/node/.npm-global
      - run:
          name: Run linting.
          command: npm run lint
          environment:
            NPM_CONFIG_PREFIX: /home/node/.npm-global
  docs:
    docker:
      - image: 'node:8'
        user: node
    steps:
      - checkout
      - run: *npm_install_and_link
      - run: npm run docs
  sample_tests:
    docker:
      - image: 'node:8'
        user: node
    steps:
      - checkout
      - run:
          name: Decrypt credentials.
          command: |
            if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
              openssl aes-256-cbc -d -in .circleci/key.json.enc \
                -out .circleci/key.json \
                -k "${SYSTEM_TESTS_ENCRYPTION_KEY}"
            fi
      - run: *npm_install_and_link
      - run: *samples_npm_install_and_link
      - run:
          name: Run sample tests.
          command: npm run samples-test
          environment:
            GCLOUD_PROJECT: long-door-651
            GOOGLE_APPLICATION_CREDENTIALS: /home/node/samples/.circleci/key.json
            NPM_CONFIG_PREFIX: /home/node/.npm-global
      - run:
          name: Remove unencrypted key.
          command: |
            if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
              rm .circleci/key.json
            fi
          when: always
    working_directory: /home/node/samples/
  system_tests:
    docker:
      - image: 'node:8'
        user: node
    steps:
      - checkout
      - run:
          name: Decrypt credentials.
          command: |
            if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
              openssl aes-256-cbc -d -in .circleci/key.json.enc \
                -out .circleci/key.json \
                -k "${SYSTEM_TESTS_ENCRYPTION_KEY}"
            fi
      - run: *npm_install_and_link
      - run:
          name: Run system tests.
          command: npm run system-test
          environment:
            GOOGLE_APPLICATION_CREDENTIALS: .circleci/key.json
      - run:
          name: Remove unencrypted key.
          command: |
            if ! [[ -z "${SYSTEM_TESTS_ENCRYPTION_KEY}" ]]; then
              rm .circleci/key.json
            fi
          when: always
  publish_npm:
    docker:
      - image: 'node:8'
        user: node
    steps:
      - checkout
      - run: npm install
      - run: echo "//registry.npmjs.org/:_authToken=${NPM_TOKEN}" > ~/.npmrc
      - run: npm publish --access=public
