SumOfUs/Champaign

View on GitHub
.circleci/config.yml

Summary

Maintainability
Test Coverage
version: 2

defaults: &defaults
  working_directory: ~/circleci-champaign
  resource_class: large
  docker:
    - image: soutech/ruby:2.6-stretch-node-awscli

jobs:
  install_dependencies:
    <<: *defaults
    steps:
      - checkout
      - restore_cache:
          name: Restoring bundler cache
          key: gems-{{ checksum "Gemfile.lock" }}
      - restore_cache:
          name: Restoring node_modules cache
          key: node-{{ checksum "yarn.lock" }}
      - run:
          name: Install gems
          command: bundle install --path vendor/bundle
      - run:
          name: Install node_modules with Yarn
          command: yarn --frozen-lockfile
      - save_cache:
          key: gems-{{ checksum "Gemfile.lock" }}
          paths:
          - vendor/bundle
      - save_cache:
          key: node-{{ checksum "yarn.lock" }}
          paths:
          - node_modules
      - persist_to_workspace:
          root: .
          paths:
            - vendor/bundle
            - node_modules
            - .bundle
  # flow:
  #   <<: *defaults
  #   steps:
  #     - checkout
  #     - attach_workspace:
  #         at: ~/circleci-champaign
  #     - run:
  #         name: Flow check
  #         command: yarn run flow check
  jest:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Run JS tests
          command: yarn run test

  precompile_assets:
    <<: *defaults
    environment:
      RAILS_ENV: production
      NODE_ENV: production
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run: bundle install --path vendor/bundle
      - run:
          name: Precompile assets
          # NOTE: Webpacker runs yarn on asset precompilation so this will install production assets into node_modules!
          command: bundle exec rake assets:precompile
      - persist_to_workspace:
          root: .
          paths:
            - public

  precompile_production_assets:
    # TODO: change epoch in cache key to git commit sha of the last commit in the sou-assets repo
    # (Branch is either current branch, $DEFAULT_ASSET_BRANCH, or 'master', in that order):
    # git ls-remote git://github.com/SumOfUs/sou-assets.git \ grep refs/heads/master | cut -f 1
    <<: *defaults
    environment:
      RAILS_ENV: production
      NODE_ENV: production
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run: bundle install --path vendor/bundle
      - run:
          name: Precompile assets
          # NOTE: Webpacker runs yarn on asset precompilation so this will install production assets into node_modules!
          command: bundle exec rake assets:precompile
      - persist_to_workspace:
          root: .
          paths:
            - public

  rspec:
    working_directory: ~/circleci-champaign
    resource_class: large
    docker:
      - image: soutech/ruby:2.6-stretch-node-awscli
        environment:
          PG_USERNAME: champaign
          PG_PASSWORD: champaign
          RAILS_ENV: test
      - image: circleci/postgres:9.4-alpine-ram
        environment:
          POSTGRES_USER: champaign
          POSTGRES_DB: champaign
      - image: redis
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Bundler
          command: bundle --path vendor/bundle
      - run:
          name: Migrations and seeding
          command: bundle exec rake db:create db:migrate db:seed champaign:seed_liquid
      - run:
          name: Precompile assets
          command: RAILS_ENV=test bundle exec rails webpacker:compile
      - run:
          name: Run specs
          command: bundle exec rspec spec
      - store_artifacts:
          path: coverage

  build_docker_image:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - setup_remote_docker
      - run:
          name: Docker build
          command: .circleci/build.sh

  build_production_docker_image:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - setup_remote_docker
      - run:
          name: Docker build
          command: .circleci/build.sh

  deploy_production:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Deploy production
          command: .circleci/deploy.sh

  deploy_staging:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Deploy staging
          command: .circleci/deploy.sh

  deploy_feature:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Deploy feature branch
          command: .circleci/deploy_testing.sh

  airbrake_register_deploy:
    <<: *defaults
    steps:
      - checkout
      - attach_workspace:
          at: ~/circleci-champaign
      - run:
          name: Deploy feature branch
          command: .circleci/airbrake_register_deploy.sh

workflows:
  version: 2
  build_and_deploy:
    jobs:
      - install_dependencies

      # - flow:
      #     requires:
      #       - install_dependencies

      - jest:
          requires:
            - install_dependencies

      - rspec:
          requires:
            - install_dependencies

      - precompile_production_assets:
          requires:
            - install_dependencies
          context: champaign-production
          # Only precompile assets when we need to build and deploy
          filters:
            branches:
              only:
                - master

      - precompile_assets:
          requires:
            - install_dependencies
          context: champaign-staging
          # Only precompile assets when we need to build and deploy
          filters:
            branches:
              only:
                - development
                - /^feature(?:\/|-|\.).+$/

      - build_docker_image:
          requires:
            - precompile_assets
          filters:
            branches:
              only:
                - master
                - development
                - /^feature(?:\/|-|\.).+$/

      - build_production_docker_image:
          requires:
            - precompile_production_assets
          filters:
            branches:
              only:
                - master

      - deploy_production:
          requires:
            - build_production_docker_image
            - rspec
            - jest
            # - flow
          context: champaign-production
          filters:
            branches:
              only:
                - master

      - deploy_staging:
          requires:
            - build_docker_image
            - rspec
            - jest
            # - flow
          context: champaign-staging
          filters:
            branches:
              only:
                - development

      - deploy_feature:
          requires:
            - build_docker_image
            - rspec
            - jest
            # - flow
          context: champaign-testing
          # match any branches that begin with feature- or feature/
          #   feature-my-feature
          #   feature/my-feature
          #   feature.my-feature
          filters:
            branches:
              only:
                - /^feature(?:\/|-|\.).+$/

      - airbrake_register_deploy:
          requires:
            - deploy_production
          context: champaign-production
          filters:
            branches:
              only:
                - master