image: docker:latest
services:
  - docker:dind

variables:
  MYSQL_USER: rekallfrwsmember
  MYSQL_DATABASE: rekallfrwsmember
  MYSQL_PASSWORD: sm4SyZVPu6wpjZfg
  MYSQL_ROOT_PASSWORD:  root
  TEST_IMAGE: registry.gitlab.tetras-libre.fr/rekall/memorekall-member-new
  CI_REGISTRY: registry.gitlab.tetras-libre.fr
  TZ: "Europe/Paris"


composer:
  stage: test
  cache:
    key: memorekall-member-global-cache-key
    paths:
        - $CI_PROJECT_DIR/vendor/
        - $CI_PROJECT_DIR/node_modules/
  before_script:
      - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
      - docker pull $TEST_IMAGE
      - docker network inspect rekall || docker network create rekall
      - docker ps -a -f name=mysql | grep mysql && docker rm mysql || true
      - docker ps -a -f name=rekall | grep rekall && docker rm rekall || true
  rules:
    - if: '$CI_PIPELINE_SOURCE == "merge_request_event" || $CI_PIPELINE_SOURCE == "web"'
    - if: '$CI_COMMIT_MESSAGE =~ /test/'
    - if: '$CI_COMMIT_BRANCH  =~ /test/ || $CI_COMMIT_BRANCH == "production" || $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH'
  script:
    - docker run --rm --network rekall -e TZ=$TZ -e MYSQL_DATABASE=$MYSQL_DATABASE -e MYSQL_USER=$MYSQL_USER -e MYSQL_ROOT_PASSWORD=$MYSQL_ROOT_PASSWORD -e MYSQL_PASSWORD=$MYSQL_PASSWORD -v ${CI_PROJECT_DIR}/docker/mysql/init_db_d:/docker-entrypoint-initdb.d -d --name=mysql mysql:5.6
    - docker run --rm --name rekall --network rekall -v ${CI_PROJECT_DIR}:/sites/memorekall/www/web_dev -v /log:/sites/memorekall/www/web_dev/log --entrypoint /sites/memorekall/www/web_dev/tools/ci-job.sh $TEST_IMAGE
  after_script:
    - docker stop mysql
    - docker stop rekall
    - docker network rm rekall
  artifacts:
    when: always
    expire_in: 1 week
    paths:
      - tests/_output
