version: 2.1
orbs:
    aws-ecr: circleci/aws-ecr@4.0.1
executors:
  docker_build:
    machine:
      docker_layer_caching: true
jobs:
    build:
        working_directory: ~/alchemy-fr/Phraseanet
        parallelism: 2
        shell: /bin/bash --login
        environment:
          CIRCLE_ARTIFACTS: /tmp/circleci-artifacts
          CIRCLE_TEST_REPORTS: /tmp/circleci-test-results
        docker:
        - image: circleci/build-image:ubuntu-14.04-XXL-upstart-1189-5614f37
        - image: circleci/rabbitmq:3.7.7
        steps:
        - checkout
        - run: mkdir -p $CIRCLE_ARTIFACTS $CIRCLE_TEST_REPORTS
        - run:
            working_directory: ~/alchemy-fr/Phraseanet
            command: nvm install v10.12.0 && nvm alias default v10.12.0
        - run:
            working_directory: ~/alchemy-fr/Phraseanet
            command: 'sudo service mysql status || sudo service mysql start;'
        # Dependencies
        #   This would typically go in either a build or a build-and-test job when using workflows
        # Restore the dependency cache
        - restore_cache:
            keys:
            # This branch if available
            - v1-dep-{{ .Branch }}-
            # Default branch if not
            - v1-dep-master-
            # Any branch if there are none on the default branch - this should be unnecessary if you have your default branch configured correctly
            - v1-dep-
        # This is based on your 1.0 configuration file or project settings
        - run: echo 127.0.0.1 redis elasticsearch db rabbitmq | sudo tee -a /etc/hosts
        - run: git clone https://github.com/alanxz/rabbitmq-c
        - run: cd rabbitmq-c && git checkout 2ca1774489328cde71195f5fa95e17cf3a80cb8a
        - run: cd rabbitmq-c && git submodule init && git submodule update && autoreconf -i && ./configure && make && sudo make install
        # disabled because pear.php.net is down cause of security failure
        #- run: pecl channel-update pear.php.net
        - run: yes '' | pecl install amqp-1.9.3
        - run: yes '' | pecl install imagick
        - run: sudo apt-get install libzmq-dev
        - run: yes '' | pecl install zmq-beta
        - run: echo "extension = amqp.so" > /opt/circleci/php/$(phpenv global)/etc/conf.d/amqp.ini
        - run: echo "extension = zmq.so" > /opt/circleci/php/$(phpenv global)/etc/conf.d/zmq.ini
        - run: echo "date.timezone = UTC" > /opt/circleci/php/$(phpenv global)/etc/conf.d/timezone.ini
        - run: sed -i 's/^\(session.cache_limiter = \).*/\1""/' /opt/circleci/php/$(phpenv global)/etc/php.ini
        - run: npm rebuild node-sass
        # This is based on your 1.0 configuration file or project settings
        - run: composer install --no-progress --no-interaction --optimize-autoloader
        # This is based on your 1.0 configuration file or project settings
        - run: node -v
        - run: npm -v
        - run: npm install -g yarn
        - run: rm ~/.yarn/bin/yarn
        - run: ln -s /opt/circleci/nodejs/v10.12.0/bin/yarn ~/.yarn/bin/yarn
        - run: yarn install
        - run: if [[ ! -e elasticsearch-2.3.3 ]]; then wget --no-check-certificate https://download.elastic.co/elasticsearch/elasticsearch/elasticsearch-2.3.3.tar.gz && tar -xvf elasticsearch-2.3.3.tar.gz && elasticsearch-2.3.3/bin/plugin install analysis-icu; fi
        - run:
            command: elasticsearch-2.3.3/bin/elasticsearch
            background: true
        # Save dependency cache
        - save_cache:
            key: v1-dep-{{ .Branch }}-{{ epoch }}
            paths:
            # This is a broad list of cache paths to include many possible development environments
            # You can probably delete some of these entries
            - vendor/bundle
            - ~/virtualenvs
            - ~/.m2
            - ~/.ivy2
            - ~/.bundle
            - ~/.go_workspace
            - ~/.gradle
            - ~/.cache/bower
            # These cache paths were specified in the 1.0 config
            - elasticsearch-2.3.3
            - node_modules
            - ~/.composer
        # This is based on your 1.0 configuration file or project settings
        - run: mysql -u ubuntu -e 'CREATE DATABASE update39_test;CREATE DATABASE ab_test;CREATE DATABASE db_test;SET @@global.sql_mode=STRICT_ALL_TABLES;SET @@global.max_allowed_packet=33554432;SET @@global.wait_timeout=999999;';
        # This is based on your 1.0 configuration file or project settings
        - run: ./bin/developer system:uninstall -v
        - run: ./bin/setup system:install -v --email=test@phraseanet.com --password=test --db-host=127.0.0.1 --db-user=ubuntu --db-template=fr-simple --db-password= --databox=db_test --appbox=ab_test --server-name=http://127.0.0.1 -y;
        - run: ./bin/developer ini:setup-tests-dbs -v
        - run: ./bin/console searchengine:index:create -v
        - run: ./bin/developer phraseanet:regenerate-sqlite -v
        - run: ./bin/developer phraseanet:generate-js-fixtures -v
        # Test
        #   This would typically be a build job when using workflows, possibly combined with build
        # This is based on your 1.0 configuration file or project settings
        - run: case $CIRCLE_NODE_INDEX in 0) EXIT=0; php -d memory_limit=-1 bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit-unit.xml --exclude-group legacy || EXIT=$?; php -d memory_limit=-1 bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit-legacy-no-web.xml --group legacy --exclude-group web || EXIT=$?; exit $EXIT;; 1) php -d memory_limit=-1 bin/phpunit --log-junit $CIRCLE_TEST_REPORTS/phpunit/junit-legacy-web.xml --group web ;; esac
        # Teardown
        #   If you break your build into multiple jobs with workflows, you will probably want to do the parts of this that are relevant in each
        # Save test results
        - store_test_results:
            path: /tmp/circleci-test-results
        # Save artifacts
        - store_artifacts:
            path: /tmp/circleci-artifacts
        - store_artifacts:
            path: /tmp/circleci-test-results


    build_phraseanet-fpm:
        machine:
          image: ubuntu-1604:201903-01
          docker_layer_caching: true
        working_directory: ~/alchemy-fr/Phraseanet
        steps:
            - checkout
            - aws-ecr/ecr-login:
                region: AWS_DEFAULT_REGION
            - run: docker build --target phraseanet-fpm -t ${AWS_ACCOUNT_URL}/phraseanet-fpm:${CIRCLE_BRANCH} .
            - aws-ecr/push-image:
                account-url: AWS_ACCOUNT_URL
                repo: "phraseanet-fpm"
                tag: "${CIRCLE_BRANCH}"

    build_phraseanet-worker:
        machine:
          image: ubuntu-1604:201903-01
          docker_layer_caching: true
        working_directory: ~/alchemy-fr/Phraseanet
        steps:
            - checkout
            - aws-ecr/ecr-login:
                region: AWS_DEFAULT_REGION
            - run: docker build --target phraseanet-worker -t ${AWS_ACCOUNT_URL}/phraseanet-worker:${CIRCLE_BRANCH} .
            - aws-ecr/push-image:
                account-url: AWS_ACCOUNT_URL
                repo: "phraseanet-worker"
                tag: "${CIRCLE_BRANCH}"

    build_phraseanet-nginx:
        machine:
          image: ubuntu-1604:201903-01
          docker_layer_caching: true
        working_directory: ~/alchemy-fr/Phraseanet
        steps:
            - checkout
            - aws-ecr/ecr-login:
                region: AWS_DEFAULT_REGION
            - run: docker build --target phraseanet-nginx -t ${AWS_ACCOUNT_URL}/phraseanet-nginx:${CIRCLE_BRANCH} .
            - aws-ecr/push-image:
                account-url: AWS_ACCOUNT_URL
                repo: "phraseanet-nginx"
                tag: "${CIRCLE_BRANCH}"


workflows:
    version: 2
    oldfashion:
        jobs:
            - build
    newfashion:
        jobs:
            - build_phraseanet-fpm:
                context: "AWS London"
            - build_phraseanet-worker:
                context: "AWS London"
            - build_phraseanet-nginx:
                context: "AWS London"
