bikeindex/bike_index

View on GitHub
.circleci/config.yml

Summary

Maintainability
Test Coverage
# To bump cache:
# Increment & Replace: 'CACHE_V4' in this config

# NB: It would be great to use Pipeline parameters instead of search and replacing the cache key
# ... but that doesn't work with this config version, and workflows don't work with version 2.1
# And circleci's documentation bounces between the two versions and everything is a pain.
# See #2257

version: 2

jobs:
  build:
    working_directory: ~/bikeindex/bike_index
    parallelism: 2
    shell: /bin/bash --login
    environment:
      RAILS_ENV: test
      NODE_ENV: test
      RACK_ENV: test
      SKIP_MEMOIZE_MANUFACTURER_OTHER: true
      TRANSLATION_BRANCH: main
      TZ: /usr/share/zoneinfo/America/Chicago
      RETRY_FLAKY: true

    docker:
      - image: cimg/ruby:2.7.8-node
        environment:
          PGHOST: 127.0.0.1
          PGUSER: root
          PSQL_PAGER: ""
      - image: cimg/postgres:13.5-postgis
        environment:
          POSTGRES_USER: root
          POSTGRES_DB: bikeindex_test
          POSTGRES_PASSWORD: ""
      - image: redis:7.2.3

    # The resource_class feature allows configuring CPU and RAM resources for each job. Different resource classes are available for different executors. https://circleci.com/docs/2.0/configuration-reference/#resourceclass
    resource_class: large

    steps:
      - add_ssh_keys:
          fingerprints:
            - "b7:01:89:de:d8:f8:77:cc:9e:5a:ca:ee:0c:24:57:13"

      - checkout

      - run:
          name: "Apt: install system / build dependencies"
          command: |
            sudo apt-get update
            sudo apt-get -y install \
              curl \
              gettext \
              imagemagick \
              libcurl4-gnutls-dev \
              libexpat1-dev \
              libssl-dev \
              libz-dev \
              postgresql-client \
              ripgrep

      - run:
          name: "Update Node.js and npm"
          command: |
            curl -sSL "https://nodejs.org/download/release/v16.20.1/node-v16.20.1-linux-x64.tar.xz" | sudo tar --strip-components=2 -xJ -C /usr/local/bin/ node-v16.20.1-linux-x64/bin/node
            # curl https://www.npmjs.com/install.sh | sudo bash

      - restore_cache:
          name: "Dockerize: cache restore"
          keys:
            - CACHE_V4-dockerize-0.6.1
            - CACHE_V4-dockerize-

      - run:
          name: "Dockerize: install"
          environment:
            DOCKERIZE_VERSION: v0.6.1
          command: |
            dockerize_tar=dockerize-linux-amd64-${DOCKERIZE_VERSION}.tar.gz
            if [[ ! -f ~/.cache/dockerize/${dockerize_tar} ]]; then
              wget https://github.com/jwilder/dockerize/releases/download/${DOCKERIZE_VERSION}/${dockerize_tar}
            fi
            sudo tar -C /usr/local/bin -xzvf ${dockerize_tar}

      - type: cache-save
        name: "Dockerize: cache save"
        key: CACHE_V4-dockerize-0.6.1
        paths:
          - ~/.cache/dockerize

      # Updated all restore_cache to make things more functional when changing a single dependency, see PR#2366
      - restore_cache:
          name: "Git: cache restore"
          keys:
            - CACHE_V4-git-2.22.0
            - CACHE_V4-git-

      - run:
          name: "Git: install"
          environment:
            GIT_VERSION: 2.22.0
          command: |
            set -x

            git_binary=~/.cache/git/git-${GIT_VERSION}/git

            if [[ ! -f $git_binary ]]; then
              rm -rf ~/.cache/git
              mkdir -p ~/.cache/git
              cd ~/.cache/git

              wget -O git.tgz https://mirrors.edge.kernel.org/pub/software/scm/git/git-${GIT_VERSION}.tar.gz
              tar -xzf git.tgz
              cd git-${GIT_VERSION}

              export DEFAULT_HELP_FORMAT="man"
              autoconf
              ./configure

              sudo make man
              sudo make install-man
            fi

            cd ~/.cache/git/git-${GIT_VERSION}
            sudo make prefix=/usr/local all
            sudo make install
            sudo cp ${git_binary} /usr/local/bin/git

      - type: cache-save
        name: "Git: cache save"
        key: CACHE_V4-git-2.22.0
        paths:
          - ~/.cache/git/git-2.22.0

      - restore_cache:
          name: "Hub: cache restore"
          keys:
            - CACHE_V4-hub-2.12.3
            - CACHE_V4-hub-

      - run:
          name: "Hub: install"
          environment:
            HUB_VERSION: 2.12.3
          command: |
            set -x

            hub_binary=~/.cache/hub/hub-linux-amd64-${HUB_VERSION}/bin/hub

            if [[ ! -f ${hub_binary} ]]; then
              rm -rf ~/.cache/hub
              mkdir -p ~/.cache/hub
              cd ~/.cache/hub

              wget -O hub.tgz https://github.com/github/hub/releases/download/v${HUB_VERSION}/hub-linux-amd64-${HUB_VERSION}.tgz
              tar -xzf hub.tgz
            fi

            sudo cp ${hub_binary} /usr/local/bin/hub

      - restore_cache:
          name: "Hub: cache save"
          keys:
            - CACHE_V4-hub-2.12.3
            - CACHE_V4-hub-
          paths:
            - ~/.cache/hub

      - run:
          name: "Bundler: install"
          command: gem install bundler -v 2.4.22

      - restore_cache:
          name: "Ruby dependencies: cache restore"
          keys:
            - CACHE_V4-gems-{{ checksum "Gemfile.lock" }}
            - CACHE_V4-gems-

      - run:
          name: "Ruby dependencies: install"
          command: |
            set -x
            bundle config set --local path '~/.cache/bundle'
            bundle install

      - type: cache-save
        name: "Ruby dependencies: cache save"
        key: CACHE_V4-gems-{{ checksum "Gemfile.lock" }}
        paths:
          - ~/.cache/bundle

      - run:
          name: Sync translations (only on main by default)
          command: bin/check_translations

      - restore_cache:
          name: "Node dependencies: cache restore"
          keys:
            - CACHE_V4-yarn-{{ checksum "yarn.lock" }}
            - CACHE_V4-yarn-

      - run:
          name: "Node dependencies: install"
          command: |
            set -x
            [[ -d ~/.cache/yarn ]] && use_local="--prefer-offline"
            yarn install --cache-folder ~/.cache/yarn $use_local

      - type: cache-save
        name: "Node dependencies: cache save"
        key: CACHE_V4-yarn-{{ checksum "yarn.lock" }}
        paths:
          - ~/.cache/yarn

      - restore_cache:
          name: "Assets: cache restore"
          keys:
            - CACHE_V4-assets-{{ .Environment.CIRCLE_SHA1 }}
            - CACHE_V4-assets-

      - run:
          name: "Assets: precompile"
          command: |
            set -x
            node --version
            bin/webpack
            bundle exec rails assets:precompile assets:clean --trace

      - type: cache-save
        name: "Assets: cache save"
        key: CACHE_V4-assets-{{ .Environment.CIRCLE_SHA1 }}
        paths:
          - .sass-cache
          - public/assets
          - public/packs
          - public/packs-test
          - tmp/cache/assets
          - tmp/cache/webpacker

      - run:
          name: Wait for PostgreSQL to start
          command: dockerize -wait tcp://localhost:5432 -timeout 1m

      - run:
          name: Setup Database
          command: |
            bundle exec rails db:create --trace
            bundle exec rails db:structure:load --trace

      - run:
          name: RSpec
          command: |
            mkdir -p test-results/rspec
            RSPEC_CMD="bundle exec rspec --profile 10 --order random --format RspecJunitFormatter --out test-results/rspec/rspec.xml --format progress --color --require spec_helper"
            circleci tests glob "spec/**/*_spec.rb" | circleci tests run --command="xargs $RSPEC_CMD" --verbose --split-by=timings
      - run:
          name: "Jest: Tests"
          command: |
            mkdir -p test-results/junit
            yarn jest --ci --runInBand --reporters=default --reporters=jest-junit
          environment:
            JEST_JUNIT_OUTPUT: "test-results/junit/js-test-results.xml" # This is the env variable for the old version of jest-junit
            JEST_JUNIT_OUTPUT_DIR: "test-results/junit/"

      - store_test_results:
          path: test-results

  deploy_production:
    machine:
      enabled: true
    working_directory: ~/bikeindex/bike_index
    steps:
      - checkout
      - run:
          name: Deploying to Cloud66 production
          command: |
            curl --insecure -X POST -d "" https://hooks.cloud66.com/stacks/redeploy/${CLOUD66_REDEPLOYMENT_PATH}

workflows:
  version: 2
  commit:
    jobs:
      - build
      - deploy_production:
          requires:
            - build
          filters:
            branches:
              only: main