version: 2
reference:
  environment: &ENVIRONMENT
    SLACK_TITLE: Vagrant CI
    RELEASE_TARGET_REPONAME: vagrant-installers
  images:
    ruby23: &ruby23
      docker:
        - image: circleci/ruby:2.3
    ruby24: &ruby24
      docker:
        - image: circleci/ruby:2.4
    ruby25: &ruby25
      docker:
        - image: circleci/ruby:2.5
    ruby26: &ruby26
      docker:
        - image: circleci/ruby:2.6
    builder: &builder
      environment:
        <<: *ENVIRONMENT
      docker:
        - image: $BUILDER_IMAGE
          auth:
            username: $BUILDER_USERNAME
            password: $BUILDER_PASSWORD
  workflows:
    public: &PUBLIC_WORKFLOW
      filters:
        branches:
          only: /^pull\/.*/
    master: &MASTER_WORKFLOW
      filters:
        branches:
          only: master
    private_build: &PRIVATE_WORKFLOW_BUILD
      context: vagrant
      filters:
        branches:
          only:
            - /^build-.*/
        tags:
          only: /.*/
  jobs:
    private_failure: &PRIVATE_FAILURE
      run:
        name: Failure handler
        command: |
          if [ -f .output ]; then
            slack -m "Vagrant job has failed: *${CIRCLE_JOB}*" -s error -f .output -T 5
          else
            slack -m "Vagrant job has failed: *${CIRCLE_JOB}*" -s error
          fi
        when: on_fail
    unit_tests: &unit_tests
      steps:
        - run: sudo apt-get update ; sudo apt-get -yq install bsdtar
        - checkout
        - restore_cache:
            key: static-site-gems-v1-{{ checksum "Gemfile.lock" }}
        - run:
            command: bundle check || bundle install --path vendor/bundle
        - save_cache:
            key: static-site-gems-v1-{{ checksum "Gemfile.lock" }}
            paths:
              - ./vendor/bundle
        - run: bundle exec rake test:unit
jobs:
  build:
    <<: *builder
    steps:
      - checkout
      - run: gem build vagrant.gemspec
      - *PRIVATE_FAILURE
      - persist_to_workspace:
          root: .
          paths:
            - ./*.gem
  store:
    <<: *builder
    steps:
      - attach_workspace:
          at: .
      - run: |
          gem_name=(vagrant-*.gem)
          if [ "${CIRCLE_TAG}" == "" ]; then
            remote_gem_name="vagrant-master.gem"
          else
            remote_gem_name="vagrant.gem"
          fi
          if [[ "${CIRCLE_BRANCH}" = "build-"* ]]; then
            s3_dst="${ASSETS_PRIVATE_LONGTERM}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/${CIRCLE_BRANCH##build-}/"
          else
            s3_dst="${ASSETS_PRIVATE_BUCKET}/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/"
          fi
          aws s3 cp "${gem_name}" "${s3_dst}${remote_gem_name}" > .output 2>&1
      - *PRIVATE_FAILURE
  release:
    <<: *builder
    steps:
      - checkout
      - attach_workspace:
          at: .
      - run: |
          set +e
          gem=(vagrant-*.gem)
          gem_version="${gem##vagrant-}"
          gem_version="${gem_version%%.gem}"
          export GITHUB_TOKEN="${HASHIBOT_TOKEN}"
          if [ "${CIRCLE_TAG}" = "" ]; then
            version="v${gem_version}+$(git rev-parse --short "${CIRCLE_SHA1}")"
            ghr -u ${CIRCLE_PROJECT_USERNAME} -r ${RELEASE_TARGET_REPONAME} -c master -prerelease -delete -replace ${version} ${gem} > .output 2>&1
          else
            version="${CIRCLE_TAG}"
            ghr -u ${CIRCLE_PROJECT_USERNAME} -r ${RELEASE_TARGET_REPONAME} -c master -delete -replace ${version} ${gem} > .output 2>&1
          fi
          slack -m "New Vagrant installers release triggered: *${version}*"
      - *PRIVATE_FAILURE
  test_ruby23:
    <<: *ruby23
    <<: *unit_tests
  test_ruby24:
    <<: *ruby24
    <<: *unit_tests
  test_ruby25:
    <<: *ruby25
    <<: *unit_tests
  test_ruby26:
    <<: *ruby26
    <<: *unit_tests
  build-website:
    # setting the working_directory along with the checkout path allows us to not have
    # to cd into the website/ directory for commands
    working_directory: ~/project/website
    docker:
      - image: hashicorp/middleman-hashicorp:0.3.35
    steps:
      - checkout:
          path: ~/project
      - restore_cache:
          key: static-site-gems-v1-{{ checksum "Gemfile.lock" }}
      - run:
          command: bundle check || bundle install --path vendor/bundle
      - save_cache:
          key: static-site-gems-v1-{{ checksum "Gemfile.lock" }}
          paths:
            - ~/project/website/vendor/bundle
      - run:
          command: bundle exec middleman build
      - run:
          command: ./scripts/deploy.sh
workflows:
  version: 2
  builds:
    jobs:
      - build:
          <<: *PRIVATE_WORKFLOW_BUILD
      - store:
          <<: *PRIVATE_WORKFLOW_BUILD
          requires:
            - build
      - release:
          <<: *PRIVATE_WORKFLOW_BUILD
          requires:
            - build
  pull_requests:
    jobs:
      - test_ruby23:
          <<: *PUBLIC_WORKFLOW
      - test_ruby24:
          <<: *PUBLIC_WORKFLOW
      - test_ruby25:
          <<: *PUBLIC_WORKFLOW
      - test_ruby26:
          <<: *PUBLIC_WORKFLOW
  master:
    jobs:
      - test_ruby23:
          <<: *MASTER_WORKFLOW
      - test_ruby24:
          <<: *MASTER_WORKFLOW
      - test_ruby25:
          <<: *MASTER_WORKFLOW
      - test_ruby26:
          <<: *MASTER_WORKFLOW
      - build:
          <<: *MASTER_WORKFLOW
          context: vagrant
          requires:
            - test_ruby23
            - test_ruby24
            - test_ruby25
            - test_ruby26
      - store:
          <<: *MASTER_WORKFLOW
          context: vagrant
          requires:
            - build
  website:
    jobs:
      - build-website:
          context: static-sites
          filters:
            branches:
              only: stable-website