# Please don't delete this file!!!

workflow:
  rules:
    - if: $CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == "master"
      when: always
    - when: never

variables:
  # This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
  # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
  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"
  # As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
  # when running from the command line.
  # `installAtEnd` and `deployAtEnd` are only effective with recent version of the corresponding plugins.
  #MAVEN_CLI_OPTS: "-s .m2/settings.xml --batch-mode --errors --fail-at-end --show-version -DinstallAtEnd=true -DdeployAtEnd=true"
  MAVEN_CLI_OPTS: "-s settings.xml --errors --fail-at-end --show-version -DdeployAtEnd=true"

# This template uses jdk11 (Corretto) for verifying and deploying images
image: $CI_DEPENDENCY_PROXY_GROUP_IMAGE_PREFIX/artaius/jfx:8u322

# To keep cache across branches add 'key: "$CI_JOB_NAME"'
cache:
  paths:
    - cache_variables.env

stages:
  - setup
  - prepare_triggers
  - triggers
  - cleanup

#####################################################
# read variables
#####################################################
vars:
  stage: setup
  when: delayed
  # This job is delayed to allow sonatype to complete the OCL release initiated by GitHub action.
  start_in: 15 minutes
  script: |
    source ~/.bashrc # needed to include JAVA_HOME env var
    test -f cache_variables.env && source cache_variables.env # needed to include RELEASE_VERSION env var

    echo "Getting previous release version no from cache..."    
    RELEASE_VERSION_PREVIOUS=${RELEASE_VERSION}
    echo "RELEASE_VERSION_PREVIOUS: ${RELEASE_VERSION_PREVIOUS}"

    echo "Getting release version no from sonatype.org..."    
    RELEASE_VERSION=$(curl -gL https://oss.sonatype.org/content/repositories/releases/com/actelion/research/openchemlib/maven-metadata.xml -s | grep release | sed -n 's/.*<release>\(.*\)<\/release>.*/\1/p')
    echo "RELEASE_VERSION: ${RELEASE_VERSION}"
    echo "RELEASE_VERSION=${RELEASE_VERSION}" > cache_variables.env

    echo "Getting current version no from pom.xml..."    
    CURR_VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
    echo "CURR_VERSION: ${CURR_VERSION}"

    if [[ ${RELEASE_VERSION} == ${RELEASE_VERSION_PREVIOUS} ]]; then
      echo "DEPLOYMENT_TYPE=snapshot"
      echo "DEPLOYMENT_TYPE=snapshot" >> variables.env
      echo "LOCAL_DEPLOYMENT_TYPE=snapshot" >> variables.env
    else
      echo "DEPLOYMENT_TYPE=release"
      echo "DEPLOYMENT_TYPE=release" >> variables.env
      echo "LOCAL_DEPLOYMENT_TYPE=release" >> variables.env
    fi
  artifacts:
    reports:
      # Use artifacts:reports:dotenv to expose the variables to other jobs
      dotenv:
        - variables.env

#####################################################
# write trigger job
#####################################################
create_triggers:
  stage: prepare_triggers
  script:
    - 'echo "Creating trigger file..."'
    - 'echo "init:" > trigger.yaml'
    - 'echo "  script:" >> trigger.yaml'
    - 'echo "    - echo \"DEPLOYMENT_TYPE is ${LOCAL_DEPLOYMENT_TYPE}\"" >> trigger.yaml'
    - 'echo "trigger_dd_core:" >> trigger.yaml'
    - 'echo "  variables:" >> trigger.yaml'
    - 'echo "    DEPLOYMENT_TYPE: $LOCAL_DEPLOYMENT_TYPE" >> trigger.yaml'
    - 'echo "  trigger: scdd/core-libraries/DD_core" >> trigger.yaml'
    - 'cat trigger.yaml'
  artifacts:
    paths:
      - trigger.yaml

#####################################################
# trigger dependent projects
#####################################################
trigger_dd_core:
  stage: triggers
  trigger:
    include:
      - artifact: trigger.yaml
        job: create_triggers

#####################################################
# Clear pipeline cache (see https://stackoverflow.com/questions/48469675/clearing-the-pipeline-cache-with-gitlab-ci)
#####################################################
cleanup:
  stage: cleanup
  script:
    - echo "Cleaning up"
    - rm -rf "${CACHE_PATH}/${CI_PIPELINE_ID}"
  when: always
