# Check https://circleci.com/docs/2.0/language-javascript/ for more details
version: 2
jobs:
  run_tests:
    docker:
      - image: circleci/node:10

    working_directory: ~/repo

    steps:
      - checkout

      - run: sudo git clone https://github.com/sstephenson/bats.git ~/bats
      - run: pushd ~/bats && sudo bash install.sh /usr/local && popd

      - run: ln -s ~/repo ~/$CIRCLE_PROJECT_REPONAME

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - run: npm run commitlint-since-last-mr
      - run: npm test

workflows:
  version: 2
  test:
    jobs:
      - run_tests

# Check https://circleci.com/docs/2.0/language-javascript/ for more details
version: 2
jobs:
  run_tests:
    docker:
      - image: circleci/node:10

    working_directory: ~/repo

    steps:
      - checkout

      - run: sudo git clone https://github.com/sstephenson/bats.git ~/bats
      - run: pushd ~/bats && sudo bash install.sh /usr/local && popd

      - run: ln -s ~/repo ~/$CIRCLE_PROJECT_REPONAME

      # Download and cache dependencies
      - restore_cache:
          keys:
          - v1-dependencies-{{ checksum "package.json" }}
          # fallback to using the latest cache if no exact match is found
          - v1-dependencies-

      - run: npm install

      - save_cache:
          paths:
            - node_modules
          key: v1-dependencies-{{ checksum "package.json" }}

      - run: npm run commitlint-since-last-mr
      - run: npm test
  docker_build:
    working_directory: /app
    docker:
      - image: docker:18
    steps:
      - checkout
      - setup_remote_docker:
          docker_layer_caching: true
      - run:
          name: Build image
          command: docker build -t artilleryio/artillery:$CIRCLE_TAG .
      - run:
          name: Test image
          command: docker run --rm -it artilleryio/artillery:$CIRCLE_TAG quick -d 20 -c 10 -n 20 https://artillery.io/
      - run:
          name: Publish image
          command: |
            docker login -u $DOCKER_USER -p $DOCKER_PASS
            docker push artilleryio/artillery:$CIRCLE_TAG

workflows:
  version: 2
  test:
    jobs:
      - run_tests
  docker:
    jobs:
      - docker_build:
          requires:
            - run_tests
          filters:
            tags:
              only: /v[0-9]+(\.[0-9])+(-[a-zA-Z0-9]+)*$/
            branches:
              only: master

Build Docker images on version-tagged commits on master
ci: Build Docker images on version-tagged commits on master
