version: 2.1

executors:
  mv-exec:
    working_directory: ~/mediaview
    docker:
      - image: 497387467775.dkr.ecr.us-east-1.amazonaws.com/oz-base-test-old:0.0.1-alpha1
      - image: redis:3.2.7
      - image: 497387467775.dkr.ecr.us-east-1.amazonaws.com/localstack:latest
        environment:
          SERVICES: dynamodb
    resource_class: medium+

jobs:
  init:
    working_directory: ~/mediaview
    executor: mv-exec
    steps:
      - restore_cache:
          keys:
            - source-v1-{{ .Branch }}-{{ .Revision }}
            - source-v1-{{ .Branch }}-
            - source-v1-

      - checkout

      - save_cache:
          key: source-v1-{{ .Branch }}-{{ .Revision }}
          paths:
            - ".git"

      - run:
          name: Add NPM authentication token
          command: echo "//registry.npmjs.org/:_authToken=$NPM_AUTH_TOKEN" > ~/.npmrc

      - restore_cache:
          name: Restore Yarn package cache
          keys:
            - yarn-packages-v3-{{ .Branch }}-{{ checksum "yarn.lock" }}
            - yarn-packages-v3-{{ .Branch }}-
            - yarn-packages-v3-

      - run:
          name: Install dependencies for node
          command: yarn install --immutable

      - save_cache:
          name: Save Yarn package cache
          key: yarn-packages-v3-{{ .Branch }}-{{ checksum "yarn.lock" }}
          paths:
            - ./.yarn/cache

      - persist_to_workspace:
          root: ~/
          paths:
            - mediaview

  lint:
    working_directory: ~/mediaview
    executor: mv-exec
    steps:
      - attach_workspace:
          at: ~/

      - run:
          name: Run linter
          command: yarn run lint

  test:
    working_directory: ~/mediaview
    executor: mv-exec
    environment:
      IAM_WEB_HOST: 'http://localhost'
      IAM_WEB_PORT: '4050'
      NODE_ENV: circleci
    steps:
      - attach_workspace:
          at: ~/

      - run:
          name: Get ffmpeg
          environment:
            DEPS_LOCATION: "https://s3.amazonaws.com/ownzones-deploy/zypline"
          command: |
            wget -q "$DEPS_LOCATION/debs/20200721/libnettle4_2.7.1-5_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20200721/libhogweed2_2.7.1-5_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20200721/libgnutls-deb0-28_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20200718/libass5_0.13.4-2_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20181114/ffmpeg_4.1-2_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20170417/x264_20160920-2245_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20180507/libopenjpeg_2.3.0-1_amd64.deb"
            wget -q "$DEPS_LOCATION/debs/20171001/kakadu_7.10.2-1_amd64.deb"

      - run:
          name: Install packages
          command: |
            apt-get -q update
            apt-get -fyq install libopenjp2-7 awscli python python-dev python-pip zip jq libxcb-shm0
            dpkg --force-depends -i "libnettle4_2.7.1-5_amd64.deb" "libhogweed2_2.7.1-5_amd64.deb" "libgnutls-deb0-28_amd64.deb"\
                                    "libass5_0.13.4-2_amd64.deb" "ffmpeg_4.1-2_amd64.deb" "x264_20160920-2245_amd64.deb" \
                                    "kakadu_7.10.2-1_amd64.deb" "libopenjpeg_2.3.0-1_amd64.deb"
            apt-get -fyq install

      - run:
          name: Install AMPAS CTL tools
          command: ./bin/install-ctl-1.5.2.sh

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

      - run:
          name: Run tests
          command: yarn run test

      - store_test_results:
          path: coverage

      - store_artifacts:
          path: coverage

  build:
    working_directory: ~/mediaview
    executor: mv-exec
    steps:
      - attach_workspace:
          at: ~/

      - run:
          name: Run build
          command: yarn run build

      - run:
          name: Application Version
          command: echo $CIRCLE_BRANCH-$CIRCLE_BUILD_NUM > ./dist/VERSION

      - persist_to_workspace:
          root: ~/
          paths:
            - mediaview/dist

  deploy:
    working_directory: ~/mediaview
    executor: mv-exec
    steps:
      - attach_workspace:
          at: ~/

      - setup_remote_docker:
          docker_layer_caching: true
          version: 20.10.11

      - run:
          name: Login to ECR
          command: aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 492750062338.dkr.ecr.us-east-1.amazonaws.com

      - run:
          name: Build docker image
          command: docker build --build-arg NPM_AUTH_TOKEN=$NPM_AUTH_TOKEN --rm=false -t image .

      - run:
          name: Deploy MGMT & Bump platform version
          command: yarn run deploy --account-id 492750062338 --repo-name tf-ecr-mediaview --aws-access-key TF_ECR_CICD_MEDIAVIEW_ACCESS_KEY --aws-secret-access-key TF_ECR_CICD_MEDIAVIEW_SECRET_KEY --bump-platform-version

workflows:
  build:
    jobs:
      - init
      - lint:
          requires:
            - init
      - test:
          requires:
            - init
      - build:
          requires:
            - init
      - deploy:
          requires:
            - build
