# PHP CircleCI 2.0 configuration file
#
# Check https://circleci.com/docs/2.0/language-php/ for more details
#
version: 2
jobs:
  build:
    docker:
      # specify the version you desire here
      # - image: alefesouza/php7-laravel-node-yarn
      - image: circleci/php:7.1-node-browsers
      - image: circleci/mysql:5.7
        environment:
          - MYSQL_USER=root
          - MYSQL_PASSWORD=
          - MYSQL_ALLOW_EMPTY_PASSWORD=true
          - MYSQL_DATABASE={{ name }}_db

    working_directory: ~/{{ name }}

    steps:
      - checkout

      - run: sudo apt install -y zlib1g-dev libicu-dev g++
      - run: sudo docker-php-ext-install zip
      - run: sudo docker-php-ext-install pdo_mysql
      - run: sudo docker-php-ext-configure intl && sudo docker-php-ext-install intl
      - run: sudo composer self-update

      # Download and cache dependencies
      # - restore_cache:
      #     keys:
      #     - v1-dependencies-\{{ checksum "composer.json" }}
      #     # fallback to using the latest cache if no exact match is found
      #     - v1-dependencies-

      - run:
          name: Run Composer
          command: |
            cp .env.testing.example .env
            composer install -n --prefer-dist
            composer run key:generate
            php artisan vue-i18n:generate
            php artisan migrate --seed
            composer run jwt:generate

      # - save_cache:
      #     key: composer-v1-\{{ checksum "composer.lock" }}
      #     paths:
      #       - vendor

      - restore_cache:
          keys:
            - node-v1-\{{ checksum "frontend/package.json" }}
            - node-v1-

      - run: cd frontend && yarn install
      - run:
          name: Run Yarn
          command: |
            cd frontend
            yarn --pure-lockfile
            yarn build:test
      - save_cache:
          key: node-v1-\{{ checksum "frontend/package.json" }}
          paths:
            - frontend/node_modules

      - run:
          name: Test
          command: |
            mkdir -p out/tests
            composer test -- --log-junit out/tests/tests-phpunit.xml
            cd frontend
            yarn test:unit

          # mv tests-jest.xml ../out/tests/tests-jest.xml
      - store_artifacts:
          path: out/tests
          destination: test-results

      - store_test_results:
          path: out/tests

  deploy:
    docker:
      # specify the version you desire here
      - image: circleci/php:7.1-jessie-node-browsers

    working_directory: ~/{{ name }}

    steps:
      - checkout

      # Install PHP vendors
      - run:
          name: Run Composer
          command: |
            composer install -n --prefer-dist
            php artisan vue-i18n:generate

      # Save vendors
      - save_cache:
          paths:
            - ./vendor
          key: v1-dependencies-\{{ checksum "composer.json" }}

      # Install NPM packages for frontend things
      - run: cd frontend && npm install

      # Save packages
      - save_cache:
          key: node-v3-\{{ checksum "frontend/package.json" }}
          paths:
            - frontend/node_modules
            - ~/.yarn

      # Install software required to run EB deployer
      - run:
          name: Installing deployment dependencies
          working_directory: ~/{{ name }}
          command: |
            sudo apt-get -y -qq update
            sudo apt-get install python-pip python-dev build-essential
            sudo pip install --upgrade setuptools
            sudo pip install awsebcli --upgrade
      # Build css and js (prod script builds these. you should make your own)
      - run:
          name: Install Node modules
          working_directory: ~/{{ name }}
          command: cd frontend && npm run release

      # Add everything (except vendors that will be installed automatically and node_modules that are not needed because we already built js and css) to a ZIP
      - run:
          name: ZIP project
          working_directory: ~/{{ name }}
          command: zip {{ name }}.zip -r * .[^.]* --exclude=vendor/* --exclude=node_modules/* --exclude=frontend/* --exclude=.circleci/* --exclude=.elasticbeanstalk/* --exclude=.git/*

      - run:
          name: Create AWS credentials manually
          command: |
            mkdir ~/.aws
            touch ~/.aws/config
            chmod 600 ~/.aws/config
            echo "[profile controlla]" > ~/.aws/config
            echo "aws_access_key_id=$AWS_ACCESS_KEY_ID" >> ~/.aws/config
            echo "aws_secret_access_key=$AWS_SECRET_ACCESS_KEY" >> ~/.aws/config

      # Run EB deploy
      - run:
          name: Deploying
          command: eb deploy QA-env --profile controlla

  release:
    docker:
      # specify the version you desire here
      - image: circleci/php:7.1-jessie-node-browsers

    working_directory: ~/{{ name }}

    steps:
      - checkout

      # Install PHP vendors
      - run:
          name: Run Composer
          command: |
            composer install -n --prefer-dist
            php artisan vue-i18n:generate

      # Install NPM packages for frontend things
      - run: cd frontend && npm install
      # Install software required to run EB deployer
      - run:
          name: Installing deployment dependencies
          working_directory: ~/{{ name }}
          command: |
            sudo apt-get -y -qq update
            sudo apt-get install python-pip python-dev build-essential
            sudo pip install --upgrade setuptools
            sudo pip install awsebcli --upgrade
      # Build css and js (prod script builds these. you should make your own)
      - run:
          name: Install Node modules
          working_directory: ~/{{ name }}
          command: cd frontend && npm run release

      # Add everything (except vendors that will be installed automatically and node_modules that are not needed because we already built js and css) to a ZIP
      - run:
          name: ZIP project
          working_directory: ~/{{ name }}
          command: zip {{ name }}.zip -r * .[^.]* --exclude=vendor/* --exclude=node_modules/* --exclude=frontend/* --exclude=.circleci/* --exclude=.elasticbeanstalk/* --exclude=.git/*

      - run:
          name: Create AWS credentials manually
          command: |
            mkdir ~/.aws
            touch ~/.aws/config
            chmod 600 ~/.aws/config
            echo "[profile controlla]" > ~/.aws/config
            echo "aws_access_key_id=$AWS_ACCESS_KEY_ID" >> ~/.aws/config
            echo "aws_secret_access_key=$AWS_SECRET_ACCESS_KEY" >> ~/.aws/config

      # Run EB deploy
      - run:
          name: Deploying
          command: eb deploy Production-env --profile controlla

workflows:
  version: 2
  build-deploy:
    jobs:
      - build:
          filters:
            branches:
              only:
                - master
                - QA
                - DevQA
      - deploy:
          requires:
            - build
          filters:
            branches:
              only:
                - QA
      - release:
          requires:
            - build
          filters:
            branches:
              only:
                - master
