###
# PREPARATIONS
###

# Some variables
# TODO: Apply also as a template!
variables:
  # Maven Settings
  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"
  MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
  # Maven deployment settings for Nexus, please use env vars in Settings -> CI/CD -> Variables.
  MAVEN_SETTINGS_PATH: ".m2/settings.xml"
  MAVEN_DEPLOY_OPTS: "--settings=$MAVEN_SETTINGS_PATH"
  # Semantic versioning commit and push vars (the TOKEN name and user)
  GIT_AUTHOR_EMAIL: ${GL_USER}@noreply.gitlab.gwdg.de
  GIT_AUTHOR_NAME: ${GL_USER}
  GIT_COMMITTER_EMAIL: ${GL_USER}@noreply.gitlab.gwdg.de
  GIT_COMMITTER_NAME: ${GL_USER}

# Include Java settings from Gitlab templates repo.
include:
  - project: 'dariah-de/gitlab-templates'
    ref: 'main'
    file: '/templates/.java.gitlab-ci.yml'
    file: '/templates/SBOM-Upload.gitlab-ci.yml'

# Stages...
stages:
  - prepare
  - test
  - build
  - deploy

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

###
# JOBS
###

# Get the project version from main POM file and store it to artifact.
get-pom-version:
  stage: prepare
  only:
    - develop
    - tags
  extends:
    - .get-pom-version-to-env

# Semantic versioning: Update POM and CHANGELOG RELEASE files, TAG a new RELEASE version!
prepare-release:
  stage: prepare
  rules:
  # Only run if branch "main" AND commit title IS NOT "1.2.3" (main commit with tag) AND DOES NOT START WITH "Prepare next development iteration" (main commit with new version)
    - if: $CI_COMMIT_REF_NAME == "main" && $CI_COMMIT_TITLE !~ /^[\d\.]+$/ && $CI_COMMIT_TITLE !~ /^Prepare next development iteration/
  image: maven:3.8.3-jdk-8
  extends:
    - .prepare-semantic-release

# Build, validate, and package all the feature branches
validate-java:
  stage: test
  except:
    - main
    - develop
    - tags
  image: maven:3.8.3-jdk-8
  script:
    - mvn $MAVEN_OPTS $MAVEN_CLI_OPTS $MAVEN_DEPLOY_OPTS -U clean validate package -Pdhrep.deb
  artifacts:
    reports:
      # Declare the JUnit reports (recursive pattern for multi-module projects)
      junit:
        - "**/target/*-reports/TEST-*.xml"

# Build and deploy all the JARs to GWDG Nexus, and build all the DEBs and store DEBs to artifacts.
# NOTE: "deploy" also triggers BOM creation!
build-and-deploy-jars:
  stage: build
  image: maven:3.8.3-jdk-8
  only:
    - develop
    - tags
  script:
    - mvn $MAVEN_OPTS $MAVEN_CLI_OPTS $MAVEN_DEPLOY_OPTS -U clean deploy -Pdhrep.deb -Psbom
    - cp target/bom.json .
  artifacts:
    paths:
      - ./oaipmh-webapp/target/*.deb
      - ./bom.json
    reports:
      # Declare the JUnit reports (recursive pattern for multi-module projects)
      junit:
        - "**/target/*-reports/TEST-*.xml"

# Deploy DEB files to APTLY SNAPSHOT repository.
deploy-snapshot-deb:
  stage: deploy
  only:
    - develop
  variables:
    PNAME: 'oaipmh-webapp'
    PPATH: '${PNAME}/target'
    PKEY: 'SNAPSHOT'
    APTLY_TARGET: 'indy-snapshots'
  extends:
    - .deploy-deb

# Deploy DEB files to APTLY RELEASE repository.
deploy-release-deb:
  stage: deploy
  only:
    - tags
  variables:
    PNAME: 'oaipmh-webapp'
    PPATH: '${PNAME}/target'
    PKEY: '${PVERSION}'
    APTLY_TARGET: 'indy-releases'
  extends:
    - .deploy-deb