stages:
  - build
  - generate-test-env
  - test
  - dockerize

variables:
  # to avoid "fatal: git fetch-pack: expected shallow list"
  GIT_STRATEGY: clone

build:
  stage: build
  tags:
    - docker
  only:
    refs:
      - ci-test
  script:
    - mvn clean package -DskipTests -DfinalName=vospace
  artifacts:
    paths:
      - target/vospace.jar
    expire_in: 7 days

pre_test:
  stage: generate-test-env
  tags:
    - shell
  script:
    - docker login -u "${CI_REGISTRY_USER}" -p "${CI_REGISTRY_PASSWORD}" "${CI_REGISTRY}"
    - docker build -t "${CI_REGISTRY_IMAGE}/vospace-test-env" -f src/test/resources/testing-Dockerfile .
    - docker push "${CI_REGISTRY_IMAGE}/vospace-test-env"
  only:
    refs:
      - ci-test
    changes:
      - src/test/resources/testing-Dockerfile

test:
  stage: test
  tags:
    - docker
  image: "${CI_REGISTRY_IMAGE}/vospace-test-env"
  variables:
    FILE_CATALOG_REPO_URL: "https://gitlab-ci-token:${CI_JOB_TOKEN}@www.ict.inaf.it/gitlab/ia2/vospace-file-catalog.git"
  script:
    - pwd
    - ls
    - git clone ${FILE_CATALOG_REPO_URL}
    - mvn clean test -Dinit_database_scripts_path=../../vospace-file-catalog
    - awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print "coverage=" 100*covered/instructions }' target/site/jacoco/jacoco.csv
  coverage: '/coverage=\d+\.\d+/'
  only:
    - ci-test

dockerize:
  stage: dockerize
  tags:
    - shell
  only:
    refs:
      - ci-test
  script:
    - docker login -u $CI_REGISTRY_USER -p $CI_REGISTRY_PASSWORD $CI_REGISTRY
    - docker build -t $CI_REGISTRY_IMAGE .
    - docker push $CI_REGISTRY_IMAGE
