variables:
    SAREF_DEV_KEY: <my_private_key>
    SAREF_PORTAL_KEY: <my_private_key>

stages:
  - pipeline
  - staging
  - release

prepare-prerelease:
  image: openjdk:8-jdk
  stage: pipeline
  allow_failure: true
  script:
    - curl -L -o saref-pipeline.jar "https://gitlab.emse.fr/saref/saref-pipeline/-/jobs/artifacts/master/raw/target/saref-pipeline.jar?job=build"
    - java -jar saref-pipeline.jar prerelease-portal -t && touch target/success
  except:
    - master
  artifacts:
    paths:
      - target/site
      - target/success
    reports:
      junit: target/report_output.xml

prepare-release:
  image: openjdk:8-jdk
  stage: pipeline
  script:
    - curl -L -o saref-pipeline.jar "https://gitlab.emse.fr/saref/saref-pipeline/-/jobs/artifacts/master/raw/target/saref-pipeline.jar?job=build"
    - java -jar saref-pipeline.jar release-portal && touch target/success
  only:
    - master
  artifacts:
    paths:
      - target/site
      - target/dataset
      - target/success
    reports:
      junit: target/report_output.xml

staging:
  image: maximelefrancois86/alpine-ssh-rsync
  stage: staging
  script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAREF_DEV_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - scp -pr '-P 29419' target/site saref-dev@saref.etsi.org:~/staging/$CI_PIPELINE_ID
    - echo "SAREF staging portal  https://saref.etsi.org/staging/$CI_PIPELINE_ID/" 
    - exit $(test -f target/success) $?

deploy:
  image: maximelefrancois86/alpine-ssh-rsync
  stage: release
  when: manual
  script:
    - eval $(ssh-agent -s)
    - ssh-add <(echo "$SAREF_PORTAL_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - rsync -rtcv -e 'ssh -p 29419' error-documents/ saref-portal@saref.etsi.org:~/error-documents
    - rsync -rtcv -e 'ssh -p 29419' target/site/ saref-portal@saref.etsi.org:~/site
    - scp -pr '-P 29419' target/dataset saref-portal@saref.etsi.org:~/dataset
  only:
   - master 
