image: node

stages:
  - build
  - packaging
  - test

test_frontend:
  stage: test
  script:
    - cd frontend-js
    - npm install  
    - npm test

test_backend:
  variables:
    POSTGRES_DB: map_viewer
    POSTGRES_USER: map_viewer
    POSTGRES_PASSWORD: 123qweasdzxc
  image: debian
  services:
    - postgres:latest
  stage: test
  script:
    - apt-get update
    - apt-get install -y openjdk-8-jdk maven
    - mvn test

build_war:
  image: debian
  stage: build
  only:
    - tags
  tags:
     - privileged
  artifacts:
    paths:
      - minerva.war
  script:
    - apt-get update
    - apt-get install -y curl sudo gnupg git openjdk-8-jdk ant maven
    - curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
    - sudo apt-get install -y nodejs
    - ant maven-build
    - cp web/target/web-1.0.war minerva.war

build_debian_package:
  image: debian
  stage: packaging
  only:
    - tags
  tags:
     - privileged
  artifacts:
    paths:
      - "debian/*.deb"
  script:
    - apt-get update
    - apt-get install -y curl sudo gnupg git openjdk-8-jdk ant maven dh-make build-essential lintian devscripts xsltproc fakeroot xsltproc docbook-xsl
    - curl -sL https://deb.nodesource.com/setup_9.x | sudo -E bash -
    - sudo apt-get install -y nodejs
    - export DEBFULLNAME="Piotr Gawron"
    - export DEBEMAIL="piotr.gawron@uni.lu"
    - ant debian-pkg

test_install_debian_package:
  image: debian
  stage: test
  only:
    - tags
# we need to run it in privileged mode due to: https://stackoverflow.com/questions/29683231/tomcat7-fail-to-start-inside-ubuntu-docker-container
  tags:
     - privileged
  services:
     - name: piotrgawron/debian-repo
       alias: debian-repo
  dependencies:
     - build_debian_package
  script:
     - apt-get update
     - apt-get install -y ssh software-properties-common dirmngr wget curl
     - debian_file=$(ls debian/*.deb)
     - echo "PUT $debian_file /docker/incoming" | sftp -o StrictHostKeyChecking=no user@debian-repo
     - ssh root@debian-repo /usr/local/sbin/reprepro-import
     - apt-add-repository "deb http://ppa.launchpad.net/webupd8team/java/ubuntu trusty main"
     - apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys EEA14886
     - apt-add-repository "deb http://debian-repo/ unstable main"
     - apt-get update
# auto accept oracle license
     - echo debconf shared/accepted-oracle-license-v1-1 select true | debconf-set-selections
#hacky way of installing minerva on docker - by default rc-invoke is preventing services changes on docker
     - mv /usr/sbin/policy-rc.d /usr/sbin/policy-rc.d.backup
     - mv Docker/policy-rc.d /usr/sbin/policy-rc.d
     - apt-get install -y --allow-unauthenticated minerva
     - mv /usr/sbin/policy-rc.d.backup /usr/sbin/policy-rc.d 
     - service tomcat8 start
#we need to wait a bit for tomcat start
     - sleep 15
     - wget http://localhost:8080/minerva/
#test if we can login and list projects
     - test 200 = $(curl --write-out %{http_code} --silent --output /dev/null -c cookie.txt http://localhost:8080/minerva/api/doLogin)
     - test 200 = $(curl --write-out %{http_code} --silent --output /dev/null --cookie cookie.txt http://localhost:8080/minerva/api/projects/)

build_rpm:
  image: centos:7
  stage: packaging
  dependencies:
    - build_war
  only:
    - tags
  tags:
     - privileged
  artifacts:
    paths:
      - "rpm/rpmbuildtemp/RPMS/noarch/minerva-*.rpm"
  script:
    - yum -y install rpmdevtools unzip
    - rpm/buildrpm.sh minerva.war

rpmlint:
  image: centos:7
  stage: test
  dependencies:
    - build_rpm
  tags:
     - privileged
  only:
    - tags
  script:
    - yum -y install rpmlint
    - rpmlint rpm/rpmbuildtemp/RPMS/noarch/minerva-*.rpm

