version: 2.1

aliases:
  - &mktestdir
    run:
      name: Create results directory
      command: mkdir -p ~/test-results

  - &storetestdir
    store_test_results:
      path: ~/test-results

orbs:
  aws-cli: circleci/aws-cli@0.1.13
  go: circleci/go@0.2.0

commands:
  getmods:
    steps:
      - go/load-cache
      - go/mod-download
      - go/save-cache

jobs:
  lint:
    docker:
    - image: golangci/golangci-lint
    steps:
      - checkout
      - *mktestdir
      - run:
          name: Run linters
          command: golangci-lint run --out-format junit-xml > ~/test-results/lint.xml
      - *storetestdir
  unit:
    executor: go/default
    steps:
    - checkout
    - *mktestdir
    - getmods
    - run:
        name: Dialect tests
        command: gotestsum -f short-verbose --junitfile ~/test-results/dialect.xml
        working_directory: dialect
    - run:
        name: Schema tests
        command: gotestsum -f short-verbose --junitfile ~/test-results/schema.xml
        working_directory: schema
    - run:
        name: Loader tests
        command: gotestsum -f short-verbose --junitfile ~/test-results/load.xml
        working_directory: entc/load
    - run:
        name: Codegen tests
        command: gotestsum -f short-verbose --junitfile ~/test-results/gen.xml
        working_directory: entc/gen
    - *storetestdir

  integration:
    docker:
      - image: circleci/golang
      - image: circleci/mysql:5.6.35
        environment: &mysql_env
          MYSQL_DATABASE: test
          MYSQL_ROOT_PASSWORD: pass
      - image: circleci/mysql:5.7.26
        environment:
          <<: *mysql_env
          MYSQL_TCP_PORT: 3307
      - image: circleci/mysql:8
        environment:
          <<: *mysql_env
          MYSQL_TCP_PORT: 3308
      - image: circleci/postgres:10.0
        environment:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        command: -p 5430
      - image: circleci/postgres:11.0
        environment:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        command: -p 5431
      - image: circleci/postgres:12.1
        environment:
          POSTGRES_DB: test
          POSTGRES_PASSWORD: pass
        command: -p 5432
      - image: entgo/gremlin-server
        entrypoint: /opt/gremlin-server/bin/gremlin-server.sh
        command: conf/gremlin-server.yaml
    steps:
      - checkout
      - run:
          name: Wait for databases
          command: >-
            dockerize -timeout 1m
            -wait tcp://localhost:3306
            -wait tcp://localhost:3307
            -wait tcp://localhost:3308
            -wait tcp://localhost:5430
            -wait tcp://localhost:5431
            -wait tcp://localhost:5432
            -wait tcp://localhost:8182
      - *mktestdir
      - getmods
      - run:
          name: Run codegen for entc/load
          working_directory: entc/load
          command: go generate
      - run:
          name: Run codegen for entc/gen
          working_directory: entc/gen
          command: go generate
      - run:
          name: Run codegen for entc/integration
          working_directory: entc/integration
          command: go generate
      - run:
          name: Run integration tests
          working_directory: entc/integration
          command: gotestsum -f short-verbose --junitfile ~/test-results/integration.xml -- -race ./...
      - *storetestdir

  docs:
    docker:
    - image: circleci/node
    steps:
    - checkout
    - run:
        name: Checking Docs Modified
        command: |
          if [[ ! $(git diff master^ --name-only doc/) ]]; then
            echo "docs not modified; no need to deploy"
            circleci step halt
          fi
    - run:
        name: Install Dependencies
        working_directory: ~/project/doc/website
        command: yarn
    - run:
        name: Build Docs
        working_directory: ~/project/doc/website
        command: yarn build
    - aws-cli/install
    - aws-cli/configure
    - run:
        name: Deploy Docs
        working_directory: ~/project/doc/website/build/ent
        command: aws s3 sync . s3://entgo.io --delete --exclude "assets/*"
    - run:
        name: Invalidate Cache
        command: aws cloudfront create-invalidation --distribution-id $CDN_DISTRIBUTION_ID --paths "/*" | jq -M "del(.Location)"

workflows:
  version: 2.1
  all:
    jobs:
    - lint
    - unit
    - integration
    - docs:
        filters:
          branches:
            only: master
