---
# Build JAVA applications using Apache Maven (http://maven.apache.org)
stages:
  - build
  - test
  - deploy

variables:
  MAVEN_OPTS: "-Dhttps.protocols=TLSv1.2 -Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true"
  
  # Backend 
  MAVEN_CLI_OPTS_1: "-f dmnverifierapi/pom.xml --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  # Frontend
  MAVEN_CLI_OPTS_2: "-f dmnverifierfrontend/pom.xml --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"

# Cache downloaded dependencies and plugins between builds.
# To keep cache across branches add 'key: "$CI_JOB_NAME"'
cache:
  paths:
    - .m2/repository

# Remove trash after builds
default:
  after_script:
    - sudo rm -rf *

.validate: &validate
  stage: build
  script:
    - 'sudo mvn $MAVEN_CLI_OPTS_1 test-compile'
    - 'sudo mvn $MAVEN_CLI_OPTS_2 test-compile'
    - 'sudo cat target/site/jacoco/index.html'
    
# For merge requests do not `install` but only run `verify`.
# See https://maven.apache.org/guides/introduction/introduction-to-the-lifecycle.html
.verify: &verify
  stage: test
  script:
    - 'sudo mvn $MAVEN_CLI_OPTS_1 verify'
    - 'sudo mvn $MAVEN_CLI_OPTS_2 verify'
  except:
    - master

# Validate merge requests using JDK8
validate:jdk8:
  <<: *validate
  image: maven:3.6.0-jdk-8

# Verify merge requests using JDK8
verify:jdk8:
  <<: *verify
  image: maven:3.6.0-jdk-8

deploy:
  image: maven:3.6.0-jdk-8
  stage: deploy
  script:
    - sudo systemctl stop dmn-verifier-backend
    - sudo systemctl stop dmn-verifier-frontend
    - 'sudo mvn $MAVEN_CLI_OPTS_1 clean install -DskipTests'
    - sudo cp dmnverifierapi/target/*-runner.jar /opt/dmn/dmn-backend-runner.jar
    - 'sudo mvn $MAVEN_CLI_OPTS_2 clean install -DskipTests'
    - sudo cp dmnverifierfrontend/target/*-runner.jar /opt/dmn/dmn-frontend-runner.jar
    - sudo systemctl start dmn-verifier-backend
    - sudo systemctl start dmn-verifier-frontend
  only:
    - master