before_script:
  - '[[ -d /srv/soutien/ ]] || mkdir -p /srv/soutien/'
  - 'which git || (apt-get update -yq && apt-get install git -yqq)'
stages:
  - test
  - cleanup_test
  - deploy

job test:
  stage: test
  variables:
    GIT_SUBMODULE_STRATEGY: normal
    SQL_HOST: "localhost"
    SQL_PORT: "3306"
    SQL_DATABASE: "test_$CI_BUILD_REF"
    SQL_USER: "soutien"
    SQL_PASSWORD: "soutien"
    PHINX_PASSWORD: "$SQL_PASSWORD"
    PHINX_USER: "$SQL_USER"
    PHINX_DATABASE: "test_$CI_BUILD_REF"
    PHINX_HOST: "$SQL_HOST"
    PHINX_PORT: "$SQL_PORT"
    SYSADMIN: "gitlab@email.com"
    FDNNURL1: "https://secure.fd2n.org/fd2n/cb"
    FDNNURL2: "https://secure.fdn2.org/fdn2/don"
    BASE_DOMAIN: "dev.laquadrature.net"
    SITE_ID: "XXXXXXXX"
    CERTIFICATE: "XXXXXXXXXXXXXXXXXXXXXXXXx"
    CTX_MODE: "TEST"
    PIPLOME_PATH: "/var/www/site/pdf/"
    PIPLOME_URL: "https://www.laquadrature.net/pdf/"
    CAMPAIGN_START_DATE: "2016-11-10"
    CAMPAIGN_BUDGET: "321000"
    LOGS: "./"
    SMTP_HOST: "smtp.example.com"
    SMTP_PORT: "25"
    SMTP_SECURITY: "none"
    SMTP_USER: "user"
    SMTP_PW: "password"
    DEBUG: "0"
    ENV: "test"
  script:
    - echo "SQL_PASSWORD=${SQL_PASSWORD}" > app/env
    - echo "SQL_USER=${SQL_USER}" >> app/env
    - echo "SQL_DATABASE=${SQL_DATABASE}" >> app/env
    - make install
    - make server-start
    - make -k test
    - make server-stop
    - mysql -u $SQL_USER -p$SQL_PASSWORD -e "DROP DATABASE $SQL_DATABASE"
  tags:
    - preprod

cleanup test:
  stage: cleanup_test
  variables:
    SQL_DATABASE: "test_$CI_BUILD_REF"
    SQL_USER: "soutien"
    SQL_PASSWORD: "soutien"
  script:
    - make server-stop
    - mysql -u $SQL_USER -p$SQL_PASSWORD -e "DROP DATABASE $SQL_DATABASE"
  when: on_failure
  tags:
    - preprod

job install:
  stage: deploy
  environment: preprod
  variables:
    PHINX_ENVIRONMENT: "development"
    PROD_HOME: "/home/don/don/"
  script:
    - chmod a+x ci/install.sh
    - ./ci/install.sh
  stage: deploy
  tags:
    - preprod
  only:
    - preprod

job deploy:
  stage: deploy
  environment: production
  variables:
    PHINX_ENVIRONMENT: "production"
    PROD_HOME: "/home/don/don/"
  script:
    - chmod a+x ci/install.sh
    - ./ci/install.sh
  stage: deploy
  tags:
    - prod
  only:
    - master
