version: 2
jobs:
  build:
    docker:
      - image: kporras07/docker-drupal-nginx:php-7.3.x-backstop
      - image: selenium/standalone-chrome-debug:3.6.0
      - image: kporras07/lighthouse-ci:nonheadless
      - image: mariadb:5.5
        environment:
          MYSQL_DATABASE: drupal
          MYSQL_ROOT_PASSWORD: drupal
    working_directory: /var/www/<%= appName %>
    steps:
      - run: apt-get update -y && apt-get install python -y
      - run: echo 127.0.0.1 <%= dashedAppName %>.local | tee -a /etc/hosts
      - checkout
      - restore_cache:
          keys:
            - npm-cache-{{ checksum "package.json" }}
            - composer-cache-{{ checksum "composer.lock" }}
      - run: npm install
      - save_cache:
         key: npm-cache-{{ checksum "package.json" }}
         paths:
           - node_modules
      - run:
          name: Install Composer
          command: |
            php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
            php -r "copy('https://composer.github.io/installer.sig', 'composer-setup.sig');" && \
            php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('composer-setup.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
            php composer-setup.php && \
            php -r "unlink('composer-setup.php');"
            mv composer.phar /usr/bin/composer
      - run:
          name: Configure Nginx
          command: |
            cp ./.circleci/<%= appName %> /etc/nginx/sites-available/default
      - run:
          name: Build site.
          command: |
            cp ./.circleci/settings.secret.php ./settings/
            composer install
            chmod 777 -R ./web/sites/default/files
            cd web
            /usr/bin/env PHP_OPTIONS="-d sendmail_path=`which true`"
            if [ -f /var/www/<%= appName %>/config/sync/core.extension.yml ]; then
              echo "Installing with existing config..."
              ../vendor/bin/drush si standard --config-dir="sites/default/config/sync" -y
            else
              echo 'Install without config ...'
              ../vendor/bin/drush si standard -y
            fi
            cd /var/www/<%= appName %>
      - run:
          name: Coding Standards Linters
          command: |
            ./node_modules/.bin/gulp phplint
            ./node_modules/.bin/gulp drupalcs
            ./node_modules/.bin/gulp eslint
      - run:
          name: Behat Testing
          command: |
            /etc/init.d/nginx start > nginx.log &
            /etc/init.d/php7.3-fpm start > php7.3-fpm.log &
            ./vendor/bin/behat -p circle
      - run:
          name: Lighthouse
          command: |
            mkdir /tmp/lighthouse
            curl -X POST -H "Content-Type: application/json" -H "X-API-KEY: abc123" --data '{"output": "html", "url": "http://<%= dashedAppName %>.local"}' http://localhost:8080/ci > /tmp/lighthouse/output.html
      - store_artifacts:
          path: /tmp/lighthouse
          destination: lighthouse
      - run:
          name: Export database
          command: |
            cd /var/www/<%= appName %>/web
            ../vendor/bin/drush sql-dump --result-file=../export.sql
      - persist_to_workspace:
          root: /var/www/
          paths: <%= appName %>

  visual-regression:
    docker:
      - image: kporras07/docker-drupal-nginx:php-7.3.x-backstop
      - image: mariadb:5.5
        environment:
          MYSQL_DATABASE: drupal
          MYSQL_ROOT_PASSWORD: drupal
    working_directory: /var/www/<%= appName %>
    steps:
      - attach_workspace:
          at: /var/www
      - run: echo 127.0.0.1 <%= dashedAppName %>.local | tee -a /etc/hosts
      - run:
          name: Install Composer
          command: |
            php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');" && \
            php -r "copy('https://composer.github.io/installer.sig', 'composer-setup.sig');" && \
            php -r "if (hash_file('SHA384', 'composer-setup.php') === trim(file_get_contents('composer-setup.sig'))) { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;" && \
            php composer-setup.php && \
            php -r "unlink('composer-setup.php');"
            mv composer.phar /usr/bin/composer
      - run:
          name: install dockerize
          command: wget https://github.com/jwilder/dockerize/releases/download/$DOCKERIZE_VERSION/dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && sudo tar -C /usr/local/bin -xzvf dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz && rm dockerize-linux-amd64-$DOCKERIZE_VERSION.tar.gz
          environment:
            DOCKERIZE_VERSION: v0.3.0
      - run:
          name: Import db
          command: |
            dockerize -wait tcp://localhost:3306 -timeout 2m
            cd /var/www/<%= appName %>/web
            cat ../export.sql | ../vendor/bin/drush sqlc
      - run:
          name: Backstop
          command: |
            cp ./.circleci/<%= appName %> /etc/nginx/sites-available/default
            /etc/init.d/nginx start > nginx.log &
            /etc/init.d/php7.3-fpm start > php7.3-fpm.log &
            backstop reference
            backstop test
      - store_artifacts:
          path: ./backstop_data
          destination: backstop

  deploy-live:
    docker:
      - image: kporras07/docker-drupal-nginx:php-7.3.x
    working_directory: /var/www/<%= appName %>
    steps:
      - checkout
      - run:
          name: Install ahoy
          command: |
            curl -L https://github.com/ahoy-cli/ahoy/releases/download/2.0.0/ahoy-bin-`uname -s`-amd64 --output /usr/local/bin/ahoy && chmod +x /usr/local/bin/ahoy
      - add_ssh_keys:
      - run:
          name: Deploy
          command: |
            git config --global user.email $GIT_EMAIL
            git config --global user.name $GIT_NAME
            echo 'Host *' >> /root/.ssh/config
            echo '   StrictHostKeyChecking no' >> /root/.ssh/config
            ahoy site deploy master "Auto deploy triggered from master branch"

workflows:
  version: 2
  build-deploys:
    jobs:
      - build
      - visual-regression:
          requires:
            - build
      - deploy-live-hold:
          type: approval
          requires:
            - build
      - deploy-live:
          requires:
            - build
            - deploy-live-hold
          filters:
            branches:
              only: master
