stages:
  - build
  - prepare  
  - test
  - deploy

variables:
    # Configure mysql environment variables (https://hub.docker.com/_/mysql/)
    MYSQL_DATABASE: "aitrainer"
    MYSQL_ROOT_PASSWORD: "andio2009"
    MYSQL_USER: "aitrainer"
    MYSQL_PASSWORD: "andio2009"


before_script:
  - echo `pwd` # debug
  - echo "$CI_BUILD_NAME, $CI_BUILD_REF_NAME $CI_BUILD_STAGE" # debug
  - export GRADLE_USER_HOME=`pwd`/.gradle
  
cache:
  paths:
    - .gradle/wrapper
    - .gradle/caches

build:
  stage: build
  image: openjdk:latest
  script:
    - ./gradlew assemble
  artifacts:
    paths:
      - build/libs/*.jar
    expire_in: 1 week
  only:
    - master

connect:
    stage: prepare
    image: mysql:latest
    script:
    - apt-get update && apt-get --assume-yes install mysql-client
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql -e "use $MYSQL_DATABASE; show tables;"
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql -e "use $MYSQL_DATABASE; DROP table if exists customer; DROP table if exists exercises; DROP table if exists exercise_type; DROP table if exists exercise_ages;"
    - mysql --user=root --password="$MYSQL_ROOT_PASSWORD" --host=mysql < "data/db/install.sql" #first time

test:
  stage: test
  image: openjdk:latest
  script:
    - export spring_profiles_active=test
    - ./gradlew check

deploy:
  stage: deploy
  image: mysql:latest
  script:
    - apt-get update && apt-get --assume-yes install sshpass
    - chmod +x ci-cd/deploy.sh
    - ci-cd/deploy.sh
  only:
    - master

after_script:
  - echo "End CI"