aiao-io/aiao

View on GitHub
.circleci/config.yml

Summary

Maintainability
Test Coverage
# Javascript Node CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-javascript/ for more details
#
version: 2.1

orbs:
  heroku: circleci/heroku@1.2.6

defaults: &defaults
  working_directory: ~/repo
  resource_class: small
  docker:
    - image: circleci/node:16-browsers
      environment:
        NODE_OPTIONS: --max_old_space_size=2048

withDatabase: &withDatabase
  working_directory: ~/repo
  resource_class: medium+
  docker:
    - image: circleci/node:16-browsers
      environment:
        NODE_OPTIONS: --max_old_space_size=6000
    - image: circleci/postgres:12-alpine-postgis
      environment:
        POSTGRES_USER: test
        POSTGRES_PASSWORD: test
        POSTGRES_DB: test

set_env: &set_env
  name: Setup Environment Variables
  command: |
    env
    if [[ $CIRCLE_PULL_REQUEST ]]
    then
      echo 'Fetching Base Commit from GitHub'
      echo 'export CIRCLE_PR_NUMBER="${CIRCLE_PR_NUMBER:-${CIRCLE_PULL_REQUEST##*/}}"' >> $BASH_ENV
      source $BASH_ENV
      echo "export CIRCLE_PR_BASE_SHA=`curl -s https://api.github.com/repos/${CIRCLE_PROJECT_USERNAME}/${CIRCLE_PROJECT_REPONAME}/pulls/${CIRCLE_PR_NUMBER} | jq -r '.base.sha'`" >> $BASH_ENV
      echo 'export AFFECTED_ARGS="--base ${CIRCLE_PR_BASE_SHA}"' >> $BASH_ENV
    else
      echo 'Fetching Base Commit from Deploy Cache'
      if [[ ! -f dist/last-deploy.txt ]]
      then
        mkdir -p dist && git rev-list --max-parents=0 HEAD > dist/last-deploy.txt
      fi
      echo 'export AFFECTED_ARGS="--base $(cat dist/last-deploy.txt)"' >> $BASH_ENV
    fi
    source $BASH_ENV
    echo $AFFECTED_ARGS
yarn_cache: &yarn_cache
  name: Restore yarn cache
  keys:
    - last-yarn-{{ checksum "yarn.lock" }}
    - last-yarn-

save_yarn_cache: &save_yarn_cache
  name: Save yarn cache
  key: last-yarn-{{ checksum "yarn.lock" }}
  paths:
    - node_modules

dist_cache: &dist_cache
  name: Restore dist cache
  keys:
    - last-dist-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - last-dist-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}
    - last-dist-{{ checksum "yarn.lock" }}
    - last-dist-

save_dist_cache: &save_dist_cache
  name: Save dist cache
  key: last-dist-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
  paths:
    - dist/tools
    - dist/libs/elements
    - dist/libs/elements-cdk
    - dist/libs/elements-vue
    - dist/libs/image-storage
    - dist/libs/stencil-toolkit
    - dist/libs/url
    - dist/libs/util

deploy_cache: &deploy_cache
  name: Restore deploy cache
  keys:
    - last-deploy-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - last-deploy-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}
    - last-deploy-{{ checksum "yarn.lock" }}

save_deploy_cache: &save_deploy_cache
  name: Save deploy cache
  key: last-deploy-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
  paths:
    - dist/last-deploy.txt

build_cache: &build_cache
  name: Restore build cache
  keys:
    - last-build-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - last-build-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}
    - last-build-{{ checksum "yarn.lock" }}

save_build_cache: &save_build_cache
  name: Save build cache
  key: last-build-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
  paths:
    - dist/apps
    - dist/libs

test_cache: &test_cache
  name: Restore test cache
  keys:
    - last-test-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - last-test-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}
    - last-test-{{ checksum "yarn.lock" }}

save_test_cache: &save_test_cache
  name: Save test cache
  key: last-test-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
  paths:
    - coverage

e2e_cache: &e2e_cache
  name: Restore e2e cache
  keys:
    - last-e2e-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
    - last-e2e-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}
    - last-e2e-{{ checksum "yarn.lock" }}

save_e2e_cache: &save_e2e_cache
  name: Save e2e cache
  key: last-e2e-{{ checksum "yarn.lock" }}-{{ .Environment.CIRCLE_BRANCH }}-{{ .Environment.CIRCLE_WORKFLOW_ID }}
  paths:
    - dist/cypress

# install
yarn_install: &yarn_install
  name: Install dependencies
  command: yarn install --frozen-lockfile --non-interactive

# coverage
send_coverage: &send_coverage
  name: coverage
  command: bash <(curl -s https://codecov.io/bash)

# format
format_check: &format_check
  name: Format check
  command: yarn format:check ${AFFECTED_ARGS}

# lint
lint: &lint
  name: Lint
  command: ./node_modules/.bin/nx workspace-lint && yarn affected:lint ${AFFECTED_ARGS} --maxParallel=3

affected_test: &affected_test
  name: Afected test
  command: yarn affected:test ${AFFECTED_ARGS} --ci --code-coverage --maxParallel=3 --exclude dev-elements-react

affected_build: &affected_build
  name: Affected build
  command: yarn affected:build ${AFFECTED_ARGS} --configuration production --maxParallel=3

# e2e
affected_e2e: &affected_e2e
  name: Affected e2e
  command: yarn affected:e2e ${AFFECTED_ARGS} --configuration production --maxParallel=3

update_deploy: &update_deploy
  name: Update Deploy
  command: |
    echo $AFFECTED_ARGS
    cat dist/last-deploy.txt
    git rev-parse HEAD
    git rev-parse HEAD > dist/last-deploy.txt
    cat dist/last-deploy.txt
commands:
  checkout_and_cache:
    description: Checkout code and restore cache
    steps:
      - checkout
      - restore_cache: *yarn_cache
      - restore_cache: *dist_cache
      - restore_cache: *deploy_cache
      - run: *set_env
  codecov_upload:
    steps:
      - run: *send_coverage
  deplay_to_heroku:
    parameters:
      project-name:
        type: string
        default: ${CIRCLE_PROJECT_REPONAME}
      branch-name:
        type: string
        default: << pipeline.git.branch >>
      pipeline-number:
        type: integer
        default: 0
    steps:
      - heroku/install
      - run: npm run build:ssr -- --prod
      - heroku/check-authentication:
          print-whoami: true
      - run: |
          git config --global user.email "hero63418@gmail.com"
          git config --global user.name "Jimmy"
      - run: bash .circleci/heroku-setup.sh
      - run: node dist/tools/deploy/heroku.js --project-name << parameters.project-name >> --branch-name << parameters.branch-name >> --pipeline-number << parameters.pipeline-number >>

jobs:
  install:
    <<: *defaults
    steps:
      - checkout
      - run: env
      - restore_cache: *yarn_cache
      - restore_cache: *dist_cache
      - run: *yarn_install
      # - run: rm -rf node_modules/.cache
      - save_cache: *save_yarn_cache
      - save_cache: *save_dist_cache
  check-formatting:
    <<: *defaults
    resource_class: medium+
    steps:
      - checkout_and_cache
      - run: *format_check
  lint:
    <<: *defaults
    resource_class: medium+
    steps:
      - checkout_and_cache
      - run: *lint
  build:
    <<: *defaults
    resource_class: medium+
    steps:
      - checkout_and_cache
      - restore_cache: *build_cache
      - run: *affected_build
      - save_cache: *save_build_cache

  test:
    <<: *withDatabase
    steps:
      - checkout_and_cache
      - restore_cache: *test_cache
      - run: *affected_test
      - save_cache: *save_test_cache
      - codecov_upload
  e2e:
    <<: *defaults
    resource_class: medium+
    steps:
      - checkout_and_cache
      - run: yarn cypress install
      - restore_cache: *e2e_cache
      - run: *affected_e2e
      - save_cache: *save_e2e_cache

  deploy:
    <<: *defaults
    steps:
      - checkout_and_cache
      # - run: echo '//registry.npmjs.org/:_authToken=${NPM_TOKEN}' > /home/circleci/repo/.npmrc
      # - deplay_to_heroku
      - run: *update_deploy
      - save_cache: *save_deploy_cache

  check_pull_request:
    <<: *withDatabase
    steps:
      - checkout_and_cache
      - run: env
      # INSTALL
      # - run: rm -rf node_modules
      - run: *yarn_install
      - save_cache: *save_yarn_cache
      - save_cache: *save_dist_cache
      # CHECK
      - run: *format_check
      - run: *lint
      # TEST
      - restore_cache: *test_cache
      - run: *affected_test
      - codecov_upload
      - save_cache: *save_test_cache
      # BUILD
      # - restore_cache: *build_cache
      # - run: *affected_build
      # - save_cache: *save_build_cache
      # E2E
      # - restore_cache: *e2e_cache
      # - run: *affected_e2e
      # - save_cache: *save_e2e_cache
      # deplay
      # - deplay_to_heroku:
      #     pipeline-number: << pipeline.number >>
      # - save_cache: *save_deploy_cache

workflows:
  version: 2
  check_pull_request:
    when:
      and:
        - not:
            matches:
              pattern: '^master$'
              value: << pipeline.git.branch >>
    jobs:
      - check_pull_request

  check_and_deploy:
    when:
      and:
        - equal: [master, << pipeline.git.branch >>]
    jobs:
      - install
      - lint:
          requires:
            - install
      - test:
          requires:
            - install
      - check-formatting:
          requires:
            - install
      - build:
          requires:
            - install
      - e2e:
          requires:
            - install
      - deploy:
          filters:
            branches:
              only:
                - master
          requires:
            - check-formatting
            - lint
            - test
            - build
            - e2e