variables:
  PHP_MAJOR_VERSION: 7
  PHP_MINOR_VERSION: 4

stages:
  - validation
  - buildprod
  - build
  - prepare
  - test
  - deploy

.wait:
  script:
    - while [ ! -f /tmp/foo.txt ]; do echo "waiting..."; sleep 1; done

.cache_paths: &cache_paths
  paths:
    - .docker-init/
    - assets/
    - config/
    - drush/
    - files/
    - settings/
    - vendor/
    - web/core/
    - web/libraries/
    - web/modules/contrib/
    - web/profiles/contrib/
    - web/sites/
    - web/themes/contrib/
    - .ahoy.yml
    - .env
    - docker-compose.yml

.artifact_paths_prod: &artifact_paths_prod
  paths:
    - config/
    - drush/
    - vendor/
    - web/
    - composer.json
    - composer.lock

.artifact_paths: &artifact_paths
  paths:
    - .docker-init/
    - assets/
    - config/
    - drush/
    - files/
    - settings/
    - tests/
    - vendor/
    - web/
    - .ahoy.yml
    - .env
    - composer.*
    - docker-compose.yml

.cache_default: &cache_default
  key: build-${COMPOSE_PROJECT_NAME}
  <<: *cache_paths

.cache_prod: &cache_prod
  key: build-prod-${COMPOSE_PROJECT_NAME}
  <<: *cache_paths

.prerequisites:
  stage: validation
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    DBREQUIRED="no"
    if [[ "$CI_COMMIT_REF_NAME" == "master" ]]; then
      DBREQUIRED="yes"
    elif [[ "$CI_COMMIT_MESSAGE" == *"[PULL_DB]"* ]]; then
      DBREQUIRED="yes"
    elif [[ "$PULL_DB" == "yes" ]]; then
      DBREQUIRED="yes"
    else
      ID=$(docker container ls --all -q -f name=^${COMPOSE_PROJECT_NAME}_mariadb_1$)
      if [[ ! -n ${ID} ]]; then
        DBREQUIRED="yes"
      else
        TABLES=$(docker exec -i ${COMPOSE_PROJECT_NAME}_mariadb_1 mysql -u drupal --password=drupal -e "show tables;" drupal)
        if [[ "${TABLES}" != *"users_data"* ]]; then
          DBREQUIRED="yes"
        fi
      fi
    fi
    echo "DBREQUIRED=${DBREQUIRED}" >> validation.env
    if [[ "$DBREQUIRED" == "yes" ]]; then
      echo "DB needs to be pulled."
    else
      echo "No pulling required."
    fi

    INITIALINSTALL="no"
    if [[ "$CI_COMMIT_MESSAGE" == *"[INITIAL_INSTALL]"* ]]; then
      INITIALINSTALL="yes"
    elif [[ "$INITIAL_INSTALL" == "yes" ]]; then
      INITIALINSTALL="yes"
    fi
    echo "INITIALINSTALL=${INITIALINSTALL}" >> validation.env

    RESETLOCALE="no"
    if [[ "$CI_COMMIT_MESSAGE" == *"[RESET_LOCALE]"* ]]; then
      RESETLOCALE="yes"
    elif [[ "$RESET_LOCALE" == "yes" ]]; then
      RESETLOCALE="yes"
    fi
    echo "RESETLOCALE=${RESETLOCALE}" >> validation.env
  except:
    variables:
      - $CAE
      - $DISABLE_CI_TESTS
  artifacts:
    reports:
      dotenv: validation.env

.build-prod:
  stage: buildprod
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  cache:
    <<: *cache_prod
    policy: pull-push
  script: |
    if [[ -n $SSH_PRIVATE_KEY ]]; then
      eval $(ssh-agent -s)
      echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
      mkdir -p ~/.ssh
      chmod 700 ~/.ssh
      ssh-keyscan gitlab.lakedrops.com >> ~/.ssh/known_hosts
      chmod 644 ~/.ssh/known_hosts
      ssh git@gitlab.lakedrops.com
    fi
    if [[ -n $DOWNGRADE_COMPOSER ]]; then
      composer self-update --$DOWNGRADE_COMPOSER;
    fi
    if [[ -n $GITLAB_ACCESS_TOKEN ]]; then
      composer config -g gitlab-token.gitlab.lakedrops.com $GITLAB_ACCESS_TOKEN;
    fi
    composer install --no-interaction --no-progress --no-dev
  artifacts:
    name: build-prod
    when: always
    <<: *artifact_paths_prod
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE

.build:
  stage: build
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  cache:
    <<: *cache_default
    policy: pull-push
  script: |
    if [[ -n $SSH_PRIVATE_KEY ]]; then
      eval $(ssh-agent -s)
      echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
      mkdir -p ~/.ssh
      chmod 700 ~/.ssh
      ssh-keyscan gitlab.lakedrops.com >> ~/.ssh/known_hosts
      chmod 644 ~/.ssh/known_hosts
      ssh git@gitlab.lakedrops.com
    fi
    if [[ -n $DOWNGRADE_COMPOSER ]]; then
      composer self-update --$DOWNGRADE_COMPOSER;
    fi
    if [[ -n $GITLAB_ACCESS_TOKEN ]]; then
      composer config -g gitlab-token.gitlab.lakedrops.com $GITLAB_ACCESS_TOKEN;
    fi
    composer install --no-interaction --no-progress
    composer lakedrops:scaffold --no-interaction
    composer lakedrops:docker4drupal --no-interaction
    docker-compose up -d
    composer lakedrops:behat --no-interaction
    mkdir -p files/default/files/css
    mkdir -p files/default/files/js
    mkdir -p files/default/files/languages
    mkdir -p files/default/files/translations
    chmod -R ugo+w files/default/files/
  artifacts:
    name: build
    when: always
    <<: *artifact_paths
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE

.dumpdb:
  stage: build
  tags:
    - ansible
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$DBREQUIRED" == "yes" ]]; then
      if [[ "$INITIALINSTALL" == "no" ]]; then
        ascr drupal-dump-db ${CI_PROJECT_PATH} ${PWD}/${CI_PROJECT_NAME}.sql master
      fi
    fi
  cache: {}
  dependencies:
    - 'Validate Environment'
  artifacts:
    name: dbdump
    when: always
    paths:
      - ${CI_PROJECT_NAME}.sql
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS

.importdb:
  stage: prepare
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$DBREQUIRED" == "yes" ]]; then
      if [[ "$INITIALINSTALL" == "no" ]]; then
        docker cp ${CI_PROJECT_NAME}.sql ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html
        drush sql:drop --yes
        drush sql:query --file=../${CI_PROJECT_NAME}.sql
        drush cr
        drush updatedb
        drush config-import sync
        drush cr
      fi
    fi
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS
  dependencies:
    - 'Validate Environment'
    - 'Build Site'
    - 'Download DB'

.updatedb:
  stage: prepare
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$DBREQUIRED" == "no" ]]; then
      if [[ "$INITIALINSTALL" == "no" ]]; then
        drush cr
        drush updatedb
        drush config-import sync
        drush cr
      fi
    fi
  except:
    refs:
      - master
      - tags
    variables:
      - $CAE
      - $DISABLE_CI_TESTS
  dependencies:
    - 'Validate Environment'
    - 'Build Site'

.theme:
  stage: prepare
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script:
    - docker run --name=${COMPOSE_PROJECT_NAME}_node -id -v $(php /usr/local/bin/volume.php) -w ${PWD} registry.lakedrops.com/docker/node:12-stretch-slim
    - docker exec ${COMPOSE_PROJECT_NAME}_node npm install --no-save
    - docker exec -e NODE_ENV=production ${COMPOSE_PROJECT_NAME}_node ./node_modules/gulp/bin/gulp.js sass
  after_script:
    - docker rm -f ${COMPOSE_PROJECT_NAME}_node
  artifacts:
    name: css
    when: always
    paths:
      - $THEME_CSS_PATH
  dependencies:
    - 'Build Site'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE

.codestyle:
  stage: test
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      # ahoy test phpcs -- -n --report=xml --report-file=phpcs.xml
      # docker cp ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html/phpcs.xml ./phpcs.xml
      echo "Disabled because of Drupal Coder issues" > ./phpcs.xml
    fi
  artifacts:
    name: phpcs
    when: always
    paths:
      - phpcs.xml
  dependencies:
    - 'Validate Environment'
    - 'Build Site'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS
      - $DISABLE_CI_TEST_CODESTYLE

.phpunit:
  stage: test
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      ahoy test phpunit -- --log-junit junit.xml --testdox-html testdox.html
    fi
  after_script:
    - docker cp ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html/junit.xml ./junit.xml
    - docker cp ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html/testdox.html ./testdox.html
  artifacts:
    name: phpunit
    when: always
    paths:
      - junit.xml
      - testdox.html
    reports:
      junit: junit.xml
  dependencies:
    - 'Validate Environment'
    - 'Build Site'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS
      - $DISABLE_CI_TEST_PHPUNIT

.behat:
  stage: test
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      ahoy behat update
      ahoy behat test2xml
    fi
  after_script:
    - docker cp ${COMPOSE_PROJECT_NAME}_php_1:/var/www/html/tests/output/default.xml ./behat.xml
  retry: 1
  artifacts:
    name: behat
    when: always
    paths:
      - behat.xml
  dependencies:
    - 'Validate Environment'
    - 'Build Site'
    - 'Build Theme'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS
      - $DISABLE_CI_TEST_BEHAT

.backstop:
  stage: test
  tags:
    - default
  image: registry.lakedrops.com/docker/gitlab-drupal-ci:php-${PHP_MAJOR_VERSION}.${PHP_MINOR_VERSION}
  variables:
    GIT_STRATEGY: none
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      backstop test
    fi
  artifacts:
    name: backstop
    when: always
    paths:
      - config/backstop_reference
      - tests/backstop/backstop_data
  dependencies:
    - 'Validate Environment'
    - 'Build Site'
    - 'Build Theme'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
      - $DISABLE_CI_TESTS
      - $DISABLE_CI_TEST_BACKSTOP

.deploy:
  stage: deploy
  tags:
    - ansible
  variables:
    GIT_STRATEGY: none
  environment:
    name: ${ENVIRONMENT_NAME}
    url: ${ENVIRONMENT_URL}
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      ascr drupal-deploy $HOST_NAME $SITE_NAME --extra-vars="DRUPAL_ARTEFACT_SRC=${PWD}" ${CAE}
    else
      ascr drupal-config $HOST_NAME $SITE_NAME --extra-vars="DRUPAL_ARTEFACT_SRC=${PWD}" ${CAE}
    fi
  cache: {}
  dependencies:
    - 'Validate Environment'
    - 'Build Theme'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE

.deployartefact:
  stage: deploy
  tags:
    - ansible
  variables:
    GIT_STRATEGY: none
  environment:
    name: ${ENVIRONMENT_NAME}
    url: ${ENVIRONMENT_URL}
  script: |
    if [[ "$INITIALINSTALL" == "no" ]]; then
      ascr drupal-deploy $HOST_NAME $SITE_NAME --extra-vars="DRUPAL_ARTEFACT_SRC=${PWD}" ${CAE}
    else
      ascr drupal-config $HOST_NAME $SITE_NAME --extra-vars="DRUPAL_ARTEFACT_SRC=${PWD}" ${CAE}
    fi
    if [[ "$RESETLOCALE" == "yes" ]]; then
      ascr drupal-reset-locale $HOST_NAME $SITE_NAME
    fi
  cache: {}
  dependencies:
    - 'Validate Environment'
    - 'Build Prod Site'
    - 'Build Theme'
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_MESSAGE =~ /^Merge tag /i
      - $CAE
